[Serializer] Allow to specify a single value in @Groups

This commit is contained in:
Kévin Dunglas 2016-11-13 20:28:52 +01:00 committed by Fabien Potencier
parent 62533f322e
commit 926aa480b2
2 changed files with 13 additions and 10 deletions

View File

@ -24,7 +24,7 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
class Groups
{
/**
* @var array
* @var string[]
*/
private $groups;
@ -39,23 +39,20 @@ class Groups
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
}
if (!is_array($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
}
foreach ($data['value'] as $group) {
$value = (array) $data['value'];
foreach ($value as $group) {
if (!is_string($group)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', get_class($this)));
}
}
$this->groups = $data['value'];
$this->groups = $value;
}
/**
* Gets groups.
*
* @return array
* @return string[]
*/
public function getGroups()
{

View File

@ -31,7 +31,7 @@ class GroupsTest extends \PHPUnit_Framework_TestCase
*/
public function testNotAnArrayGroupsParameter()
{
new Groups(array('value' => 'coopTilleuls'));
new Groups(array('value' => 12));
}
/**
@ -49,4 +49,10 @@ class GroupsTest extends \PHPUnit_Framework_TestCase
$groups = new Groups(array('value' => $validData));
$this->assertEquals($validData, $groups->getGroups());
}
public function testSingleGroup()
{
$groups = new Groups(array('value' => 'a'));
$this->assertEquals(array('a'), $groups->getGroups());
}
}