Skip to content

Commit 0ce70f5

Browse files
committed
Merge branch '2.5'
* 2.5: (33 commits) [Validator] Added Swedish translations [Validator] Fixed ExpressionValidator when the validation root is not an object [Validator] Fixed: Made it possible (again) to pass a class name to Validator::validatePropertyValue() 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. Issue #11489 Added some CA and ES translations [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 Add point about ConsoleLogger to Console 2.5 changelog [Validator] Fixed failing tests [Validator] CS fixes [FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy validators [Validator] Added extensive test coverage for the constraint validators for the different APIs ... Conflicts: src/Symfony/Component/Validator/Resources/translations/validators.ca.xlf
2 parents b05371d + f7bca26 commit 0ce70f5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

Normalizer/GetSetMethodNormalizer.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ public function normalize($object, $format = null, array $context = array())
129129
*/
130130
public function denormalize($data, $class, $format = null, array $context = array())
131131
{
132+
if (is_array($data) || is_object($data) && $data instanceof \ArrayAccess) {
133+
$normalizedData = $data;
134+
} elseif (is_object($data)) {
135+
$normalizedData = array();
136+
137+
foreach ($data as $attribute => $value) {
138+
$normalizedData[$attribute] = $value;
139+
}
140+
} else {
141+
$normalizedData = array();
142+
}
143+
132144
$reflectionClass = new \ReflectionClass($class);
133145
$constructor = $reflectionClass->getConstructor();
134146

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

142-
if (isset($data[$paramName])) {
143-
$params[] = $data[$paramName];
154+
if (isset($normalizedData[$paramName])) {
155+
$params[] = $normalizedData[$paramName];
144156
// don't run set for a parameter passed to the constructor
145-
unset($data[$paramName]);
157+
unset($normalizedData[$paramName]);
146158
} elseif ($constructorParameter->isOptional()) {
147159
$params[] = $constructorParameter->getDefaultValue();
148160
} else {
@@ -156,10 +168,10 @@ public function denormalize($data, $class, $format = null, array $context = arra
156168

157169
$object = $reflectionClass->newInstanceArgs($params);
158170
} else {
159-
$object = new $class;
171+
$object = new $class();
160172
}
161173

162-
foreach ($data as $attribute => $value) {
174+
foreach ($normalizedData as $attribute => $value) {
163175
$setter = 'set'.$this->formatAttribute($attribute);
164176

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

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 33 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');
@@ -66,6 +71,17 @@ public function testDenormalize()
6671
$this->assertTrue($obj->isBaz());
6772
}
6873

74+
public function testDenormalizeWithObject()
75+
{
76+
$data = new \stdClass();
77+
$data->foo = 'foo';
78+
$data->bar = 'bar';
79+
$data->fooBar = 'foobar';
80+
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any');
81+
$this->assertEquals('foo', $obj->getFoo());
82+
$this->assertEquals('bar', $obj->getBar());
83+
}
84+
6985
public function testDenormalizeOnCamelCaseFormat()
7086
{
7187
$this->normalizer->setCamelizedAttributes(array('camel_case'));
@@ -76,6 +92,11 @@ public function testDenormalizeOnCamelCaseFormat()
7692
$this->assertEquals('camelCase', $obj->getCamelCase());
7793
}
7894

95+
public function testDenormalizeNull()
96+
{
97+
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
98+
}
99+
79100
/**
80101
* @dataProvider attributeProvider
81102
*/
@@ -119,6 +140,18 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
119140
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
120141
}
121142

143+
public function testConstructorWithObjectDenormalize()
144+
{
145+
$data = new \stdClass();
146+
$data->foo = 'foo';
147+
$data->bar = 'bar';
148+
$data->baz = true;
149+
$data->fooBar = 'foobar';
150+
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any');
151+
$this->assertEquals('foo', $obj->getFoo());
152+
$this->assertEquals('bar', $obj->getBar());
153+
}
154+
122155
/**
123156
* @dataProvider provideCallbacks
124157
*/

0 commit comments

Comments
 (0)