8000 minor #17422 [Serializer] Introduce constants for context keys (dunglas) · symfony/symfony@d55f73f · GitHub
[go: up one dir, main page]

Skip to content

Commit d55f73f

Browse files
committed
minor #17422 [Serializer] Introduce constants for context keys (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Introduce constants for context keys | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Introduces constants for keys. Follow up @xabbuh's https://github.com/symfony/symfony/pull/17113/files#r48598904 Commits ------- c56c7bf [Serializer] Introduce constants for context keys
2 parents 3a19369 + c56c7bf commit d55f73f

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
*/
2828
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
2929
{
30+
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
31+
const OBJECT_TO_POPULATE = 'object_to_populate';
32+
const GROUPS = 'groups';
33+
3034
/**
3135
* @var int
3236
*/
@@ -185,16 +189,16 @@ protected function isCircularReference($object, &$context)
185189
{
186190
$objectHash = spl_object_hash($object);
187191

188-
if (isset($context['circular_reference_limit'][$objectHash])) {
189-
if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
190-
unset($context['circular_reference_limit'][$objectHash]);
192+
if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) {
193+
if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) {
194+
unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]);
191195

192196
return true;
193197
}
194198

195-
++$context['circular_reference_limit'][$objectHash];
199+
++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
196200
} else {
197-
$context['circular_reference_limit'][$objectHash] = 1;
201+
$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
198202
}
199203

200204
return false;
@@ -248,13 +252,13 @@ protected function formatAttribute($attributeName)
248252
*/
249253
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
250254
{
251-
if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
255+
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
252256
return false;
253257
}
254258

255259
$allowedAttributes = array();
256260
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
257-
if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
261+
if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS]))) {
258262
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
259263
}
260264
}
@@ -296,12 +300,12 @@ protected function prepareForDenormalization($data)
296300
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
297301
{
298302
if (
299-
isset($context['object_to_populate']) &&
300-
is_object($context['object_to_populate']) &&
301-
$context['object_to_populate'] instanceof $class
303+
isset($context[static::OBJECT_TO_POPULATE]) &&
304+
is_object($context[static::OBJECT_TO_POPULATE]) &&
305+
$context[static::OBJECT_TO_POPULATE] instanceof $class
302306
) {
303-
$object = $context['object_to_populate'];
304-
unset($context['object_to_populate']);
307+
$object = $context[static::OBJECT_TO_POPULATE];
308+
unset($context[static::OBJECT_TO_POPULATE]);
305309

306310
return $object;
307311
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
66
use Symfony\Component\Serializer\Mapping\ClassMetadata;
77
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
8+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
89
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
910
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
1011
use Symfony\Component\Serializer\Tests\Fixtures\ProxyDummy;
@@ -55,10 +56,10 @@ public function testGetAllowedAttributesAsString()
5556

5657
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
5758

58-
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), true);
59+
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), true);
5960
$this->assertEquals(array('a2', 'a4'), $result);
6061

61-
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), true);
62+
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), true);
6263
$this->assertEquals(array('a3', 'a4'), $result);
6364
}
6465

@@ -84,18 +85,18 @@ public function testGetAllowedAttributesAsObjects()
8485

8586
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
8687

87-
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('test')), false);
88+
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
8889
$this->assertEquals(array($a2, $a4), $result);
8990

90-
$result = $this->normalizer->getAllowedAttributes('c', array('groups' => array('other')), false);
91+
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
9192
$this->assertEquals(array($a3, $a4), $result);
9293
}
9394

9495
public function testObjectToPopulateWithProxy()
9596
{
9697
$proxyDummy = new ProxyDummy();
9798

98-
$context = array('object_to_populate' => $proxyDummy);
99+
$context = array(AbstractNormalizer::OBJECT_TO_POPULATE => $proxyDummy);
99100

100101
$normalizer = new ObjectNormalizer();
101102
$normalizer->denormalize(array('foo' => 'bar'), 'Symfony\Component\Serializer\Tests\Fixtures\ToBeProxyfiedDummy', null, $context);

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public function testGroupsNormalize()
277277

278278
$this->assertEquals(array(
279279
'bar' => 'bar',
280-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
280+
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('c'))));
281281

282282
$this->assertEquals(array(
283283
'symfony' => 'symfony',
@@ -286,7 +286,7 @@ public function testGroupsNormalize()
286286
'bar' => 'bar',
287287
'kevin' => 'kevin',
288288
'coopTilleuls' => 'coopTilleuls',
289-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
289+
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('a', 'c'))));
290290
}
291291

292292
public function testGroupsDenormalize()
@@ -304,7 +304,7 @@ public function testGroupsDenormalize()
304304
$toNormalize,
305305
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
306306
null,
307-
array('groups' => array('a'))
307+
array(GetSetMethodNormalizer::GROUPS => array('a'))
308308
);
309309
$this->assertEquals($obj, $normalized);
310310

@@ -314,7 +314,7 @@ public function testGroupsDenormalize()
314314
$toNormalize,
315315
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
316316
null,
317-
array('groups' => array('a', 'b'))
317+
array(GetSetMethodNormalizer::GROUPS => array('a', 'b'))
318318
);
319319
$this->assertEquals($obj, $normalized);
320320
}
@@ -336,7 +336,7 @@ public function testGroupsNormalizeWithNameConverter()
336336
'foo_bar' => '@dunglas',
337337
'symfony' => '@coopTilleuls',
338338
),
339-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
339+
$this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
340340
);
341341
}
342342

@@ -357,7 +357,7 @@ public function testGroupsDenormalizeWithNameConverter()
357357
'foo_bar' => '@dunglas',
358358
'symfony' => '@coopTilleuls',
359359
'coop_tilleuls' => 'les-tilleuls.coop',
360-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
360+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
361361
);
362362
}
363363

@@ -533,7 +533,7 @@ public function testObjectToPopulate()
533533
array('bar' => 'bar'),
534534
__NAMESPACE__.'\GetSetDummy',
535535
null,
536-
array('object_to_populate' => $dummy)
536+
array(GetSetMethodNormalizer::OBJECT_TO_POPULATE => $dummy)
537537
);
538538

539539
$this->assertEquals($dummy, $obj);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function testGroupsNormalize()
197197

198198
$this->assertEquals(array(
199199
'bar' => 'bar',
200-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
200+
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('c'))));
201201

202202
$this->assertEquals(array(
203203
'symfony' => 'symfony',
@@ -206,7 +206,7 @@ public function testGroupsNormalize()
206206
'bar' => 'bar',
207207
'kevin' => 'kevin',
208208
'coopTilleuls' => 'coopTilleuls',
209-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
209+
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('a', 'c'))));
210210
}
211211

212212
public function testGroupsDenormalize()
@@ -224,7 +224,7 @@ public function testGroupsDenormalize()
224224
$toNormalize,
225225
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
226226
null,
227-
array('groups' => array('a'))
227+
array(ObjectNormalizer::GROUPS => array('a'))
228228
);
229229
$this->assertEquals($obj, $normalized);
230230

@@ -234,7 +234,7 @@ public function testGroupsDenormalize()
234234
$toNormalize,
235235
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
236236
null,
237-
array('groups' => array('a', 'b'))
237+
array(ObjectNormalizer::GROUPS => array('a', 'b'))
238238
);
239239
$this->assertEquals($obj, $normalized);
240240
}
@@ -256,7 +256,7 @@ public function testGroupsNormalizeWithNameConverter()
256256
'foo_bar' => '@dunglas',
257257
'symfony' => '@coopTilleuls',
258258
),
259-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
259+
$this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('name_converter')))
260260
);
261261
}
262262

@@ -277,7 +277,7 @@ public function testGroupsDenormalizeWithNameConverter()
277277
'foo_bar' => '@dunglas',
278278
'symfony' => '@coopTilleuls',
279279
'coop_tilleuls' => 'les-tilleuls.coop',
280-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
280+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(ObjectNormalizer::GROUPS => array('name_converter')))
281281
);
282282
}
283283

src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ public function testGroupsNormalize()
214214

215215
$this->assertEquals(array(
216216
'bar' => 'bar',
217-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
217+
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));
218218

219219
// The PropertyNormalizer is not able to hydrate properties from parent classes
220220
$this->assertEquals(array(
221221
'symfony' => 'symfony',
222222
'foo' => 'foo',
223223
'fooBar' => 'fooBar',
224224
'bar' => 'bar',
225-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
225+
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
226226
}
227227

228228
public function testGroupsDenormalize()
@@ -240,7 +240,7 @@ public function testGroupsDenormalize()
240240
$toNormalize,
241241
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
242242
null,
243-
array('groups' => array('a'))
243+
array(PropertyNormalizer::GROUPS => array('a'))
244244
);
245245
$this->assertEquals($obj, $normalized);
246246

@@ -250,7 +250,7 @@ public function testGroupsDenormalize()
250250
$toNormalize,
251251
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
252252
null,
253-
array('groups' => array('a', 'b'))
253+
array(PropertyNormalizer::GROUPS => array('a', 'b'))
254254
);
255255
$this->assertEquals($obj, $normalized);
256256
}
@@ -272,7 +272,7 @@ public function testGroupsNormalizeWithNameConverter()
272272
'foo_bar' => '@dunglas',
273273
'symfony' => '@coopTilleuls',
274274
),
275-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
275+
$this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('name_converter')))
276276
);
277277
}
278278

@@ -293,7 +293,7 @@ public function testGroupsDenormalizeWithNameConverter()
293293
'foo_bar' => '@dunglas',
294294
'symfony' => '@coopTilleuls',
295295
'coop_tilleuls' => 'les-tilleuls.coop',
296-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
296+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(PropertyNormalizer::GROUPS => array('name_converter')))
297297
);
298298
}
299299

0 commit comments

Comments
 (0)
0