Skip to content

Commit dc92bf2

Browse files
committed
add normalizers
1 parent 3001230 commit dc92bf2

File tree

6 files changed

+149
-4
lines changed

6 files changed

+149
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"brick/date-time": "^0.2.3",
2323
"brick/money": "^0.5.1",
2424
"friendsofphp/php-cs-fixer": "^2.17",
25+
"illuminate/queue": "^8.32",
2526
"moneyphp/money": "^3.3",
2627
"nesbot/carbon": "^2.46",
2728
"phpunit/phpunit": "^9.5",

src/CarbonNormalizer.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Morrislaptop\SymfonyCustomNormalizers;
4+
5+
use Carbon\Carbon;
6+
use Carbon\CarbonInterface;
7+
use InvalidArgumentException;
8+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10+
11+
class CarbonNormalizer implements NormalizerInterface, DenormalizerInterface
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function normalize($object, string $format = null, array $context = [])
17+
{
18+
if (! $object instanceof CarbonInterface) {
19+
throw new InvalidArgumentException('Cannot serialize an object that is not a CarbonInterface in CarbonNormalizer.');
20+
}
21+
22+
return $object->toRfc3339String();
23+
}
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
public function supportsNormalization($data, string $format = null)
29+
{
30+
return $data instanceof CarbonInterface;
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function denormalize($data, string $type, string $format = null, array $context = [])
37+
{
38+
return new Carbon($data);
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function supportsDenormalization($data, string $type, string $format = null)
45+
{
46+
return is_a($type, CarbonInterface::class, true);
47+
}
48+
}

src/ModelIdentifierNormalizer.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Morrislaptop\SymfonyCustomNormalizers;
4+
5+
use Illuminate\Contracts\Database\ModelIdentifier;
6+
use Illuminate\Contracts\Queue\QueueableCollection;
7+
use Illuminate\Contracts\Queue\QueueableEntity;
8+
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
9+
use InvalidArgumentException;
10+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12+
13+
class ModelIdentifierNormalizer implements NormalizerInterface, DenormalizerInterface
14+
{
15+
use SerializesAndRestoresModelIdentifiers;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function normalize($object, string $format = null, array $context = [])
21+
{
22+
if (! $this->supportsNormalization($object)) {
23+
throw new InvalidArgumentException('Cannot serialize an object that is not a QueueableEntity or QueueableCollection in ModelIdentifierNormalizer.');
24+
}
25+
26+
return $this->getSerializedPropertyValue($object);
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function supportsNormalization($data, string $format = null)
33+
{
34+
return ($data instanceof QueueableEntity || $data instanceof QueueableCollection);
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function denormalize($data, $type, string $format = null, array $context = [])
41+
{
42+
$identifier = $data instanceof ModelIdentifier
43+
? $data
44+
: new ModelIdentifier($data['class'], $data['id'], $data['relations'], $data['connection']);
45+
46+
return $this->getRestoredPropertyValue($identifier);
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function supportsDenormalization($data, $type, string $format = null)
53+
{
54+
return $this->normalizedDataIsModelIdentifier($data)
55+
&& $this->isNormalizedToModelIdentifier($type);
56+
}
57+
58+
protected function normalizedDataIsModelIdentifier($data): bool
59+
{
60+
return $data instanceof ModelIdentifier
61+
|| isset($data['class'], $data['id'], $data['relations'], $data['connection']);
62+
}
63+
64+
protected function isNormalizedToModelIdentifier($class): bool
65+
{
66+
return is_a($class, QueueableEntity::class, true)
67+
|| is_a($class, QueueableCollection::class, true);
68+
}
69+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Morrislaptop\SymfonyCustomNormalizers;
4+
5+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
6+
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
7+
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
8+
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
9+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
10+
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
11+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as SymfonyObjectNormalizer;
12+
13+
class ObjectWithDocblocksNormalizer extends SymfonyObjectNormalizer
14+
{
15+
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
16+
{
17+
parent::__construct(
18+
$classMetadataFactory,
19+
$nameConverter,
20+
$propertyAccessor,
21+
$propertyTypeExtractor ?? new PhpDocExtractor(),
22+
$classDiscriminatorResolver,
23+
$objectClassResolver,
24+
$defaultContext
25+
);
26+
}
27+
}

tests/Brick/MoneyNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public function it_can_normalize_money()
2222
$normalized = $serializer->normalize($money);
2323

2424
// Assert.
25-
$this->assertEquals(['minor' => 5000, 'currency' => 'USD'], $normalized);
25+
$this->assertEquals(['amount' => 5000, 'currency' => 'USD'], $normalized);
2626
}
2727

2828
/** @test */
2929
public function it_can_denormalize_money()
3030
{
3131
// Arrange.
3232
$normalized = [
33-
'minor' => 5000,
33+
'amount' => 5000,
3434
'currency' => 'USD',
3535
];
3636
$serializer = new Serializer([

tests/Money/MoneyNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public function it_can_normalize_money()
2222
$normalized = $serializer->normalize($money);
2323

2424
// Assert.
25-
$this->assertEquals(['minor' => 5000, 'currency' => 'USD'], $normalized);
25+
$this->assertEquals(['amount' => 5000, 'currency' => 'USD'], $normalized);
2626
}
2727

2828
/** @test */
2929
public function it_can_denormalize_money()
3030
{
3131
// Arrange.
3232
$normalized = [
33-
'minor' => 5000,
33+
'amount' => 5000,
3434
'currency' => 'USD',
3535
];
3636
$serializer = new Serializer([

0 commit comments

Comments
 (0)