From dc92bf29c3db643ab066e61dc44b2bab97e81b13 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Sun, 14 Mar 2021 21:11:49 +0000 Subject: [PATCH] add normalizers --- composer.json | 1 + src/CarbonNormalizer.php | 48 +++++++++++++++++++ src/ModelIdentifierNormalizer.php | 69 +++++++++++++++++++++++++++ src/ObjectWithDocblocksNormalizer.php | 27 +++++++++++ tests/Brick/MoneyNormalizerTest.php | 4 +- tests/Money/MoneyNormalizerTest.php | 4 +- 6 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 src/CarbonNormalizer.php create mode 100644 src/ModelIdentifierNormalizer.php create mode 100644 src/ObjectWithDocblocksNormalizer.php diff --git a/composer.json b/composer.json index 5e119fc..25a323c 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "brick/date-time": "^0.2.3", "brick/money": "^0.5.1", "friendsofphp/php-cs-fixer": "^2.17", + "illuminate/queue": "^8.32", "moneyphp/money": "^3.3", "nesbot/carbon": "^2.46", "phpunit/phpunit": "^9.5", diff --git a/src/CarbonNormalizer.php b/src/CarbonNormalizer.php new file mode 100644 index 0000000..28243b2 --- /dev/null +++ b/src/CarbonNormalizer.php @@ -0,0 +1,48 @@ +toRfc3339String(); + } + + /** + * @inheritdoc + */ + public function supportsNormalization($data, string $format = null) + { + return $data instanceof CarbonInterface; + } + + /** + * @inheritDoc + */ + public function denormalize($data, string $type, string $format = null, array $context = []) + { + return new Carbon($data); + } + + /** + * @inheritDoc + */ + public function supportsDenormalization($data, string $type, string $format = null) + { + return is_a($type, CarbonInterface::class, true); + } +} diff --git a/src/ModelIdentifierNormalizer.php b/src/ModelIdentifierNormalizer.php new file mode 100644 index 0000000..96c13b3 --- /dev/null +++ b/src/ModelIdentifierNormalizer.php @@ -0,0 +1,69 @@ +supportsNormalization($object)) { + throw new InvalidArgumentException('Cannot serialize an object that is not a QueueableEntity or QueueableCollection in ModelIdentifierNormalizer.'); + } + + return $this->getSerializedPropertyValue($object); + } + + /** + * @inheritdoc + */ + public function supportsNormalization($data, string $format = null) + { + return ($data instanceof QueueableEntity || $data instanceof QueueableCollection); + } + + /** + * @inheritdoc + */ + public function denormalize($data, $type, string $format = null, array $context = []) + { + $identifier = $data instanceof ModelIdentifier + ? $data + : new ModelIdentifier($data['class'], $data['id'], $data['relations'], $data['connection']); + + return $this->getRestoredPropertyValue($identifier); + } + + /** + * @inheritdoc + */ + public function supportsDenormalization($data, $type, string $format = null) + { + return $this->normalizedDataIsModelIdentifier($data) + && $this->isNormalizedToModelIdentifier($type); + } + + protected function normalizedDataIsModelIdentifier($data): bool + { + return $data instanceof ModelIdentifier + || isset($data['class'], $data['id'], $data['relations'], $data['connection']); + } + + protected function isNormalizedToModelIdentifier($class): bool + { + return is_a($class, QueueableEntity::class, true) + || is_a($class, QueueableCollection::class, true); + } +} diff --git a/src/ObjectWithDocblocksNormalizer.php b/src/ObjectWithDocblocksNormalizer.php new file mode 100644 index 0000000..d1b4890 --- /dev/null +++ b/src/ObjectWithDocblocksNormalizer.php @@ -0,0 +1,27 @@ +normalize($money); // Assert. - $this->assertEquals(['minor' => 5000, 'currency' => 'USD'], $normalized); + $this->assertEquals(['amount' => 5000, 'currency' => 'USD'], $normalized); } /** @test */ @@ -30,7 +30,7 @@ public function it_can_denormalize_money() { // Arrange. $normalized = [ - 'minor' => 5000, + 'amount' => 5000, 'currency' => 'USD', ]; $serializer = new Serializer([ diff --git a/tests/Money/MoneyNormalizerTest.php b/tests/Money/MoneyNormalizerTest.php index d6f9ade..0282a8c 100644 --- a/tests/Money/MoneyNormalizerTest.php +++ b/tests/Money/MoneyNormalizerTest.php @@ -22,7 +22,7 @@ public function it_can_normalize_money() $normalized = $serializer->normalize($money); // Assert. - $this->assertEquals(['minor' => 5000, 'currency' => 'USD'], $normalized); + $this->assertEquals(['amount' => 5000, 'currency' => 'USD'], $normalized); } /** @test */ @@ -30,7 +30,7 @@ public function it_can_denormalize_money() { // Arrange. $normalized = [ - 'minor' => 5000, + 'amount' => 5000, 'currency' => 'USD', ]; $serializer = new Serializer([