Skip to content

Commit

Permalink
[Serializer] Introduce constants for context keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Jan 18, 2016
1 parent 31aef7b commit c56c7bf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
28 changes: 16 additions & 12 deletions src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -84,18 +85,18 @@ 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);
}

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);
Expand Down
Expand Up @@ -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',
Expand All @@ -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()
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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',
Expand All @@ -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()
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand Down
Expand Up @@ -214,15 +214,15 @@ 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(
'symfony' => 'symfony',
'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()
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -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')))
);
}

Expand All @@ -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')))
);
}

Expand Down

0 comments on commit c56c7bf

Please sign in to comment.