Skip to content

Commit

Permalink
add normalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop committed Mar 14, 2021
1 parent 3001230 commit dc92bf2
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions src/CarbonNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Morrislaptop\SymfonyCustomNormalizers;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class CarbonNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* @inheritdoc
*/
public function normalize($object, string $format = null, array $context = [])
{
if (! $object instanceof CarbonInterface) {
throw new InvalidArgumentException('Cannot serialize an object that is not a CarbonInterface in CarbonNormalizer.');
}

return $object->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);
}
}
69 changes: 69 additions & 0 deletions src/ModelIdentifierNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Morrislaptop\SymfonyCustomNormalizers;

use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class ModelIdentifierNormalizer implements NormalizerInterface, DenormalizerInterface
{
use SerializesAndRestoresModelIdentifiers;

/**
* @inheritdoc
*/
public function normalize($object, string $format = null, array $context = [])
{
if (! $this->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);
}
}
27 changes: 27 additions & 0 deletions src/ObjectWithDocblocksNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Morrislaptop\SymfonyCustomNormalizers;

use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as SymfonyObjectNormalizer;

class ObjectWithDocblocksNormalizer extends SymfonyObjectNormalizer
{
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
{
parent::__construct(
$classMetadataFactory,
$nameConverter,
$propertyAccessor,
$propertyTypeExtractor ?? new PhpDocExtractor(),
$classDiscriminatorResolver,
$objectClassResolver,
$defaultContext
);
}
}
4 changes: 2 additions & 2 deletions tests/Brick/MoneyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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 */
public function it_can_denormalize_money()
{
// Arrange.
$normalized = [
'minor' => 5000,
'amount' => 5000,
'currency' => 'USD',
];
$serializer = new Serializer([
Expand Down
4 changes: 2 additions & 2 deletions tests/Money/MoneyNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ 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 */
public function it_can_denormalize_money()
{
// Arrange.
$normalized = [
'minor' => 5000,
'amount' => 5000,
'currency' => 'USD',
];
$serializer = new Serializer([
Expand Down

0 comments on commit dc92bf2

Please sign in to comment.