From c56c7bf869d952ba44ed48ab566fbca8361bd97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 18 Jan 2016 17:42:55 +0100 Subject: [PATCH] [Serializer] Introduce constants for context keys --- .../Normalizer/AbstractNormalizer.php | 28 +++++++++++-------- .../Normalizer/AbstractNormalizerTest.php | 11 ++++---- .../Normalizer/GetSetMethodNormalizerTest.php | 14 +++++----- .../Tests/Normalizer/ObjectNormalizerTest.php | 12 ++++---- .../Normalizer/PropertyNormalizerTest.php | 12 ++++---- 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index ae184bbeb6ea..f12d3d226308 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -27,6 +27,10 @@ */ 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 */ @@ -185,16 +189,16 @@ protected function isCircularReference($object, &$context) { $objectHash = spl_object_hash($object); - if (isset($context['circular_reference_limit'][$objectHash])) { - if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) { - unset($context['circular_reference_limit'][$objectHash]); + if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) { + if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) { + unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]); return true; } - ++$context['circular_reference_limit'][$objectHash]; + ++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]; } else { - $context['circular_reference_limit'][$objectHash] = 1; + $context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1; } return false; @@ -248,13 +252,13 @@ protected function formatAttribute($attributeName) */ 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; } $allowedAttributes = array(); 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; } } @@ -296,12 +300,12 @@ protected function prepareForDenormalization($data) protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes) { if ( - isset($context['object_to_populate']) && - is_object($context['object_to_populate']) && - $context['object_to_populate'] instanceof $class + isset($context[static::OBJECT_TO_POPULATE]) && + is_object($context[static::OBJECT_TO_POPULATE]) && + $context[static::OBJECT_TO_POPULATE] instanceof $class ) { - $object = $context['object_to_populate']; - unset($context['object_to_populate']); + $object = $context[static::OBJECT_TO_POPULATE]; + unset($context[static::OBJECT_TO_POPULATE]); return $object; } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index b317ec276fad..66ac99235031 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -5,6 +5,7 @@ use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassMetadata; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy; use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy; @@ -55,10 +56,10 @@ public function testGetAllowedAttributesAsString() $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); - $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); } @@ -84,10 +85,10 @@ public function testGetAllowedAttributesAsObjects() $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); - $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); } @@ -95,7 +96,7 @@ public function testObjectToPopulateWithProxy() { $proxyDummy = new ProxyDummy(); - $context = array('object_to_populate' => $proxyDummy); + $context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy); $normalizer = new ObjectNormalizer(); $normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index 1d487c2fdd5c..9027bfa7ec02 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -277,7 +277,7 @@ public function testGroupsNormalize() $this->assertEquals(array( 'bar' => 'bar', - ), $this->normalizer->normalize($obj, null, array('groups' => array('c')))); + ), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('c')))); $this->assertEquals(array( 'symfony' => 'symfony', @@ -286,7 +286,7 @@ public function testGroupsNormalize() 'bar' => 'bar', 'kevin' => 'kevin', '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() @@ -304,7 +304,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a')) + array(GetSetMethodNormalizer::GROUPS => array('a')) ); $this->assertEquals($obj, $normalized); @@ -314,7 +314,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a', 'b')) + array(GetSetMethodNormalizer::GROUPS => array('a', 'b')) ); $this->assertEquals($obj, $normalized); } @@ -336,7 +336,7 @@ public function testGroupsNormalizeWithNameConverter() 'foo_bar' => '@dunglas', '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 @@ public function testGroupsDenormalizeWithNameConverter() 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', '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 @@ public function testObjectToPopulate() array('bar' => 'bar'), __NAMESPACE__.'\GetSetDummy', null, - array('object_to_populate' => $dummy) + array(GetSetMethodNormalizer::OBJECT_TO_POPULATE => $dummy) ); $this->assertEquals($dummy, $obj); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 80a021d08fa2..c0602d7fa6b7 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -197,7 +197,7 @@ public function testGroupsNormalize() $this->assertEquals(array( 'bar' => 'bar', - ), $this->normalizer->normalize($obj, null, array('groups' => array('c')))); + ), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('c')))); $this->assertEquals(array( 'symfony' => 'symfony', @@ -206,7 +206,7 @@ public function testGroupsNormalize() 'bar' => 'bar', 'kevin' => 'kevin', '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() @@ -224,7 +224,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a')) + array(ObjectNormalizer::GROUPS => array('a')) ); $this->assertEquals($obj, $normalized); @@ -234,7 +234,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a', 'b')) + array(ObjectNormalizer::GROUPS => array('a', 'b')) ); $this->assertEquals($obj, $normalized); } @@ -256,7 +256,7 @@ public function testGroupsNormalizeWithNameConverter() 'foo_bar' => '@dunglas', '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 @@ public function testGroupsDenormalizeWithNameConverter() 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', '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'))) ); } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php index fd303818006f..381936ad006e 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php @@ -214,7 +214,7 @@ public function testGroupsNormalize() $this->assertEquals(array( '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 $this->assertEquals(array( @@ -222,7 +222,7 @@ public function testGroupsNormalize() 'foo' => 'foo', 'fooBar' => 'fooBar', '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() @@ -240,7 +240,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a')) + array(PropertyNormalizer::GROUPS => array('a')) ); $this->assertEquals($obj, $normalized); @@ -250,7 +250,7 @@ public function testGroupsDenormalize() $toNormalize, 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, - array('groups' => array('a', 'b')) + array(PropertyNormalizer::GROUPS => array('a', 'b')) ); $this->assertEquals($obj, $normalized); } @@ -272,7 +272,7 @@ public function testGroupsNormalizeWithNameConverter() 'foo_bar' => '@dunglas', '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 @@ public function testGroupsDenormalizeWithNameConverter() 'foo_bar' => '@dunglas', 'symfony' => '@coopTilleuls', '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'))) ); }