Skip to content

Commit 96807a6

Browse files
committed
Merge branch '2.8'
* 2.8: (22 commits) [Form] fixed BC-break on grouped choice lists [WebProfilerBundle] add import for Twig macro made Symfony compatible with both Twig 1.x and 2.x [VarDumper] Add caster for pgsql resources [Debug/VarDumper] minor cleanups [Form] only use PropertyPath if not already callable [Form] fix reworked choice list phpdoc [DoctrineBridge][Form] Add old tests to legacy group Fixed warning when command alias is longer than command name removed _self usage when not needed Add support for target attributes. Implement the support of timezone objects in the stub IntlDateFormatter typofix - https://github.com/vlajos/misspell_fixer make doctrine mappings compiler pass exception message more understandable fix debug-ext 003.phpt [Yaml] Nested merge keys [FrameworkBundle] [Command] removed unused variable. [FrameworkBundle] fix serializer config check [Debug] Enhance DebugClassLoader performance on MacOSX Add support for variadic arguments in the GetSetNormalizer ...
2 parents 1050ea6 + c16b0c9 commit 96807a6

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
@@ -263,7 +263,15 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
263263

264264
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
265265
$ignored = in_array($paramName, $this->ignoredAttributes);
266-
if ($allowed && !$ignored && array_key_exists($key, $data)) {
266+
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
267+
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
268+
if (!is_array($data[$paramName])) {
269+
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));
270+
}
271+
272+
$params = array_merge($params, $data[$paramName]);
273+
}
274+
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
267275
$params[] = $data[$key];
268276
// don't run set for a parameter passed to the constructor
269277
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 <[email protected]>
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
@@ -144,6 +144,28 @@ public function testConstructorDenormalizeWithOptionalDefaultArgument()
144144
$this->assertEquals('test', $obj->getBar());
145145
}
146146

147+
/**
148+
* @requires PHP 5.6
149+
*/
150+
public function testConstructorDenormalizeWithVariadicArgument()
151+
{
152+
$obj = $this->normalizer->denormalize(
153+
array('foo' => array(1, 2, 3)),
154+
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
155+
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
156+
}
157+
158+
/**
159+
* @requires PHP 5.6
160+
*/
161+
public function testConstructorDenormalizeWithMissingVariadicArgument()
162+
{
163+
$obj = $this->normalizer->denormalize(
164+
array(),
165+
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
166+
$this->assertEquals(array(), $obj->getFoo());
167+
}
168+
147169
public function testConstructorWithObjectDenormalize()
148170
{
149171
$data = new \stdClass();

0 commit comments

Comments
 (0)