Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #19371 [Serializer] Give access to the context to support* me…
…thods (dunglas)

This PR was squashed before being merged into the 3.3-dev branch (closes #19371).

Discussion
----------

[Serializer] Give access to the context to support* methods

| Q | A |
| --- | --- |
| Branch? | master |
| Bug fix? | no (?) |
| New feature? | yes |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | - |
| License | MIT |
| Doc PR | n/a |

This is a current use case to want to access the context form the `supports*` methods. This PR fixes this limitation.

Maybe can it be considered a bug fix?

Commits
-------

6a7a16e [Serializer] Give access to the context to support* methods
  • Loading branch information
fabpot committed Feb 13, 2017
2 parents 320529e + 6a7a16e commit 17b4363
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 48 deletions.
15 changes: 9 additions & 6 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Expand Up @@ -22,7 +22,7 @@
*
* @final since version 3.3.
*/
class ChainDecoder implements DecoderInterface
class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*/
{
protected $decoders = array();
protected $decoderByFormat = array();
Expand All @@ -37,16 +37,18 @@ public function __construct(array $decoders = array())
*/
final public function decode($data, $format, array $context = array())
{
return $this->getDecoder($format)->decode($data, $format, $context);
return $this->getDecoder($format, $context)->decode($data, $format, $context);
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding($format/*, array $context = array()*/)
{
$context = func_num_args() > 1 ? func_get_arg(1) : array();

try {
$this->getDecoder($format);
$this->getDecoder($format, $context);
} catch (RuntimeException $e) {
return false;
}
Expand All @@ -58,12 +60,13 @@ public function supportsDecoding($format)
* Gets the decoder supporting the format.
*
* @param string $format
* @param array $context
*
* @return DecoderInterface
*
* @throws RuntimeException If no decoder is found.
*/
private function getDecoder($format)
private function getDecoder($format, array $context)
{
if (isset($this->decoderByFormat[$format])
&& isset($this->decoders[$this->decoderByFormat[$format]])
Expand All @@ -72,7 +75,7 @@ private function getDecoder($format)
}

foreach ($this->decoders as $i => $decoder) {
if ($decoder->supportsDecoding($format)) {
if ($decoder->supportsDecoding($format, $context)) {
$this->decoderByFormat[$format] = $i;

return $decoder;
Expand Down
23 changes: 14 additions & 9 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Expand Up @@ -22,7 +22,7 @@
*
* @final since version 3.3.
*/
class ChainEncoder implements EncoderInterface
class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/
{
protected $encoders = array();
protected $encoderByFormat = array();
Expand All @@ -37,16 +37,18 @@ public function __construct(array $encoders = array())
*/
final public function encode($data, $format, array $context = array())
{
return $this->getEncoder($format)->encode($data, $format, $context);
return $this->getEncoder($format, $context)->encode($data, $format, $context);
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding($format/*, array $context = array()*/)
{
$context = func_num_args() > 1 ? func_get_arg(1) : array();

try {
$this->getEncoder($format);
$this->getEncoder($format, $context);
} catch (RuntimeException $e) {
return false;
}
Expand All @@ -58,19 +60,21 @@ public function supportsEncoding($format)
* Checks whether the normalization is needed for the given format.
*
* @param string $format
* @param array $context
*
* @return bool
*/
public function needsNormalization($format)
public function needsNormalization($format/*, array $context = array()*/)
{
$encoder = $this->getEncoder($format);
$context = func_num_args() > 1 ? func_get_arg(1) : array();
$encoder = $this->getEncoder($format, $context);

if (!$encoder instanceof NormalizationAwareInterface) {
return true;
}

if ($encoder instanceof self) {
return $encoder->needsNormalization($format);
return $encoder->needsNormalization($format, $context);
}

return false;
Expand All @@ -80,12 +84,13 @@ public function needsNormalization($format)
* Gets the encoder supporting the format.
*
* @param string $format
* @param array $context
*
* @return EncoderInterface
*
* @throws RuntimeException if no encoder is found
*/
private function getEncoder($format)
private function getEncoder($format, array $context)
{
if (isset($this->encoderByFormat[$format])
&& isset($this->encoders[$this->encoderByFormat[$format]])
Expand All @@ -94,7 +99,7 @@ private function getEncoder($format)
}

foreach ($this->encoders as $i => $encoder) {
if ($encoder->supportsEncoding($format)) {
if ($encoder->supportsEncoding($format, $context)) {
$this->encoderByFormat[$format] = $i;

return $encoder;
Expand Down
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Encoder;

/**
* Adds the support of an extra $context parameter for the supportsDecoding method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface ContextAwareDecoderInterface extends DecoderInterface
{
/**
* {@inheritdoc}
*
* @param array $context options that decoders have access to
*/
public function supportsDecoding($format, array $context = array());
}
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Encoder;

/**
* Adds the support of an extra $context parameter for the supportsEncoding method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface ContextAwareEncoderInterface extends EncoderInterface
{
/**
* {@inheritdoc}
*
* @param array $context options that encoders have access to
*/
public function supportsEncoding($format, array $context = array());
}
Expand Up @@ -21,6 +21,8 @@
* Denormalizes arrays of objects.
*
* @author Alexander M. Turek <me@derrabus.de>
*
* @final since version 3.3.
*/
class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
{
Expand Down Expand Up @@ -64,10 +66,12 @@ public function denormalize($data, $class, $format = null, array $context = arra
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
public function supportsDenormalization($data, $type, $format = null/*, array $context = array()*/)
{
$context = func_num_args() > 3 ? func_get_arg(3) : array();

return substr($type, -2) === '[]'
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
}

/**
Expand Down
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* Adds the support of an extra $context parameter for the supportsDenormalization method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface ContextAwareDenormalizerInterface extends DenormalizerInterface
{
/**
* {@inheritdoc}
*
* @param array $context options that denormalizers have access to
*/
public function supportsDenormalization($data, $type, $format = null, array $context = array());
}
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* Adds the support of an extra $context parameter for the supportsNormalization method.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface ContextAwareNormalizerInterface extends NormalizerInterface
{
/**
* {@inheritdoc}
*
* @param array $context options that normalizers have access to
*/
public function supportsNormalization($data, $format = null, array $context = array());
}

0 comments on commit 17b4363

Please sign in to comment.