Skip to content

Commit e807a23

Browse files
Merge branch '2.7' into 2.8
* 2.7: (70 commits) [travis] Use container-based infrastructure [HttpKernel] use ConfigCache::getPath() method when it exists [PropertyAccess] Fix setting public property on a class having a magic getter [Routing] Display file which contain deprecated option ContainerInterface: unused exception dropped bumped Symfony version to 2.6.8 updated VERSION for 2.6.7 updated CHANGELOG for 2.6.7 bumped Symfony version to 2.3.29 updated VERSION for 2.3.28 update CONTRIBUTORS for 2.3.28 updated CHANGELOG for 2.3.28 [Debug] Fixed ClassNotFoundFatalErrorHandlerTest [SecurityBundle] use access decision constants in config [SecurityBundle] use session auth constants in config PhpDoc fix in AbstractRememberMeServices [Filesystem] Simplified an if statement [SecurityBundle] Use Enum Nodes Instead Of Scalar [Debug 2.3] Fix test for PHP7 [HttpKernel] Check if "symfony/proxy-manager-bridge" package is installed ... Conflicts: src/Symfony/Bundle/DebugBundle/composer.json src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php src/Symfony/Component/Form/README.md src/Symfony/Component/Intl/README.md src/Symfony/Component/Security/README.md src/Symfony/Component/Translation/Loader/CsvFileLoader.php src/Symfony/Component/Translation/Loader/IniFileLoader.php src/Symfony/Component/Translation/Loader/MoFileLoader.php src/Symfony/Component/Translation/Loader/PhpFileLoader.php src/Symfony/Component/Translation/Loader/PoFileLoader.php src/Symfony/Component/Translation/Loader/YamlFileLoader.php src/Symfony/Component/Translation/README.md src/Symfony/Component/Translation/Translator.php src/Symfony/Component/Validator/README.md
2 parents 0e69dd2 + 122c2b0 commit e807a23

11 files changed

+207
-25
lines changed

Mapping/Loader/AnnotationLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
5454
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
5555
}
5656

57-
if ($property->getDeclaringClass()->name == $className) {
57+
if ($property->getDeclaringClass()->name === $className) {
5858
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
5959
if ($groups instanceof Groups) {
6060
foreach ($groups->getGroups() as $group) {
@@ -68,10 +68,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
6868
}
6969

7070
foreach ($reflectionClass->getMethods() as $method) {
71-
if ($method->getDeclaringClass()->name == $className) {
71+
if ($method->getDeclaringClass()->name === $className) {
7272
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
7373
if ($groups instanceof Groups) {
74-
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
74+
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
7575
$attributeName = lcfirst($matches[2]);
7676

7777
if (isset($attributesMetadata[$attributeName])) {
@@ -85,7 +85,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
8585
$attributeMetadata->addGroup($group);
8686
}
8787
} else {
88-
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get" or "is".', $className, $method->name));
88+
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
8989
}
9090
}
9191

Normalizer/GetSetMethodNormalizer.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function normalize($object, $format = null, array $context = array())
5858
foreach ($reflectionMethods as $method) {
5959
if ($this->isGetMethod($method)) {
6060
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
61-
6261
if (in_array($attributeName, $this->ignoredAttributes)) {
6362
continue;
6463
}
@@ -104,14 +103,14 @@ public function denormalize($data, $class, $format = null, array $context = arra
104103
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
105104

106105
foreach ($normalizedData as $attribute => $value) {
106+
if ($this->nameConverter) {
107+
$attribute = $this->nameConverter->denormalize($attribute);
108+
}
109+
107110
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
108111
$ignored = in_array($attribute, $this->ignoredAttributes);
109112

110113
if ($allowed && !$ignored) {
111-
if ($this->nameConverter) {
112-
$attribute = $this->nameConverter->denormalize($attribute);
113-
}
114-
115114
$setter = 'set'.ucfirst($attribute);
116115

117116
if (method_exists($object, $setter)) {
@@ -128,7 +127,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
128127
*/
129128
public function supportsNormalization($data, $format = null)
130129
{
131-
return is_object($data) && $this->supports(get_class($data));
130+
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
132131
}
133132

134133
/**

Normalizer/ObjectNormalizer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4343
*/
4444
public function supportsNormalization($data, $format = null)
4545
{
46-
return is_object($data);
46+
return is_object($data) && !$data instanceof \Traversable;
4747
}
4848

4949
/**
@@ -141,14 +141,14 @@ public function denormalize($data, $class, $format = null, array $context = arra
141141
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
142142

143143
foreach ($normalizedData as $attribute => $value) {
144+
if ($this->nameConverter) {
145+
$attribute = $this->nameConverter->denormalize($attribute);
146+
}
147+
144148
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
145149
$ignored = in_array($attribute, $this->ignoredAttributes);
146150

147151
if ($allowed && !$ignored) {
148-
if ($this->nameConverter) {
149-
$attribute = $this->nameConverter->normalize($attribute);
150-
}
151-
152152
try {
153153
$this->propertyAccessor->setValue($object, $attribute, $value);
154154
} catch (NoSuchPropertyException $exception) {

Normalizer/PropertyNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
127127
*/
128128
public function supportsNormalization($data, $format = null)
129129
{
130-
return is_object($data) && $this->supports(get_class($data));
130+
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
131131
}
132132

133133
/**

Tests/Fixtures/GroupDummy.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2323
*/
2424
private $foo;
2525
/**
26-
* @Groups({"b", "c"})
26+
* @Groups({"b", "c", "name_converter"})
2727
*/
2828
protected $bar;
2929
private $fooBar;
3030
private $symfony;
3131

32+
/**
33+
* @Groups({"b"})
34+
*/
3235
public function setBar($bar)
3336
{
3437
$this->bar = $bar;
3538
}
3639

40+
/**
41+
* @Groups({"c"})
42+
*/
3743
public function getBar()
3844
{
3945
return $this->bar;
@@ -55,9 +61,9 @@ public function setFooBar($fooBar)
5561
}
5662

5763
/**
58-
* @Groups({"a", "b"})
64+
* @Groups({"a", "b", "name_converter"})
5965
*/
60-
public function getFooBar()
66+
public function isFooBar()
6167
{
6268
return $this->fooBar;
6369
}

Tests/Fixtures/GroupDummyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
interface GroupDummyInterface
2020
{
2121
/**
22-
* @Groups({"a"})
22+
* @Groups({"a", "name_converter"})
2323
*/
2424
public function getSymfony();
2525
}

Tests/Mapping/TestClassMetadataFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public static function createClassMetadata($withParent = false, $withInterface =
3030
$bar = new AttributeMetadata('bar');
3131
$bar->addGroup('b');
3232
$bar->addGroup('c');
33+
$bar->addGroup('name_converter');
3334
$expected->addAttributeMetadata($bar);
3435

3536
$fooBar = new AttributeMetadata('fooBar');
3637
$fooBar->addGroup('a');
3738
$fooBar->addGroup('b');
39+
$fooBar->addGroup('name_converter');
3840
$expected->addAttributeMetadata($fooBar);
3941

4042
$symfony = new AttributeMetadata('symfony');
@@ -53,6 +55,7 @@ public static function createClassMetadata($withParent = false, $withInterface =
5355

5456
if ($withInterface) {
5557
$symfony->addGroup('a');
58+
$symfony->addGroup('name_converter');
5659
}
5760

5861
// load reflection class so that the comparison passes

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1516
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
1617
use Symfony\Component\Serializer\Serializer;
1718
use Symfony\Component\Serializer\SerializerInterface;
@@ -115,6 +116,16 @@ public function testLegacyDenormalizeOnCamelCaseFormat()
115116
$this->assertEquals('camelCase', $obj->getCamelCase());
116117
}
117118

119+
public function testNameConverterSupport()
120+
{
121+
$this->normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
122+
$obj = $this->normalizer->denormalize(
123+
array('camel_case' => 'camelCase'),
124+
__NAMESPACE__.'\GetSetDummy'
125+
);
126+
$this->assertEquals('camelCase', $obj->getCamelCase());
127+
}
128+
118129
public function testDenormalizeNull()
119130
{
120131
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
@@ -262,6 +273,48 @@ public function testGroupsDenormalize()
262273
$this->assertEquals($obj, $normalized);
263274
}
264275

276+
public function testGroupsNormalizeWithNameConverter()
277+
{
278+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
279+
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
280+
$this->normalizer->setSerializer($this->serializer);
281+
282+
$obj = new GroupDummy();
283+
$obj->setFooBar('@dunglas');
284+
$obj->setSymfony('@coopTilleuls');
285+
$obj->setCoopTilleuls('les-tilleuls.coop');
286+
287+
$this->assertEquals(
288+
array(
289+
'bar' => null,
290+
'foo_bar' => '@dunglas',
291+
'symfony' => '@coopTilleuls',
292+
),
293+
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
294+
);
295+
}
296+
297+
public function testGroupsDenormalizeWithNameConverter()
298+
{
299+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
300+
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
301+
$this->normalizer->setSerializer($this->serializer);
302+
303+
$obj = new GroupDummy();
304+
$obj->setFooBar('@dunglas');
305+
$obj->setSymfony('@coopTilleuls');
306+
307+
$this->assertEquals(
308+
$obj,
309+
$this->normalizer->denormalize(array(
310+
'bar' => null,
311+
'foo_bar' => '@dunglas',
312+
'symfony' => '@coopTilleuls',
313+
'coop_tilleuls' => 'les-tilleuls.coop',
314+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
315+
);
316+
}
317+
265318
/**
266319
* @dataProvider provideCallbacks
267320
*/
@@ -449,6 +502,11 @@ public function testDenormalizeNonExistingAttribute()
449502
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
450503
);
451504
}
505+
506+
public function testNoTraversableSupport()
507+
{
508+
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
509+
}
452510
}
453511

454512
class GetSetDummy

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\Normalizer;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
1516
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1617
use Symfony\Component\Serializer\Serializer;
1718
use Symfony\Component\Serializer\SerializerInterface;
@@ -111,6 +112,16 @@ public function testLegacyDenormalizeOnCamelCaseFormat()
111112
$this->assertEquals('camelCase', $obj->getCamelCase());
112113
}
113114

115+
public function testNameConverterSupport()
116+
{
117+
$this->normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
118+
$obj = $this->normalizer->denormalize(
119+
array('camel_case' => 'camelCase'),
120+
__NAMESPACE__.'\ObjectDummy'
121+
);
122+
$this->assertEquals('camelCase', $obj->getCamelCase());
123+
}
124+
114125
public function testDenormalizeNull()
115126
{
116127
$this->assertEquals(new ObjectDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\ObjectDummy'));
@@ -206,6 +217,48 @@ public function testGroupsDenormalize()
206217
$this->assertEquals($obj, $normalized);
207218
}
208219

220+
public function testGroupsNormalizeWithNameConverter()
221+
{
222+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
223+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
224+
$this->normalizer->setSerializer($this->serializer);
225+
226+
$obj = new GroupDummy();
227+
$obj->setFooBar('@dunglas');
228+
$obj->setSymfony('@coopTilleuls');
229+
$obj->setCoopTilleuls('les-tilleuls.coop');
230+
231+
$this->assertEquals(
232+
array(
233+
'bar' => null,
234+
'foo_bar' => '@dunglas',
235+
'symfony' => '@coopTilleuls',
236+
),
237+
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
238+
);
239+
}
240+
241+
public function testGroupsDenormalizeWithNameConverter()
242+
{
243+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
244+
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
245+
$this->normalizer->setSerializer($this->serializer);
246+
247+
$obj = new GroupDummy();
248+
$obj->setFooBar('@dunglas');
249+
$obj->setSymfony('@coopTilleuls');
250+
251+
$this->assertEquals(
252+
$obj,
253+
$this->normalizer->denormalize(array(
254+
'bar' => null,
255+
'foo_bar' => '@dunglas',
256+
'symfony' => '@coopTilleuls',
257+
'coop_tilleuls' => 'les-tilleuls.coop',
258+
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
259+
);
260+
}
261+
209262
/**
210263
* @dataProvider provideCallbacks
211264
*/
@@ -376,6 +429,11 @@ public function testDenormalizeNonExistingAttribute()
376429
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\ObjectDummy')
377430
);
378431
}
432+
433+
public function testNoTraversableSupport()
434+
{
435+
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
436+
}
379437
}
380438

381439
class ObjectDummy
@@ -423,7 +481,7 @@ public function setCamelCase($camelCase)
423481

424482
public function otherMethod()
425483
{
426-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
484+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
427485
}
428486

429487
public function setObject($object)
@@ -462,7 +520,7 @@ public function isBaz()
462520

463521
public function otherMethod()
464522
{
465-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
523+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
466524
}
467525
}
468526

@@ -495,6 +553,6 @@ public function getBaz()
495553

496554
public function otherMethod()
497555
{
498-
throw new \RuntimeException("Dummy::otherMethod() should not be called");
556+
throw new \RuntimeException('Dummy::otherMethod() should not be called');
499557
}
500558
}

0 commit comments

Comments
 (0)