Skip to content

Commit 913dc7e

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: (28 commits) [Process] Use stream based storage to avoid memory issues Fix upgrade guides concerning erroneous removal of assets helper [Process] Remove a misleading comment Fix markdown typo ChooseBaseUrl should return an index [Form] ChoiceType: Fix a notice when 'choices' normalizer is replaced Improve the phpdoc of SplFileInfo methods [Process] Use stream based storage to avoid memory issues [FrameworkBundle] Don't log twice with the error handler Remove useless is_object condition [Process] Fix typo, no arguments needed anymore [Serializer] Introduce constants for context keys Fixed the documentation of VoterInterface::supportsAttribute Fixed Bootstrap form theme form "reset" buttons Remove useless duplicated tests [FrameworkBundle] Optimize framework extension tests synchronize 2.7 and 3.0 upgrade files fix merge 2.3 into 2.7 for SecureRandom dependency Use is_subclass_of instead of reflection Use is_subclass_of instead of Reflection when possible ...
2 parents 60d6ea5 + 101683b commit 913dc7e

File tree

7 files changed

+49
-78
lines changed

7 files changed

+49
-78
lines changed

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
}

Normalizer/CustomNormalizer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public function supportsDenormalization($data, $type, $format = null)
6363
return false;
6464
}
6565

66-
$class = new \ReflectionClass($type);
67-
68-
return $class->isSubclassOf('Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
66+
return is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
6967
}
7068
}

Serializer.php

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,24 @@ public function normalize($data, $format = null, array $context = array())
127127
if (null === $data || is_scalar($data)) {
128128
return $data;
129129
}
130-
if (is_object($data) && $this->supportsNormalization($data, $format)) {
131-
return $this->normalizeObject($data, $format, $context);
132-
}
133-
if ($data instanceof \Traversable) {
130+
131+
if (is_array($data) || $data instanceof \Traversable) {
134132
$normalized = array();
135133
foreach ($data as $key => $val) {
136134
$normalized[$key] = $this->normalize($val, $format, $context);
137135
}
138136

139137
return $normalized;
140138
}
139+
141140
if (is_object($data)) {
142-
return $this->normalizeObject($data, $format, $context);
143-
}
144-
if (is_array($data)) {
145-
foreach ($data as $key => $val) {
146-
$data[$key] = $this->normalize($val, $format, $context);
141+
if (!$this->normalizers) {
142+
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
147143
}
148144

149-
return $data;
145+
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
150146
}
147+
151148
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
152149
}
153150

@@ -185,10 +182,6 @@ public function supportsDenormalization($data, $type, $format = null)
185182
*/
186183
private function getNormalizer($data, $format)
187184
{
188-
if ($isObject = is_object($data)) {
189-
$class = get_class($data);
190-
}
191-
192185
foreach ($this->normalizers as $normalizer) {
193186
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
194187
return $normalizer;
@@ -230,31 +223,6 @@ final public function decode($data, $format, array $context = array())
230223
return $this->decoder->decode($data, $format, $context);
231224
}
232225

233-
/**
234-
* Normalizes an object into a set of arrays/scalars.
235-
*
236-
* @param object $object object to normalize
237-
* @param string $format format name, present to give the option to normalizers to act differently based on formats
238-
* @param array $context The context data for this particular normalization
239-
*
240-
* @return array|string|bool|int|float|null
241-
*
242-
* @throws LogicException
243-
* @throws UnexpectedValueException
244-
*/
245-
private function normalizeObject($object, $format, array $context = array())
246-
{
247-
if (!$this->normalizers) {
248-
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
249-
}
250-
251-
if ($normalizer = $this->getNormalizer($object, $format)) {
252-
return $normalizer->normalize($object, $format, $context);
253-
}
254-
255-
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object)));
256-
}
257-
258226
/**
259227
* Denormalizes data back into an object of the given class.
260228
*

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);

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);

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
}
@@ -268,7 +268,7 @@ public function testGroupsNormalizeWithNameConverter()
268268
'foo_bar' => '@dunglas',
269269
'symfony' => '@coopTilleuls',
270270
),
271-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
271+
$this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('name_converter')))
272272
);
273273
}
274274

@@ -289,7 +289,7 @@ public function testGroupsDenormalizeWithNameConverter()
289289
'foo_bar' => '@dunglas',
290290
'symfony' => '@coopTilleuls',
291291
'coop_tilleuls' => 'les-tilleuls.coop',
292-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
292+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(ObjectNormalizer::GROUPS => array('name_converter')))
293293
);
294294
}
295295

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)