Skip to content

Commit 9c87ecc

Browse files
committed
[Serializer] Supports hassers and setters for groups annotations
1 parent 40a350b commit 9c87ecc

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
5454
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
5555
}
5656

57-
if ($property->getDeclaringClass()->name == $className) {
57+
if ($property->getDeclaringClass()->name === $className) {
5858
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
5959
if ($groups instanceof Groups) {
6060
foreach ($groups->getGroups() as $group) {
@@ -68,10 +68,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
6868
}
6969

7070
foreach ($reflectionClass->getMethods() as $method) {
71-
if ($method->getDeclaringClass()->name == $className) {
71+
if ($method->getDeclaringClass()->name === $className) {
7272
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
7373
if ($groups instanceof Groups) {
74-
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
74+
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
7575
$attributeName = lcfirst($matches[2]);
7676

7777
if (isset($attributesMetadata[$attributeName])) {
@@ -85,7 +85,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
8585
$attributeMetadata->addGroup($group);
8686
}
8787
} else {
88-
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get" or "is".', $className, $method->name));
88+
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
8989
}
9090
}
9191

src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
2222
* @Groups({"a"})
2323
*/
2424
private $foo;
25-
/**
26-
* @Groups({"b", "c"})
27-
*/
2825
protected $bar;
2926
private $fooBar;
3027
private $symfony;
3128

29+
/**
30+
* @Groups({"b"})
31+
*/
3232
public function setBar($bar)
3333
{
3434
$this->bar = $bar;
3535
}
3636

37+
/**
38+
* @Groups({"c"})
39+
*/
3740
public function getBar()
3841
{
3942
return $this->bar;
@@ -57,7 +60,7 @@ public function setFooBar($fooBar)
5760
/**
5861
* @Groups({"a", "b"})
5962
*/
60-
public function getFooBar()
63+
public function isFooBar()
6164
{
6265
return $this->fooBar;
6366
}

0 commit comments

Comments
 (0)