[Serializer] Supports hassers and setters for groups annotations

This commit is contained in:
Kévin Dunglas 2015-04-02 22:29:07 +02:00
parent 40a350b7f8
commit 9c87eccf9f
2 changed files with 11 additions and 8 deletions

View File

@ -54,7 +54,7 @@ class AnnotationLoader implements LoaderInterface
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
}
if ($property->getDeclaringClass()->name == $className) {
if ($property->getDeclaringClass()->name === $className) {
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
if ($groups instanceof Groups) {
foreach ($groups->getGroups() as $group) {
@ -68,10 +68,10 @@ class AnnotationLoader implements LoaderInterface
}
foreach ($reflectionClass->getMethods() as $method) {
if ($method->getDeclaringClass()->name == $className) {
if ($method->getDeclaringClass()->name === $className) {
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
if ($groups instanceof Groups) {
if (preg_match('/^(get|is)(.+)$/i', $method->name, $matches)) {
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
$attributeName = lcfirst($matches[2]);
if (isset($attributesMetadata[$attributeName])) {
@ -85,7 +85,7 @@ class AnnotationLoader implements LoaderInterface
$attributeMetadata->addGroup($group);
}
} else {
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));
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));
}
}

View File

@ -22,18 +22,21 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
* @Groups({"a"})
*/
private $foo;
/**
* @Groups({"b", "c"})
*/
protected $bar;
private $fooBar;
private $symfony;
/**
* @Groups({"b"})
*/
public function setBar($bar)
{
$this->bar = $bar;
}
/**
* @Groups({"c"})
*/
public function getBar()
{
return $this->bar;
@ -57,7 +60,7 @@ class GroupDummy extends GroupDummyParent implements GroupDummyInterface
/**
* @Groups({"a", "b"})
*/
public function getFooBar()
public function isFooBar()
{
return $this->fooBar;
}