8000 [Serializer] Add support for variadic arguments in the GetSetNormalizer by stof · Pull Request #15426 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for variadic arguments in the GetSetNormalizer #15426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,19 @@ public function denormalize($data, $class, $format = null, array $context = arra
foreach ($constructorParameters as $constructorParameter) {
$paramName = lcfirst($this->formatAttribute($constructorParameter->name));

if (isset($normalizedData[$paramName])) {
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
if (isset($normalizedData[$paramName])) {
if (!is_array($normalizedData[$paramName])) {
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test for this?

}

$params = array_merge($params, $normalizedData[$paramName]);
}
} elseif (isset($normalizedData[$paramName])) {
$params[] = $normalizedData[$paramName];
// don't run set for a parameter passed to the constructor
unset($normalizedData[$paramName]);
} elseif ($constructorParameter->isOptional()) {
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();
} else {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Co 8000 mponent\Serializer\Tests\Fixtures;

class VariadicConstructorArgsDummy
{
private $foo;

public function __construct(...$foo)
{
$this->foo = $foo;
}

public function getFoo()
{
return $this->foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ public function testConstructorDenormalizeWithMissingOptionalArgument()
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
}

/**
* @requires PHP 5.6
*/
public function testConstructorDenormalizeWithVariadicArgument()
{
$obj = $this->normalizer->denormalize(
array('foo' => array(1, 2, 3)),
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
}

/**
* @requires PHP 5.6
*/
public function testConstructorDenormalizeWithMissingVariadicArgument()
{
$obj = $this->normalizer->denormalize(
array(),
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
$this->assertEquals(array(), $obj->getFoo());
}

public function testConstructorWithObjectDenormalize()
{
$data = new \stdClass();
Expand Down
0