Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 926aa48

Browse files
dunglasfabpot
authored andcommitted
[Serializer] Allow to specify a single value in @groups
1 parent 62533f3 commit 926aa48

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/Symfony/Component/Serializer/Annotation/Groups.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Groups
2525
{
2626
/**
27-
* @var array
27+
* @var string[]
2828
*/
2929
private $groups;
3030

@@ -39,23 +39,20 @@ public function __construct(array $data)
3939
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
4040
}
4141

42-
if (!is_array($data['value'])) {
43-
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
44-
}
45-
46-
foreach ($data['value'] as $group) {
42+
$value = (array) $data['value'];
43+
foreach ($value as $group) {
4744
if (!is_string($group)) {
48-
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
45+
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', get_class($this)));
4946
}
5047
}
5148

52-
$this->groups = $data['value'];
49+
$this->groups = $value;
5350
}
5451

5552
/**
5653
* Gets groups.
5754
*
58-
* @return array
55+
* @return string[]
5956
*/
6057
public function getGroups()
6158
{

src/Symfony/Component/Serializer/Tests/Annotation/GroupsTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testEmptyGroupsParameter()
3131
*/
3232
public function testNotAnArrayGroupsParameter()
3333
{
34-
new Groups(array('value' => 'coopTilleuls'));
34+
new Groups(array('value' => 12));
3535
}
3636

3737
/**
@@ -49,4 +49,10 @@ public function testGroupsParameters()
4949
$groups = new Groups(array('value' => $validData));
5050
$this->assertEquals($validData, $groups->getGroups());
5151
}
52+
53+
public function testSingleGroup()
54+
{
55+
$groups = new Groups(array('value' => 'a'));
56+
$this->assertEquals(array('a'), $groups->getGroups());
57+
}
5258
}

0 commit comments

Comments
 (0)