Skip to content

[Serializer] Supports hassers and setters for groups annotations #14187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this present before? Can it safely be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe, I've just moved them on other supported methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you test "hasser" ?


/**
* @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