Skip to content

Commit cba20ed

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: (48 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 synchronize 2.8 and 3.0 upgrade files 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 Fixed the form profiler when using long form types [PropertyInfo] PhpDocExtractor: Fix a notice when the property doesn't exist Remove useless duplicated tests [FrameworkBundle] Optimize framework extension tests synchronize 2.7 and 3.0 upgrade files ...
2 parents 621c02e + 913dc7e commit cba20ed

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
@@ -25,6 +25,10 @@
2525
*/
2626
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
2727
{
28+
const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
29+
const OBJECT_TO_POPULATE = 'object_to_populate';
30+
const GROUPS = 'groups';
31+
2832
/**
2933
* @var int
3034
*/
@@ -146,16 +150,16 @@ protected function isCircularReference($object, &$context)
146150
{
147151
$objectHash = spl_object_hash($object);
148152

149-
if (isset($context['circular_reference_limit'][$objectHash])) {
150-
if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
151-
unset($context['circular_reference_limit'][$objectHash]);
153+
if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) {
154+
if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) {
155+
unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]);
152156

153157
return true;
154158
}
155159

156-
++$context['circular_reference_limit'][$objectHash];
160+
++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
157161
} else {
158-
$context['circular_reference_limit'][$objectHash] = 1;
162+
$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
159163
}
160164

161165
return false;
@@ -193,13 +197,13 @@ protected function handleCircularReference($object)
193197
*/
194198
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
195199
{
196-
if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
200+
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
197201
return false;
198202
}
199203

200204
$allowedAttributes = array();
201205
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
202-
if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
206+
if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS]))) {
203207
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
204208
}
205209
}
@@ -241,12 +245,12 @@ protected function prepareForDenormalization($data)
241245
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
242246
{
243247
if (
244-
isset($context['object_to_populate']) &&
245-
is_object($context['object_to_populate']) &&
246-
$context['object_to_populate'] instanceof $class
248+
isset($context[static::OBJECT_TO_POPULATE]) &&
249+
is_object($context[static::OBJECT_TO_POPULATE]) &&
250+
$context[static::OBJECT_TO_POPULATE] instanceof $class
247251
) {
248-
$object = $context['object_to_populate'];
249-
unset($context['object_to_populate']);
252+
$object = $context[static::OBJECT_TO_POPULATE];
253+
unset($context[static::OBJECT_TO_POPULATE]);
250254

251255
return $object;
252256
}

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
@@ -200,7 +200,7 @@ public function testGroupsNormalize()
200200

201201
$this->assertEquals(array(
202202
'bar' => 'bar',
203-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
203+
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('c'))));
204204

205205
$this->assertEquals(array(
206206
'symfony' => 'symfony',
@@ -209,7 +209,7 @@ public function testGroupsNormalize()
209209
'bar' => 'bar',
210210
'kevin' => 'kevin',
211211
'coopTilleuls' => 'coopTilleuls',
212-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
212+
), $this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('a', 'c'))));
213213
}
214214

215215
public function testGroupsDenormalize()
@@ -227,7 +227,7 @@ public function testGroupsDenormalize()
227227
$toNormalize,
228228
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
229229
null,
230-
array('groups' => array('a'))
230+
array(GetSetMethodNormalizer::GROUPS => array('a'))
231231
);
232232
$this->assertEquals($obj, $normalized);
233233

@@ -237,7 +237,7 @@ public function testGroupsDenormalize()
237237
$toNormalize,
238238
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
239239
null,
240-
array('groups' => array('a', 'b'))
240+
array(GetSetMethodNormalizer::GROUPS => array('a', 'b'))
241241
);
242242
$this->assertEquals($obj, $normalized);
243243
}
@@ -259,7 +259,7 @@ public function testGroupsNormalizeWithNameConverter()
259259
'foo_bar' => '@dunglas',
260260
'symfony' => '@coopTilleuls',
261261
),
262-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
262+
$this->normalizer->normalize($obj, null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
263263
);
264264
}
265265

@@ -280,7 +280,7 @@ public function testGroupsDenormalizeWithNameConverter()
280280
'foo_bar' => '@dunglas',
281281
'symfony' => '@coopTilleuls',
282282
'coop_tilleuls' => 'les-tilleuls.coop',
283-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
283+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(GetSetMethodNormalizer::GROUPS => array('name_converter')))
284284
);
285285
}
286286

@@ -456,7 +456,7 @@ public function testObjectToPopulate()
456456
array('bar' => 'bar'),
457457
__NAMESPACE__.'\GetSetDummy',
458458
null,
459-
array('object_to_populate' => $dummy)
459+
array(GetSetMethodNormalizer::OBJECT_TO_POPULATE => $dummy)
460460
);
461461

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

Tests/Normalizer/ObjectNormalizerTest.php

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

170170
$this->assertEquals(array(
171171
'bar' => 'bar',
172-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
172+
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('c'))));
173173

174174
$this->assertEquals(array(
175175
'symfony' => 'symfony',
@@ -178,7 +178,7 @@ public function testGroupsNormalize()
178178
'bar' => 'bar',
179179
'kevin' => 'kevin',
180180
'coopTilleuls' => 'coopTilleuls',
181-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
181+
), $this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('a', 'c'))));
182182
}
183183

184184
public function testGroupsDenormalize()
@@ -196,7 +196,7 @@ public function testGroupsDenormalize()
196196
$toNormalize,
197197
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
198198
null,
199-
array('groups' => array('a'))
199+
array(ObjectNormalizer::GROUPS => array('a'))
200200
);
201201
$this->assertEquals($obj, $normalized);
202202

@@ -206,7 +206,7 @@ public function testGroupsDenormalize()
206206
$toNormalize,
207207
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
208208
null,
209-
array('groups' => array('a', 'b'))
209+
array(ObjectNormalizer::GROUPS => array('a', 'b'))
210210
);
211211
$this->assertEquals($obj, $normalized);
212212
}
@@ -240,7 +240,7 @@ public function testGroupsNormalizeWithNameConverter()
240240
'foo_bar' => '@dunglas',
241241
'symfony' => '@coopTilleuls',
242242
),
243-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
243+
$this->normalizer->normalize($obj, null, array(ObjectNormalizer::GROUPS => array('name_converter')))
244244
);
245245
}
246246

@@ -261,7 +261,7 @@ public function testGroupsDenormalizeWithNameConverter()
261261
'foo_bar' => '@dunglas',
262262
'symfony' => '@coopTilleuls',
263263
'coop_tilleuls' => 'les-tilleuls.coop',
264-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
264+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(ObjectNormalizer::GROUPS => array('name_converter')))
265265
);
266266
}
267267

Tests/Normalizer/PropertyNormalizerTest.php

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

144144
$this->assertEquals(array(
145145
'bar' => 'bar',
146-
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
146+
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('c'))));
147147

148148
// The PropertyNormalizer is not able to hydrate properties from parent classes
149149
$this->assertEquals(array(
150150
'symfony' => 'symfony',
151151
'foo' => 'foo',
152152
'fooBar' => 'fooBar',
153153
'bar' => 'bar',
154-
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
154+
), $this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('a', 'c'))));
155155
}
156156

157157
public function testGroupsDenormalize()
@@ -169,7 +169,7 @@ public function testGroupsDenormalize()
169169
$toNormalize,
170170
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
171171
null,
172-
array('groups' => array('a'))
172+
array(PropertyNormalizer::GROUPS => array('a'))
173173
);
174174
$this->assertEquals($obj, $normalized);
175175

@@ -179,7 +179,7 @@ public function testGroupsDenormalize()
179179
$toNormalize,
180180
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
181181
null,
182-
array('groups' => array('a', 'b'))
182+
array(PropertyNormalizer::GROUPS => array('a', 'b'))
183183
);
184184
$this->assertEquals($obj, $normalized);
185185
}
@@ -201,7 +201,7 @@ public function testGroupsNormalizeWithNameConverter()
201201
'foo_bar' => '@dunglas',
202202
'symfony' => '@coopTilleuls',
203203
),
204-
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
204+
$this->normalizer->normalize($obj, null, array(PropertyNormalizer::GROUPS => array('name_converter')))
205205
);
206206
}
207207

@@ -222,7 +222,7 @@ public function testGroupsDenormalizeWithNameConverter()
222222
'foo_bar' => '@dunglas',
223223
'symfony' => '@coopTilleuls',
224224
'coop_tilleuls' => 'les-tilleuls.coop',
225-
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
225+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array(PropertyNormalizer::GROUPS => array('name_converter')))
226226
);
227227
}
228228

0 commit comments

Comments
 (0)