Skip to content

Commit 8b0b2c2

Browse files
committed
Merge branch '2.3' into 2.4
* 2.3: (22 commits) Fix incorrect romanian plural translations fix axes handling in Crawler::filterXPath() fix some docblocks Fixed self-reference in 'service_container' service breaks garbage collection (and clone). [Process] Fix tests when pcntl is not available. [DependencyInjection] Roll back changes made to generated files. [Console] Roll back changes made to fixture files. [Validator] Added more detailed inline documentation [Validator] Removed information from the violation output if the value is an array, object or resource partially reverted previous commit fixed CS properly handle null data when denormalizing [Validator] Renamed valueToString() to formatValue(); added missing formatValue() calls [Validator] Fixed CS [Validator] Fixed date-to-string conversion tests to match ICU 51 [Validator] Added "{{ value }}" parameters where they were missing [Validator] Simplified and explained the LuhnValidator [Validator] Simplified IssnValidator [Validator] Fixed and simplified IsbnValidator [Validator] Simplified IBAN validation algorithm ... Conflicts: src/Symfony/Component/Console/Helper/DescriptorHelper.php src/Symfony/Component/DependencyInjection/Container.php src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php src/Symfony/Component/HttpFoundation/File/UploadedFile.php src/Symfony/Component/HttpKernel/Fragment/FragmentHandler.php src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php src/Symfony/Component/Validator/Constraints/CollectionValidator.php src/Symfony/Component/Validator/Tests/Constraints/EqualToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/IdenticalToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotEqualToValidatorTest.php src/Symfony/Component/Validator/Tests/Constraints/NotIdenticalToValidatorTest.php
2 parents 58b229c + 68671b6 commit 8b0b2c2

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public function normalize($object, $format = null, array $context = array())
117117
*/
118118
public function denormalize($data, $class, $format = null, array $context = array())
119119
{
120+
if (is_array($data) || is_object($data) && $data instanceof \ArrayAccess) {
121+
$normalizedData = $data;
122+
} elseif (is_object($data)) {
123+
$normalizedData = array();
124+
125+
foreach ($data as $attribute => $value) {
126+
$normalizedData[$attribute] = $value;
127+
}
128+
} else {
129+
$normalizedData = array();
130+
}
131+
120132
$reflectionClass = new \ReflectionClass($class);
121133
$constructor = $reflectionClass->getConstructor();
122134

@@ -127,10 +139,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
127139
foreach ($constructorParameters as $constructorParameter) {
128140
$paramName = lcfirst($this->formatAttribute($constructorParameter->name));
129141

130-
if (isset($data[$paramName])) {
131-
$params[] = $data[$paramName];
142+
if (isset($normalizedData[$paramName])) {
143+
$params[] = $normalizedData[$paramName];
132144
// don't run set for a parameter passed to the constructor
133-
unset($data[$paramName]);
145+
unset($normalizedData[$paramName]);
134146
} elseif ($constructorParameter->isOptional()) {
135147
$params[] = $constructorParameter->getDefaultValue();
136148
} else {
@@ -144,10 +156,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
144156

145157
$object = $reflectionClass->newInstanceArgs($params);
146158
} else {
147-
$object = new $class;
159+
$object = new $class();
148160
}
149161

150-
foreach ($data as $attribute => $value) {
162+
foreach ($normalizedData as $attribute => $value) {
151163
$setter = 'set'.$this->formatAttribute($attribute);
152164

153165
if (method_exists($object, $setter)) {

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
1919
{
20+
/**
21+
* @var GetSetMethodNormalizer
22+
*/
23+
private $normalizer;
24+
2025
protected function setUp()
2126
{
2227
$this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer');
@@ -63,6 +68,17 @@ public function testDenormalize()
6368
$this->assertEquals('bar', $obj->getBar());
6469
}
6570

71+
public function testDenormalizeWithObject()
72+
{
73+
$data = new \stdClass();
74+
$data->foo = 'foo';
75+
$data->bar = 'bar';
76+
$data->fooBar = 'foobar';
77+
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any');
78+
$this->assertEquals('foo', $obj->getFoo());
79+
$this->assertEquals('bar', $obj->getBar());
80+
}
81+
6682
public function testDenormalizeOnCamelCaseFormat()
6783
{
6884
$this->normalizer->setCamelizedAttributes(array('camel_case'));
@@ -73,6 +89,11 @@ public function testDenormalizeOnCamelCaseFormat()
7389
$this->assertEquals('camelCase', $obj->getCamelCase());
7490
}
7591

92+
public function testDenormalizeNull()
93+
{
94+
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
95+
}
96+
7697
/**
7798
* @dataProvider attributeProvider
7899
*/
@@ -115,6 +136,17 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
115136
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
116137
}
117138

139+
public function testConstructorWithObjectDenormalize()
140+
{
141+
$data = new \stdClass();
142+
$data->foo = 'foo';
143+
$data->bar = 'bar';
144+
$data->fooBar = 'foobar';
145+
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any');
146+
$this->assertEquals('foo', $obj->getFoo());
147+
$this->assertEquals('bar', $obj->getBar());
148+
}
149+
118150
/**
119151
* @dataProvider provideCallbacks
120152
*/

0 commit comments

Comments
 (0)