Skip to content

Commit 1a76887

Browse files
Merge branch '2.3' into 2.7
* 2.3: typofix - https://github.com/vlajos/misspell_fixer [Yaml] Nested merge keys Add support for variadic arguments in the GetSetNormalizer Conflicts: src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php src/Symfony/Component/Yaml/Tests/Fixtures/sfMergeKey.yml
2 parents 2942f87 + 3b3ffba commit 1a76887

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,15 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
312312

313313
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
314314
$ignored = in_array($paramName, $this->ignoredAttributes);
315-
if ($allowed && !$ignored && array_key_exists($key, $data)) {
315+
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
316+
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
317+
if (!is_array($data[$paramName])) {
318+
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
319+
}
320+
321+
$params = array_merge($params, $data[$paramName]);
322+
}
323+
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
316324
$params[] = $data[$key];
317325
// don't run set for a parameter passed to the constructor
318326
unset($data[$key]);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class VariadicConstructorArgsDummy
15+
{
16+
private $foo;
17+
18+
public function __construct(...$foo)
19+
{
20+
$this->foo = $foo;
21+
}
22+
23+
public function getFoo()
24+
{
25+
return $this->foo;
26+
}
27+
}

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ public function testConstructorDenormalizeWithOptionalDefaultArgument()
216216
$this->assertEquals('test', $obj->getBar());
217217
}
218218

219+
/**
220+
* @requires PHP 5.6
221+
*/
222+
public function testConstructorDenormalizeWithVariadicArgument()
223+
{
224+
$obj = $this->normalizer->denormalize(
225+
array('foo' => array(1, 2, 3)),
226+
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
227+
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
228+
}
229+
230+
/**
231+
* @requires PHP 5.6
232+
*/
233+
public function testConstructorDenormalizeWithMissingVariadicArgument()
234+
{
235+
$obj = $this->normalizer->denormalize(
236+
array(),
237+
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
238+
$this->assertEquals(array(), $obj->getFoo());
239+
}
240+
219241
public function testConstructorWithObjectDenormalize()
220242
{
221243
$data = new \stdClass();

0 commit comments

Comments
 (0)