-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: normalize SubscriptionItemPriceChangeDetails
wip: serializer wip: drop ambigiuous test Update composer.lock Update TestCase.php move Data converter to root wip: rename normalizer wip: time normalizer wip: code refactoring wip: add tests
- Loading branch information
Showing
14 changed files
with
194 additions
and
151 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imdhemy\GooglePlay\Normalizer; | ||
|
||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
final readonly class Normalizer | ||
{ | ||
public function __construct(private SerializerInterface $serializer) | ||
{ | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
return new self(new Serializer([ | ||
new TimeNormalizer(), | ||
new ObjectNormalizer(), | ||
], [new JsonEncoder()])); | ||
} | ||
|
||
/** | ||
* Deserializes data into the given type. | ||
* | ||
* @template TObject of object | ||
* @template TType of string|class-string<TObject> | ||
* | ||
* @param TType $type | ||
* | ||
* @psalm-return (TType is class-string<TObject> ? TObject : mixed) | ||
* | ||
* @phpstan-return ($type is class-string<TObject> ? TObject : mixed) | ||
* | ||
* @psalm-suppress MixedReturnStatement | ||
*/ | ||
public function normalize(array $data, string $type): mixed | ||
{ | ||
$json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); | ||
|
||
return $this->serializer->deserialize($json, $type, 'json'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Imdhemy\GooglePlay\Normalizer; | ||
|
||
use Imdhemy\GooglePlay\ValueObjects\Time; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
final class TimeNormalizer implements DenormalizerInterface | ||
{ | ||
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Time | ||
{ | ||
assert(is_string($data)); | ||
|
||
return new Time($data); | ||
} | ||
|
||
public function supportsDenormalization( | ||
mixed $data, | ||
string $type, | ||
?string $format = null, | ||
array $context = [], | ||
): bool { | ||
return Time::class === $type && is_string($data); | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [ | ||
Time::class => true, | ||
]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Tests\Normalizer; | ||
|
||
use Imdhemy\GooglePlay\Normalizer\TimeNormalizer; | ||
use Imdhemy\GooglePlay\ValueObjects\Time; | ||
use Tests\TestCase; | ||
|
||
final class TimeNormalizerTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function denormalize(): void | ||
{ | ||
$zulu = '2021-09-01T00:00:00Z'; | ||
$sut = new TimeNormalizer(); | ||
|
||
$time = $sut->denormalize($zulu, Time::class); | ||
|
||
$this->assertEquals($zulu, $time->originalValue); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider provide_data_for_supports_denormalization | ||
*/ | ||
public function supports_denormalization(mixed $data, string $type, bool $expected): void | ||
{ | ||
$sut = new TimeNormalizer(); | ||
|
||
$actual = $sut->supportsDenormalization($data, $type); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public static function provide_data_for_supports_denormalization(): array | ||
{ | ||
return [ | ||
'valid' => ['data' => '2021-09-01T00:00:00Z', 'type' => Time::class, 'expected' => true], | ||
'different_type' => ['data' => '2021-09-01T00:00:00Z', 'type' => 'string', 'expected' => false], | ||
'different_data' => ['data' => 123, 'type' => Time::class, 'expected' => false], | ||
]; | ||
} | ||
|
||
/** @test */ | ||
public function get_supported_types(): void | ||
{ | ||
$sut = new TimeNormalizer(); | ||
|
||
$actual = $sut->getSupportedTypes(null); | ||
|
||
$this->assertEquals([Time::class => true], $actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.