Skip to content

Commit ac9b31b

Browse files
authored
feat: support for Symfony Serializer 6+ (#110)
1 parent e32f507 commit ac9b31b

13 files changed

+190
-56
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"doctrine/orm": "^2.6.3",
1818
"symfony/property-access": "^4.4 || ^5.4 || ^6.0",
1919
"symfony/property-info": "^4.4 || ^5.4 || ^6.0",
20-
"symfony/serializer": "^4.4 || ^5.4"
20+
"symfony/serializer": "^4.4 || ^5.4 || ^6.0"
2121
},
2222
"require-dev": {
2323
"doctrine/doctrine-bundle": "^1.12.13 || ^2.2",

src/Serializer.php

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,25 @@
99

1010
namespace Dunglas\DoctrineJsonOdm;
1111

12+
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
1213
use Symfony\Component\Serializer\Serializer as BaseSerializer;
1314

14-
final class Serializer extends BaseSerializer
15-
{
16-
private const KEY_TYPE = '#type';
17-
private const KEY_SCALAR = '#scalar';
18-
19-
public function normalize($data, $format = null, array $context = [])
15+
if (method_exists(ArrayDenormalizer::class, 'setSerializer')) {
16+
// Symfony <=5.4
17+
final class Serializer extends BaseSerializer
2018
{
21-
$normalizedData = parent::normalize($data, $format, $context);
22-
23-
if (\is_object($data)) {
24-
$typeData = [self::KEY_TYPE => \get_class($data)];
25-
$valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : $normalizedData;
26-
$normalizedData = array_merge($typeData, $valueData);
27-
}
19+
use SerializerTrait;
2820

29-
return $normalizedData;
21+
private const KEY_TYPE = '#type';
22+
private const KEY_SCALAR = '#scalar';
3023
}
31-
32-
public function denormalize($data, $type, $format = null, array $context = [])
24+
} else {
25+
// Symfony >=6.0
26+
final class Serializer extends BaseSerializer
3327
{
34-
if (\is_array($data) && (isset($data[self::KEY_TYPE]))) {
35-
$keyType = $data[self::KEY_TYPE];
36-
unset($data[self::KEY_TYPE]);
37-
38-
$data = $data[self::KEY_SCALAR] ?? $data;
39-
$data = $this->denormalize($data, $keyType, $format, $context);
40-
41-
return parent::denormalize($data, $keyType, $format, $context);
42-
}
43-
44-
if (is_iterable($data)) {
45-
$type = ('' === $type) ? 'stdClass' : $type;
46-
47-
return parent::denormalize($data, $type.'[]', $format, $context);
48-
}
28+
use TypedSerializerTrait;
4929

50-
return $data;
30+
private const KEY_TYPE = '#type';
31+
private const KEY_SCALAR = '#scalar';
5132
}
5233
}

src/SerializerTrait.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* (c) Kévin Dunglas <[email protected]>
5+
*
6+
* This source file is subject to the MIT license that is bundled
7+
* with this source code in the file LICENSE.
8+
*/
9+
10+
namespace Dunglas\DoctrineJsonOdm;
11+
12+
/**
13+
* @internal
14+
*
15+
* @author Kévin Dunglas <[email protected]>
16+
*/
17+
trait SerializerTrait
18+
{
19+
/**
20+
* @param mixed $data
21+
* @param string|null $format
22+
*
23+
* @return array|\ArrayObject|bool|float|int|string|null
24+
*/
25+
public function normalize($data, $format = null, array $context = [])
26+
{
27+
$normalizedData = parent::normalize($data, $format, $context);
28+
29+
if (\is_object($data)) {
30+
$typeData = [self::KEY_TYPE => \get_class($data)];
31+
$valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : $normalizedData;
32+
$normalizedData = array_merge($typeData, $valueData);
33+
}
34+
35+
return $normalizedData;
36+
}
37+
38+
/**
39+
* @param $data
40+
*
41+
* @return mixed
42+
*/
43+
public function denormalize($data, $type, $format = null, array $context = [])
44+
{
45+
if (\is_array($data) && (isset($data[self::KEY_TYPE]))) {
46+
$keyType = $data[self::KEY_TYPE];
47+
unset($data[self::KEY_TYPE]);
48+
49+
$data = $data[self::KEY_SCALAR] ?? $data;
50+
$data = $this->denormalize($data, $keyType, $format, $context);
51+
52+
return parent::denormalize($data, $keyType, $format, $context);
53+
}
54+
55+
if (is_iterable($data)) {
56+
$type = ('' === $type) ? 'stdClass' : $type;
57+
58+
return parent::denormalize($data, $type.'[]', $format, $context);
59+
}
60+
61+
return $data;
62+
}
63+
}

src/TypedSerializerTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* (c) Kévin Dunglas <[email protected]>
5+
*
6+
* This source file is subject to the MIT license that is bundled
7+
* with this source code in the file LICENSE.
8+
*/
9+
10+
namespace Dunglas\DoctrineJsonOdm;
11+
12+
/**
13+
* @internal
14+
*
15+
* @author Kévin Dunglas <[email protected]>
16+
*/
17+
trait TypedSerializerTrait
18+
{
19+
use SerializerTrait {
20+
normalize as private doNormalize;
21+
denormalize as private doDenormalize;
22+
}
23+
24+
public function normalize(mixed $data, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
25+
{
26+
return $this->doNormalize($data, $format, $context);
27+
}
28+
29+
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
30+
{
31+
return $this->doDenormalize($data, $type, $format, $context);
32+
}
33+
}

tests/Fixtures/TestBundle/Entity/Attribute.php renamed to tests/Fixtures/TestBundle/Document/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* with this source code in the file LICENSE.
88
*/
99

10-
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
1111

1212
/**
1313
* @author Kévin Dunglas <[email protected]>

tests/Fixtures/TestBundle/Entity/Attributes.php renamed to tests/Fixtures/TestBundle/Document/Attributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* with this source code in the file LICENSE.
88
*/
99

10-
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
1111

1212
class Attributes
1313
{

tests/Fixtures/TestBundle/Entity/Bar.php renamed to tests/Fixtures/TestBundle/Document/Bar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* with this source code in the file LICENSE.
88
*/
99

10-
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
1111

1212
/**
1313
* @author Kévin Dunglas <[email protected]>

tests/Fixtures/TestBundle/Entity/Baz.php renamed to tests/Fixtures/TestBundle/Document/Baz.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* with this source code in the file LICENSE.
88
*/
99

10-
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
1111

1212
/**
1313
* @author Kévin Dunglas <[email protected]>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* (c) Kévin Dunglas <[email protected]>
5+
*
6+
* This source file is subject to the MIT license that is bundled
7+
* with this source code in the file LICENSE.
8+
*/
9+
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
11+
12+
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
13+
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
14+
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
15+
16+
/**
17+
* Fixture class to test object normalized as scalar values.
18+
*
19+
* @author Antonio J. García Lagar <[email protected]>
20+
*/
21+
if (method_exists(ArrayDenormalizer::class, 'setSerializer')) {
22+
// Symfony <=5.4
23+
class ScalarValue implements NormalizableInterface, DenormalizableInterface
24+
{
25+
use ScalarValueTrait;
26+
}
27+
} else {
28+
// Symfony >=6.0
29+
class ScalarValue implements NormalizableInterface, DenormalizableInterface
30+
{
31+
use ScalarValueTrait, TypedScalarValueTrait {
32+
TypedScalarValueTrait::normalize insteadof ScalarValueTrait;
33+
TypedScalarValueTrait::denormalize insteadof ScalarValueTrait;
34+
}
35+
}
36+
}

tests/Fixtures/TestBundle/Entity/ScalarValue.php renamed to tests/Fixtures/TestBundle/Document/ScalarValueTrait.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77
* with this source code in the file LICENSE.
88
*/
99

10-
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Entity;
10+
namespace Dunglas\DoctrineJsonOdm\Tests\Fixtures\TestBundle\Document;
1111

12-
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
1312
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14-
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
1513
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1614

17-
/**
18-
* Fixture class to test object normalized as scalar values.
19-
*
20-
* @author Antonio J. García Lagar <[email protected]>
21-
*/
22-
class ScalarValue implements NormalizableInterface, DenormalizableInterface
15+
trait ScalarValueTrait
2316
{
2417
private $value;
2518

0 commit comments

Comments
 (0)