10000 [Serializer] Give access to the context to support* methods · Bilge/symfony@6a7a16e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a7a16e

Browse files
dunglasfabpot
authored andcommitted
[Serializer] Give access to the context to support* methods
1 parent a0f1e85 commit 6a7a16e

11 files changed

+227
-49
lines changed

src/Symfony/Component/Serializer/Encoder/ChainDecoder.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final since version 3.3.
2424
*/
25-
class ChainDecoder implements DecoderInterface
25+
class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*/
2626
{
2727
protected $decoders = array();
2828
protected $decoderByFormat = array();
@@ -37,16 +37,18 @@ public function __construct(array $decoders = array())
3737
*/
3838
final public function decode($data, $format, array $context = array())
3939
{
40-
return $this->getDecoder($format)->decode($data, $format, $context);
40+
return $this->getDecoder($format, $context)->decode($data, $format, $context);
4141
}
4242

4343
/**
4444
* {@inheritdoc}
4545
*/
46-
public function supportsDecoding($format)
46+
public function supportsDecoding($format/*, array $context = array()*/)
4747
{
48+
$context = func_num_args() > 1 ? func_get_arg(1) : array();
49+
4850
try {
49-
$this->getDecoder($format);
51+
$this->getDecoder($format, $context);
5052
} catch (RuntimeException $e) {
5153
return false;
5254
}
@@ -58,12 +60,13 @@ public function supportsDecoding($format)
5860
* Gets the decoder supporting the format.
5961
*
6062
* @param string $format
63+
* @param array $context
6164
*
6265
* @return DecoderInterface
6366
*
6467
* @throws RuntimeException If no decoder is found.
6568
*/
66-
private function getDecoder($format)
69+
private function getDecoder($format, array $context)
6770
{
6871
if (isset($this->decoderByFormat[$format])
6972
&& isset($this->decoders[$this->decoderByFormat[$format]])
@@ -72,7 +75,7 @@ private function getDecoder($format)
7275
}
7376

7477
foreach ($this->decoders as $i => $decoder) {
75-
if ($decoder->supportsDecoding($format)) {
78+
if ($decoder->supportsDecoding($format, $context)) {
7679
$this->decoderByFormat[$format] = $i;
7780

7881
return $decoder;

src/Symfony/Component/Serializer/Encoder/ChainEncoder.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final since version 3.3.
2424
*/
25-
class ChainEncoder implements EncoderInterface
25+
class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/
2626
{
2727
protected $encoders = array();
2828
protected $encoderByFormat = array();
@@ -37,16 +37,18 @@ public function __construct(array $encoders = array())
3737
*/
3838
final public function encode($data, $format, array $context = array())
3939
{
40-
return $this->getEncoder($format)->encode($data, $format, $context);
40+
return $this->getEncoder($format, $context)->encode($data, $format, $context);
4141
}
4242

4343
/**
4444
* {@inheritdoc}
4545
*/
46-
public function supportsEncoding($format)
46+
public function supportsEncoding($format/*, array $context = array()*/)
4747
{
48+
$context = func_num_args() > 1 ? func_get_arg(1) : array();
49+
4850
try {
49-
$this->getEncoder($format);
51+
$this->getEncoder($format, $context);
5052
} catch (RuntimeException $e) {
5153
return false;
5254
}
@@ -58,19 +60,21 @@ public function supportsEncoding($format)
5860
* Checks whether the normalization is needed for the given format.
5961
*
6062
* @param string $format
63+
* @param array $context
6164
*
6265
* @return bool
6366
*/
64-
public function needsNormalization($format)
67+
public function needsNormalization($format/*, array $context = array()*/)
6568
{
66-
$encoder = $this->getEncoder($format);
69+
$context = func_num_args() > 1 ? func_get_arg(1) : array();
70+
$encoder = $this->getEncoder($format, $context);
6771

6872
if (!$encoder instanceof NormalizationAwareInterface) {
6973
return true;
7074
}
7175

7276
if ($encoder instanceof self) {
73-
return $encoder->needsNormalization($format);
77+
return $encoder->needsNormalization($format, $context);
7478
}
7579

7680
return false;
@@ -80,12 +84,13 @@ public function needsNormalization($format)
8084
* Gets the encoder supporting the format.
8185
*
8286
* @param string $format
87+
* @param array $context
8388
*
8489
* @return EncoderInterface
8590
*
8691
* @throws RuntimeException if no encoder is found
8792
*/
88-
private function getEncoder($format)
93+
private function getEncoder($format, array $context)
8994
{
9095
if (isset($this->encoderByFormat[$format])
9196
&& isset($this->encoders[$this->encoderByFormat[$format]])
@@ -94,7 +99,7 @@ private function getEncoder($format)
9499
}
95100

96101
foreach ($this->encoders as $i => $encoder) {
97-
if ($encoder->supportsEncoding($format)) {
102+
if ($encoder->supportsEncoding($format, $context)) {
98103
$this->encoderByFormat[$format] = $i;
99104

100105
return $encoder;
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\Encoder;
13+
14+
/**
15+
* Adds the support of an extra $context parameter for the supportsDecoding method.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface ContextAwareDecoderInterface extends DecoderInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @param array $context options that decoders have access to
25+
*/
26+
public function supportsDecoding($format, array $context = array());
27+
}
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\Encoder;
13+
14+
/**
15+
* Adds the support of an extra $context parameter for the supportsEncoding method.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface ContextAwareEncoderInterface extends EncoderInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @param array $context options that encoders have access to
25+
*/
26+
public function supportsEncoding($format, array $context = array());
27+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
267267
throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
268268
}
269269

270-
if ($this->serializer->supportsDenormalization($data, $class, $format)) {
270+
if ($this->serializer->supportsDenormalization($data, $class, $format, $context)) {
271271
return $this->serializer->denormalize($data, $class, $format, $context);
272272
}
273273
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Denormalizes arrays of objects.
2222
*
2323
* @author Alexander M. Turek <me@derrabus.de>
24+
*
25+
* @final since version 3.3.
2426
*/
2527
class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
2628
{
@@ -64,10 +66,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
6466
/**
6567
* {@inheritdoc}
6668
*/
67-
public function supportsDenormalization($data, $type, $format = null)
69+
public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/)
6870
{
71+
$context = func_num_args() > 3 ? func_get_arg(3) : array();
72+
6973
return substr($type, -2) === '[]'
70-
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
74+
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
7175
}
7276

7377
/**
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+
* Adds the support of an extra $context parameter for the supportsDenormalization method.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface ContextAwareDenormalizerInterface extends DenormalizerInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @param array $context options that denormalizers have access to
25+
*/
26+
public function supportsDenormalization($data, $type, $format = null, array $context = array());
27+
}
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+
* Adds the support of an extra $context parameter for the supportsNormalization method.
16+
*
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
interface ContextAwareNormalizerInterface extends NormalizerInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @param array $context options that normalizers have access to
25+
*/
26+
public function supportsNormalization($data, $format = null, array $context = array());
27+
}

0 commit comments

Comments
 (0)
0