8000 Add normalizer / denormalizer awarness · symfony/symfony@53cf61c · GitHub
[go: up one dir, main page]

Skip to content

Commit 53cf61c

Browse files
committed
Add normalizer / denormalizer awarness
1 parent 37f5264 commit 53cf61c

10 files changed

+208
-15
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
1818
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
1919
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
20+
use Symfony\Component\Serializer\SerializerAwareInterface;
21+
use Symfony\Component\Serializer\SerializerAwareTrait;
2022

2123
/**
2224
* Normalizer implementation.
2325
*
2426
* @author Kévin Dunglas <dunglas@gmail.com>
2527
*/
26-
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
28+
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
2729
{
30+
use SerializerAwareTrait;
31+
2832
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
2933
const OBJECT_TO_POPULATE = 'object_to_populate';
3034
const GROUPS = 'groups';

src/Symfony/Component/Serializer/Normalizer/CustomNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\SerializerAwareInterface;
15+
use Symfony\Component\Serializer\SerializerAwareTrait;
16+
1417
/**
1518
* @author Jordi Boggiano <j.boggiano@seld.be>
1619
*/
17-
class CustomNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
20+
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
1821
{
22+
use SerializerAwareTrait;
23+
1924
/** D7AE
2025
* {@inheritdoc}
2126
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
/**
15+
* Class accepting a denormalizer.
16+
*
17+
* @author Joel Wurtz <joel.wurtz@gmail.com>
18+
*/
19+
interface DenormalizerAwareInterface
20+
{
21+
/**
22+
* Sets the owning Denormalizer object.
23+
*
24+
* @param DenormalizerInterface $denormalizer
25+
*/
26+
public function setDenormalizer(DenormalizerInterface $denormalizer);
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
/**
15+
* DenormalizerAware trait.
16+
*
17+
* @author Joel Wurtz <joel.wurtz@gmail.com>
18+
*/
19+
trait DenormalizerAwareTrait
20+
{
21+
/**
22+
* @var DenormalizerInterface
23+
*/
24+
protected $denormalizer;
25+
26+
/**
27+
* Sets the Denormalizer.
28+
*
29+
* @param DenormalizerInterface $denormalizer A DenormalizerInterface instance
30+
*/
31+
public function setSerializer(DenormalizerInterface $denormalizer)
32+
{
33+
$this->denormalizer = $denormalizer;
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
/**
15+
* Class accepting a normalizer.
16+
*
17+
* @author Joel Wurtz <joel.wurtz@gmail.com>
18+
*/
19+
interface NormalizerAwareInterface
20+
{
21+
/**
22+
* Sets the owning Normalizer object.
23+
*
24+
* @param NormalizerInterface $normalizer
25+
*/
26+
public function setNormalizer(NormalizerInterface $normalizer);
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
/**
15+
* NormalizerAware trait.
16+
*
17+
* @author Joel Wurtz <joel.wurtz@gmail.com>
18+
*/
19+
trait NormalizerAwareTrait
20+
{
21+
/**
22+
* @var NormalizerInterface
23+
*/
24+
protected $normalizer;
25+
26+
/**
27+
* Sets the normalizer.
28+
*
29+
* @param NormalizerInterface $normalizer A NormalizerInterface instance< 10000 /div>
30+
*/
31+
public function setSerializer(NormalizerInterface $normalizer)
32+
{
33+
$this->normalizer = $normalizer;
34+
}
35+
}

src/Symfony/Component/Serializer/Normalizer/SerializerAwareNormalizer.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,17 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14-
use Symfony\Component\Serializer\SerializerInterface;
14+
use Symfony\Component\Serializer\SerializerAwareTrait;
1515
use Symfony\Component\Serializer\SerializerAwareInterface;
1616

1717
/**
1818
* SerializerAware Normalizer implementation.
1919
*
2020
* @author Jordi Boggiano <j.boggiano@seld.be>
21+
*
22+
* @deprecated since version 3.1, to be removed in 4.0. Use the SerializerAwareTrait instead.
2123
*/
2224
abstract class SerializerAwareNormalizer implements SerializerAwareInterface
2325
{
24-
/**
25-
* @var SerializerInterface
26-
*/
27-
protected $serializer;
28-
29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function setSerializer(SerializerInterface $serializer)
33-
{
34-
$this->serializer = $serializer;
35-
}
26+
use SerializerAwareTrait;
3627
}

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Serializer\Encoder\ChainEncoder;
1616
use Symfony\Component\Serializer\Encoder\EncoderInterface;
1717
use Symfony\Component\Serializer\Encoder\DecoderInterface;
18+
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
19+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
1820
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1921
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2022
use Symfony\Component\Serializer\Exception\LogicException;
@@ -64,6 +66,14 @@ public function __construct(array $normalizers = array(), array $encoders = arra
6466
if ($normalizer instanceof SerializerAwareInterface) {
6567
$normalizer->setSerializer($this);
6668
}
69+
70+
if ($normalizer instanceof DenormalizerAwareInterface) {
71+
$normalizer->setDenormalizer($this);
72+
}
73+
74+
if ($normalizer instanceof NormalizerAwareInterface) {
75+
$normalizer->setNormalizer($this);
76+
}
6777
}
6878
$this->normalizers = $normalizers;
6979

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer;
13+
14+
/**
15+
* SerializerAware trait.
16+
*
17+
* @author Joel Wurtz <joel.wurtz@gmail.com>
18+
*/
19+
trait SerializerAwareTrait
20+
{
21+
/**
22+
* @var SerializerInterface
23+
*/
24+
protected $serializer;
25+
26+
/**
27+
* Sets the serializer.
28+
*
29+
* @param SerializerInterface $serializer A SerializerInterface instance
30+
*/
31+
public function setSerializer(SerializerInterface $serializer)
32+
{
33+
$this->serializer = $serializer;
34+
}
35+
}

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Symfony\Component\Serializer\Tests;
1313

1414
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
15+
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
16+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
17+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
18+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1519
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1620
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
1721
use Symfony\Component\Serializer\Serializer;
@@ -312,6 +316,26 @@ public function testDeserializeArray()
312316
$serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
313317
);
314318
}
319+
320+
public function testNormalizerAware()
321+
{
322+
$normalizerAware = $this->getMock(NormalizerAwareInterface::class);
323+
$normalizerAware->expects($this->once())
324+
->method('setNormalizer')
325+
->with($this->isInstanceOf(NormalizerInterface::class));
326+
327+
new Serializer([$normalizerAware]);
328+
}
329+
330+
public function testDenormalizerAware()
331+
{
332+
$denormalizerAware = $this->getMock(DenormalizerAwareInterface::class);
333+
$denormalizerAware->expects($this->once())
334+
->method('setDenormalizer')
335+
->with($this->isInstanceOf(DenormalizerInterface::class));
336+
337+
new Serializer([$denormalizerAware]);
338+
}
315339
}
316340

317341
class Model

0 commit comments

Comments
 (0)
0