Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e5da4cd

Browse filesBrowse files
committed
more documentation on the constants and test cleanup
1 parent 25c69ae commit e5da4cd
Copy full SHA for e5da4cd

10 files changed

+97
-47
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+29-7Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,24 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
3333
use SerializerAwareTrait;
3434

3535
/* constants to configure the context */
36-
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
37-
const OBJECT_TO_POPULATE = 'object_to_populate';
36+
/**
37+
* How many loops of circular reference to allow while normalizing.
38+
*
39+
* The default value of 1 means that when we encounter the same object a
40+
* second time, we consider that a circular reference.
41+
*
42+
* You can raise this value for special cases, e.g. in combination with the
43+
* max depth setting of the object normalizer.
44+
*/
45+
public const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
46+
47+
/**
48+
* Instead of creating a new instance of an object, update the specified object.
49+
*
50+
* If you have a nested structure, child objects will be overwritten with
51+
* new instances unless you set DEEP_OBJECT_TO_POPULATE to true.
52+
*/
53+
public const OBJECT_TO_POPULATE = 'object_to_populate';
3854

3955
/**
4056
* Only (de)normalize attributes that are in the specified groups.
@@ -68,7 +84,16 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
6884
* - array $context the serialization context
6985
*/
7086
public const CALLBACKS = 'callbacks';
71-
const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
87+
88+
/**
89+
* Handler to call when a circular reference has been detected.
90+
*
91+
* If you specify no handler, a CircularReferenceException is thrown.
92+
*
93+
* The method will be called with ($object, $format, $context) and its
94+
* return value is returned as the result of the normalize call.
95+
*/
96+
public const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
7297

7398
/**
7499
* Skip the specified attributes when normalizing an object tree.
@@ -80,10 +105,7 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
80105
*/
81106
public const IGNORED_ATTRIBUTES = 'ignored_attributes';
82107

83-
/**
84-
* @internal
85-
*/
86-
const CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'circular_reference_limit_counters';
108+
protected const CIRCULAR_REFERENCE_LIMIT_COUNTERS = 'circular_reference_limit_counters';
87109

88110
protected $defaultContext = [
89111
self::ALLOW_EXTRA_ATTRIBUTES => true,

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+34-6Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,41 @@
3333
*/
3434
abstract class AbstractObjectNormalizer extends AbstractNormalizer
3535
{
36-
const ENABLE_MAX_DEPTH = 'enable_max_depth';
37-
const DEPTH_KEY_PATTERN = 'depth_%s::%s';
38-
const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
36+
/**
37+
* Set to true to respect the max depth metadata on fields.
38+
*/
39+
public const ENABLE_MAX_DEPTH = 'enable_max_depth';
40+
41+
/**
42+
* How to track the current depth in the context.
43+
*/
44+
private const DEPTH_KEY_PATTERN = 'depth_%s::%s';
45+
46+
/**
47+
* While denormalizing, we can verify that types match.
48+
*
49+
* You can disable this by setting this flag to true.
50+
*/
51+
public const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';
52+
3953
const SKIP_NULL_VALUES = 'skip_null_values';
40-
const MAX_DEPTH_HANDLER = 'max_depth_handler';
41-
const EXCLUDE_FROM_CACHE_KEY = 'exclude_from_cache_key';
42-
const DEEP_OBJECT_TO_POPULATE = 'deep_object_to_populate';
54+
55+
public const MAX_DEPTH_HANDLER = 'max_depth_handler';
56+
57+
/**
58+
* Specify which context key are not relevant to determine which attributes
59+
* of an object to (de)normalize.
60+
*/
61+
public const EXCLUDE_FROM_CACHE_KEY = 'exclude_from_cache_key';
62+
63+
/**
64+
* Flag to tell the denormalizer to also populate existing objects on
65+
* attributes of the main object.
66+
*
67+
* Setting this to true is only useful if you also specify the root object
68+
* in OBJECT_TO_POPULATE.
69+
*/
70+
public const DEEP_OBJECT_TO_POPULATE = 'deep_object_to_populate';
4371

4472
private $propertyTypeExtractor;
4573
private $typesCache = [];

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/CircularReferenceTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/CircularReferenceTestTrait.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
44

55
use Symfony\Component\Serializer\Exception\CircularReferenceException;
6-
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
76
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8-
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
97

108
/**
119
* Test AbstractNormalizer::CIRCULAR_REFERENCE_LIMIT and AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER.

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectConstructor.php renamed to ‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsObject.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsObject.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
44

55

6-
class ObjectConstructor
6+
class ConstructorArgumentsObject
77
{
88
private $foo;
99
private $bar;

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public function testDefaultConstructorArguments()
1919

2020
$denormalizer = $this->getDenormalizerForConstructArguments();
2121

22-
$result = $denormalizer->denormalize($data, ObjectConstructor::class, 'json', [
22+
$result = $denormalizer->denormalize($data, ConstructorArgumentsObject::class, 'json', [
2323
AbstractNormalizer::DEFAULT_CONSTRUCTOR_ARGUMENTS => [
24-
ObjectConstructor::class => ['foo' => '', 'bar' => '', 'baz' => null],
24+
ConstructorArgumentsObject::class => ['foo' => '', 'bar' => '', 'baz' => null],
2525
],
2626
]);
2727

28-
$this->assertEquals(new ObjectConstructor(10, '', null), $result);
28+
$this->assertEquals(new ConstructorArgumentsObject(10, '', null), $result);
2929
}
3030

3131
public function testMetadataAwareNameConvertorWithNotSerializedConstructorParameter()
@@ -56,7 +56,7 @@ public function testConstructorWithMissingData()
5656
$normalizer = $this->getDenormalizerForConstructArguments();
5757

5858
$this->expectException(MissingConstructorArgumentsException::class);
59-
$this->expectExceptionMessage('Cannot create an instance of '.ObjectConstructor::class.' from serialized data because its constructor requires parameter "bar" to be present.');
60-
$normalizer->denormalize($data, ObjectConstructor::class);
59+
$this->expectExceptionMessage('Cannot create an instance of '.ConstructorArgumentsObject::class.' from serialized data because its constructor requires parameter "bar" to be present.');
60+
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
6161
}
6262
}

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/MaxDepthTestTrait.php
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
44

5-
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
65
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
76
use Symfony\Component\Serializer\Tests\Fixtures\MaxDepthDummy;
87

8+
/**
9+
* Covers AbstractObjectNormalizer::ENABLE_MAX_DEPTH and AbstractObjectNormalizer::MAX_DEPTH_HANDLER.
10+
*/
911
trait MaxDepthTestTrait
1012
{
1113
abstract protected function getNormalizerForMaxDepth(): NormalizerInterface;
@@ -29,7 +31,7 @@ public function testMaxDepth()
2931
$level4->bar = 'level4';
3032
$level3->child = $level4;
3133

32-
$result = $normalizer->normalize($level1, null, [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]);
34+
$result = $normalizer->normalize($level1, null, ['enable_max_depth' => true]);
3335

3436
$expected = [
3537
'bar' => 'level1',
@@ -76,7 +78,7 @@ public function testMaxDepthHandler()
7678
$this->assertInstanceOf(MaxDepthDummy::class, $parentObject);
7779
$this->assertSame('bar', $attributeName);
7880
$this->assertSame('test', $format);
79-
$this->assertArrayHasKey(AbstractObjectNormalizer::ENABLE_MAX_DEPTH, $context);
81+
$this->assertArrayHasKey('enable_max_depth', $context);
8082

8183
$handlerCalled = true;
8284

@@ -85,8 +87,8 @@ public function testMaxDepthHandler()
8587

8688
$normalizer = $this->getNormalizerForMaxDepth();
8789
$normalizer->normalize($level1, 'test', [
88-
AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
89-
AbstractObjectNormalizer::MAX_DEPTH_HANDLER => $maxDepthHandler
90+
'enable_max_depth' => true,
91+
'max_depth_handler' => $maxDepthHandler
9092
]);
9193

9294
$this->assertTrue($handlerCalled);

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectToPopulateTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectToPopulateTestTrait.php
+7-8Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
55

6-
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
76
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
87
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
98
use Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy;
@@ -14,28 +13,28 @@ abstract protected function getDenormalizerForObjectToPopulate(): DenormalizerIn
1413

1514
public function testObjectToPopulate()
1615
{
17-
$dummy = new GetSetDummy();
18-
$dummy->setFoo('foo');
16+
$dummy = new ObjectDummy();
17+
$dummy->bar = 'bar';
1918

2019
$denormalizer = $this->getDenormalizerForObjectToPopulate();
2120

2221
$obj = $denormalizer->denormalize(
23-
['bar' => 'bar'],
24-
GetSetDummy::class,
22+
['foo' => 'foo'],
23+
ObjectDummy::class,
2524
null,
26-
[AbstractNormalizer::OBJECT_TO_POPULATE => $dummy]
25+
['object_to_populate' => $dummy]
2726
);
2827

2928
$this->assertEquals($dummy, $obj);
3029
$this->assertEquals('foo', $obj->getFoo());
31-
$this->assertEquals('bar', $obj->getBar());
30+
$this->assertEquals('bar', $obj->bar);
3231
}
3332

3433
public function testObjectToPopulateWithProxy()
3534
{
3635
$proxyDummy = new ProxyDummy();
3736

38-
$context = [AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy];
37+
$context = ['object_to_populate' => $proxyDummy];
3938

4039
$denormalizer = $this->getDenormalizerForObjectToPopulate();
4140
$denormalizer->denormalize(['foo' => 'bar'], ToBeProxyfiedDummy::class, null, $context);

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/JsonNumber.php renamed to ‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/TypeEnforcementNumberObject.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/TypeEnforcementNumberObject.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
44

5-
class JsonNumber
5+
class TypeEnforcementNumberObject
66
{
77
/**
88
* @var float

‎src/Symfony/Component/Serializer/Tests/Normalizer/Features/TypeEnforcementTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/Features/TypeEnforcementTestTrait.php
+12-9Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@
22

33
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
44

5+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
56
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
67
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
78

9+
/**
10+
* Test type mismatches with a denormalizer that is aware of types.
11+
* Covers AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT.
12+
*/
813
trait TypeEnforcementTestTrait
914
{
1015
abstract protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface;
1116

12-
/**
13-
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
14-
* @expectedExceptionMessage The type of the "date" attribute for class "Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectOuter" must be one of "DateTimeInterface" ("string" given).
15-
*/
1617
public function testRejectInvalidType()
1718
{
1819
$denormalizer = $this->getDenormalizerForTypeEnforcement();
20+
21+
$this->expectException(UnexpectedValueException::class);
22+
$this->expectExceptionMessage('The type of the "date" attribute for class "'.ObjectOuter::class.'" must be one of "DateTimeInterface" ("string" given).');
1923
$denormalizer->denormalize(['date' => 'foo'], ObjectOuter::class);
2024
}
2125

22-
/**
23-
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
24-
* @expectedExceptionMessage The type of the key "a" must be "int" ("string" given).
25-
*/
2626
public function testRejectInvalidKey()
2727
{
2828
$denormalizer = $this->getDenormalizerForTypeEnforcement();
29+
30+
$this->expectException(UnexpectedValueException::class);
31+
$this->expectExceptionMessage('The type of the key "a" must be "int" ("string" given).');
2932
$denormalizer->denormalize(['inners' => ['a' => ['foo' => 1]]], ObjectOuter::class);
3033
}
3134

@@ -35,7 +38,7 @@ public function testDoNotRejectInvalidTypeOnDisableTypeEnforcementContextOption(
3538

3639
$this->assertSame('foo', $denormalizer->denormalize(
3740
['number' => 'foo'],
38-
JsonNumber::class,
41+
TypeEnforcementNumberObject::class,
3942
null,
4043
[AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true]
4144
)->number);

‎src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use Symfony\Component\Serializer\Tests\Normalizer\Features\GroupsTestTrait;
4242
use Symfony\Component\Serializer\Tests\Normalizer\Features\IgnoredAttributesTestTrait;
4343
use Symfony\Component\Serializer\Tests\Normalizer\Features\MaxDepthTestTrait;
44-
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectConstructor;
4544
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy;
4645
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectToPopulateTestTrait;
4746
use Symfony\Component\Serializer\Tests\Normalizer\Features\TypeEnforcementTestTrait;
@@ -296,8 +295,7 @@ protected function getDenormalizerForTypeEnforcement(): DenormalizerInterface
296295
{
297296
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
298297
$normalizer = new ObjectNormalizer(null, null, null, $extractor);
299-
$serializer = new Serializer([new ArrayDenormalizer(), $normalizer]);
300-
$normalizer->setSerializer($serializer);
298+
new Serializer([new ArrayDenormalizer(), $normalizer]);
301299

302300
return $normalizer;
303301
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.