Skip to content

Commit

Permalink
feature #14187 [Serializer] Supports hassers and setters for groups a…
Browse files Browse the repository at this point in the history
…nnotations (dunglas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Serializer] Supports hassers and setters for groups annotations

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

For more coherence with the new `ObjectNormalizer` (#13257), this PR allows to add `@Groups` annotations on hasser and setter methods too.

Commits
-------

9c87ecc [Serializer] Supports hassers and setters for groups annotations
  • Loading branch information
dunglas committed Apr 27, 2015
2 parents 0cd300f + 9c87ecc commit a1f7fb3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Expand Up @@ -54,7 +54,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
$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) {
Expand All @@ -68,10 +68,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
}

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])) {
Expand All @@ -85,7 +85,7 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
$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));
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Symfony/Component/Serializer/Tests/Fixtures/GroupDummy.php
Expand Up @@ -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;
Expand All @@ -57,7 +60,7 @@ public function setFooBar($fooBar)
/**
* @Groups({"a", "b"})
*/
public function getFooBar()
public function isFooBar()
{
return $this->fooBar;
}
Expand Down

0 comments on commit a1f7fb3

Please sign in to comment.