[Serializer] Introduce constants for context keys
This commit is contained in:
parent
31aef7ba79
commit
c56c7bf869
@ -27,6 +27,10 @@ use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
|
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||||
{
|
{
|
||||||
|
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
|
||||||
|
const OBJECT_TO_POPULATE = 'object_to_populate';
|
||||||
|
const GROUPS = 'groups';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
@ -185,16 +189,16 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
{
|
{
|
||||||
$objectHash = spl_object_hash($object);
|
$objectHash = spl_object_hash($object);
|
||||||
|
|
||||||
if (isset($context['circular_reference_limit'][$objectHash])) {
|
if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) {
|
||||||
if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
|
if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) {
|
||||||
unset($context['circular_reference_limit'][$objectHash]);
|
unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
++$context['circular_reference_limit'][$objectHash];
|
++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
|
||||||
} else {
|
} else {
|
||||||
$context['circular_reference_limit'][$objectHash] = 1;
|
$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -248,13 +252,13 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
*/
|
*/
|
||||||
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
|
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
|
||||||
{
|
{
|
||||||
if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
|
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$allowedAttributes = array();
|
$allowedAttributes = array();
|
||||||
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
|
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
|
||||||
if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
|
if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS]))) {
|
||||||
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
|
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,12 +300,12 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
|
|||||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
||||||
{
|
{
|
||||||
if (
|
if (
|
||||||
isset($context['object_to_populate']) &&
|
isset($context[static::OBJECT_TO_POPULATE]) &&
|
||||||
is_object($context['object_to_populate']) &&
|
is_object($context[static::OBJECT_TO_POPULATE]) &&
|
||||||
$context['object_to_populate'] instanceof $class
|
$context[static::OBJECT_TO_POPULATE] instanceof $class
|
||||||
) {
|
) {
|
||||||
$object = $context['object_to_populate'];
|
$object = $context[static::OBJECT_TO_POPULATE];
|
||||||
unset($context['object_to_populate']);
|
unset($context[static::OBJECT_TO_POPULATE]);
|
||||||
|
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer;
|
|||||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
|
||||||
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
|
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
|
||||||
@ -55,10 +56,10 @@ class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
||||||
|
|
||||||
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), true);
|
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), true);
|
||||||
$this->assertEquals(array('a2', 'a4'), $result);
|
$this->assertEquals(array('a2', 'a4'), $result);
|
||||||
|
|
||||||
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), true);
|
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), true);
|
||||||
$this->assertEquals(array('a3', 'a4'), $result);
|
$this->assertEquals(array('a3', 'a4'), $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,10 +85,10 @@ class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
|
||||||
|
|
||||||
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), false);
|
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
|
||||||
$this->assertEquals(array($a2, $a4), $result);
|
$this->assertEquals(array($a2, $a4), $result);
|
||||||
|
|
||||||
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
|
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
|
||||||
$this->assertEquals(array($a3, $a4), $result);
|
$this->assertEquals(array($a3, $a4), $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$proxyDummy = new ProxyDummy();
|
$proxyDummy = new ProxyDummy();
|
||||||
|
|
||||||
$context = array('object_to_populate' => $proxyDummy);
|
$context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy);
|
||||||
|
|
||||||
$normalizer = new ObjectNormalizer();
|
$normalizer = new ObjectNormalizer();
|
||||||
$normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
|
$normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);
|
||||||
|
@ -277,7 +277,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('c'))));
|
||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'symfony' => 'symfony',
|
'symfony' => 'symfony',
|
||||||
@ -286,7 +286,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
'kevin' => 'kevin',
|
'kevin' => 'kevin',
|
||||||
'coopTilleuls' => 'coopTilleuls',
|
'coopTilleuls' => 'coopTilleuls',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('a', 'c'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGroupsDenormalize()
|
public function testGroupsDenormalize()
|
||||||
@ -304,7 +304,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a'))
|
array(GetSetMethodNormalizer::GROUPS => array('a'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
|
|
||||||
@ -314,7 +314,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a', 'b'))
|
array(GetSetMethodNormalizer::GROUPS => array('a', 'b'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
}
|
}
|
||||||
@ -336,7 +336,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
),
|
),
|
||||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
$this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,7 +357,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -533,7 +533,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('bar' => 'bar'),
|
array('bar' => 'bar'),
|
||||||
__NAMESPACE__.'\GetSetDummy',
|
__NAMESPACE__.'\GetSetDummy',
|
||||||
null,
|
null,
|
||||||
array('object_to_populate' => $dummy)
|
array(GetSetMethodNormalizer::OBJECT_TO_POPULATE => $dummy)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals($dummy, $obj);
|
$this->assertEquals($dummy, $obj);
|
||||||
|
@ -197,7 +197,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('c'))));
|
||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'symfony' => 'symfony',
|
'symfony' => 'symfony',
|
||||||
@ -206,7 +206,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
'kevin' => 'kevin',
|
'kevin' => 'kevin',
|
||||||
'coopTilleuls' => 'coopTilleuls',
|
'coopTilleuls' => 'coopTilleuls',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('a', 'c'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGroupsDenormalize()
|
public function testGroupsDenormalize()
|
||||||
@ -224,7 +224,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a'))
|
array(ObjectNormalizer::GROUPS => array('a'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
|
|
||||||
@ -234,7 +234,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a', 'b'))
|
array(ObjectNormalizer::GROUPS => array('a', 'b'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
),
|
),
|
||||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
$this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +277,7 @@ class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(ObjectNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));
|
||||||
|
|
||||||
// The PropertyNormalizer is not able to hydrate properties from parent classes
|
// The PropertyNormalizer is not able to hydrate properties from parent classes
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
@ -222,7 +222,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo' => 'foo',
|
'foo' => 'foo',
|
||||||
'fooBar' => 'fooBar',
|
'fooBar' => 'fooBar',
|
||||||
'bar' => 'bar',
|
'bar' => 'bar',
|
||||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGroupsDenormalize()
|
public function testGroupsDenormalize()
|
||||||
@ -240,7 +240,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a'))
|
array(PropertyNormalizer::GROUPS => array('a'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
|
|
||||||
@ -250,7 +250,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$toNormalize,
|
$toNormalize,
|
||||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||||
null,
|
null,
|
||||||
array('groups' => array('a', 'b'))
|
array(PropertyNormalizer::GROUPS => array('a', 'b'))
|
||||||
);
|
);
|
||||||
$this->assertEquals($obj, $normalized);
|
$this->assertEquals($obj, $normalized);
|
||||||
}
|
}
|
||||||
@ -272,7 +272,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
),
|
),
|
||||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
$this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +293,7 @@ class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'foo_bar' => '@dunglas',
|
'foo_bar' => '@dunglas',
|
||||||
'symfony' => '@coopTilleuls',
|
'symfony' => '@coopTilleuls',
|
||||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(PropertyNormalizer::GROUPS => array('name_converter')))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user