Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6905cd

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

10 files changed

+209
-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 <[email protected]>
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 <[email protected]>
1619
*/
17-
class CustomNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
20+
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
1821
{
22+
use SerializerAwareTrait;
23+
1924
/**
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111

1212
namespace Symfony\Component\Serializer\Tests;
1313

14+
use Prophecy\Argument;
1415
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
16+
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
17+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
18+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
19+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1520
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1621
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
1722
use Symfony\Component\Serializer\Serializer;
@@ -312,6 +317,26 @@ public function testDeserializeArray()
312317
$serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
313318
);
314319
}
320+
321+
public function testNormalizerAware()
322+
{
323+
$normalizerAware = $this->getMock(NormalizerAwareInterface::class);
324+
$normalizerAware->expects($this->once())
325+
->method('setNormalizer')
326+
->with($this->isInstanceOf(NormalizerInterface::class));
327+
328+
new Serializer([$normalizerAware]);
329+
}
330+
331+
public function testDenormalizerAware()
332+
{
333+
$denormalizerAware = $this->getMock(DenormalizerAwareInterface::class);
334+
$denormalizerAware->expects($this->once())
335+
->method('setDenormalizer')
336+
->with($this->isInstanceOf(DenormalizerInterface::class));
337+
338+
new Serializer([$denormalizerAware]);
339+
}
315340
}
316341

317342
class Model

0 commit comments

Comments
 (0)