Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PropertyAccess] Allow custom methods on property accesses #18016

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

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added method annotation for virtual properties
  • Loading branch information
lrlopez committed Jul 3, 2016
commit e46df9834db8811660ba22cac18bffe9929e1c36
30 changes: 30 additions & 0 deletions 30 src/Symfony/Component/PropertyAccess/Annotation/Adder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Annotation;

/**
* Property accessor adder configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class Adder
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be named PropertyAdder for more clarity.

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw you should make it possible to omit property when using the annotation:

/**
 * @PropertyAdder("fields")
 */
public function addFieldToList($field)
{
    // ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. I didn't add the Property prefix as there was a property used as a parameter. Changing the annotation name it has sense to allow it be omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm... I've doing some experiments. If you omit property in the parameter still works because by default it assigns its value to the first public field in the annotation class. So, no code changes are needed. If we add another parameter, we just have to make sure it is not the first one in the class.

{
/**
* Associates this method to the setter of this property.
*
* @var string
*/
public $property;
}
30 changes: 30 additions & 0 deletions 30 src/Symfony/Component/PropertyAccess/Annotation/Getter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Annotation;

/**
* Property accessor getter configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class Getter
Copy link
Contributor

Choose a reason for hiding this comment

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

PropertyGetter, same for other annotations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

{
/**
* Associates this method to the getter of this property.
*
* @var string
*/
public $property;
}
30 changes: 30 additions & 0 deletions 30 src/Symfony/Component/PropertyAccess/Annotation/Remover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Annotation;

/**
* Property accessor remover configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class Remover
{
/**
* Associates this method to the setter of this property.
*
* @var string
*/
public $property;
}
30 changes: 30 additions & 0 deletions 30 src/Symfony/Component/PropertyAccess/Annotation/Setter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Annotation;

/**
* Property accessor setter configuration annotation.
*
* @Annotation
* @Target({"METHOD"})
*
* @author Luis Ramón López <lrlopez@gmail.com>
*/
class Setter
{
/**
* Associates this method to the setter of this property.
*
* @var string
*/
public $property;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
namespace Symfony\Component\PropertyAccess\Mapping\Loader;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\PropertyAccess\Annotation\Adder;
use Symfony\Component\PropertyAccess\Annotation\Getter;
use Symfony\Component\PropertyAccess\Annotation\Property;
use Symfony\Component\PropertyAccess\Annotation\Remover;
use Symfony\Component\PropertyAccess\Annotation\Setter;
use Symfony\Component\PropertyAccess\Mapping\PropertyMetadata;
use Symfony\Component\PropertyAccess\Mapping\ClassMetadata;

Expand Down Expand Up @@ -68,6 +72,44 @@ public function loadClassMetadata(ClassMetadata $classMetadata)
}
}

foreach ($reflectionClass->getMethods() as $method) {
if ($method->getDeclaringClass()->name === $className) {

foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Getter) {
if (!isset($propertiesMetadata[$annotation->property])) {
$propertiesMetadata[$annotation->property] = new PropertyMetadata($annotation->property);
$classMetadata->addPropertyMetadata($propertiesMetadata[$annotation->property]);
}
$propertiesMetadata[$annotation->property]->setGetter($method->getName());
}
if ($annotation instanceof Setter) {
if (!isset($propertiesMetadata[$annotation->property])) {
$propertiesMetadata[$annotation->property] = new PropertyMetadata($annotation->property);
$classMetadata->addPropertyMetadata($propertiesMetadata[$annotation->property]);
}
$propertiesMetadata[$annotation->property]->setSetter($method->getName());
}
if ($annotation instanceof Adder) {
if (!isset($propertiesMetadata[$annotation->property])) {
$propertiesMetadata[$annotation->property] = new PropertyMetadata($annotation->property);
$classMetadata->addPropertyMetadata($propertiesMetadata[$annotation->property]);
}
$propertiesMetadata[$annotation->property]->setAdder($method->getName());
}
if ($annotation instanceof Remover) {
if (!isset($propertiesMetadata[$annotation->property])) {
$propertiesMetadata[$annotation->property] = new PropertyMetadata($annotation->property);
$classMetadata->addPropertyMetadata($propertiesMetadata[$annotation->property]);
}
$propertiesMetadata[$annotation->property]->setRemover($method->getName());
}

$loaded = true;
}
}
}

return $loaded;
}
}
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ private function getReadAccessInfo($class, $property)
$hasProperty = $reflClass->hasProperty($property);
$access[self::ACCESS_HAS_PROPERTY] = $hasProperty;

if ($hasProperty && $this->classMetadataFactory) {
if ($this->classMetadataFactory) {
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertiesMetadata();
$metadata = isset($metadata[$property]) ? $metadata[$property] : null;
}
Expand Down Expand Up @@ -753,7 +753,7 @@ private function getWriteAccessInfo($class, $property, $value)
$transversable = is_array($value) || $value instanceof \Traversable;
$done = false;

if ($hasProperty && $this->classMetadataFactory) {
if ($this->classMetadataFactory) {
$metadata = $this->classMetadataFactory->getMetadataFor($class)->getPropertiesMetadata();
$metadata = isset($metadata[$property]) ? $metadata[$property] : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

use Symfony\Component\PropertyAccess\Annotation\Property;
use Symfony\Component\PropertyAccess\Annotation\Getter;
use Symfony\Component\PropertyAccess\Annotation\Setter;

class TestClass
{
Expand All @@ -29,12 +31,14 @@ class TestClass
private $publicGetter;
private $date;

private $quantity;

/**
* @Property(getter="customGetterTest", setter="customSetterTest")
*/
private $customGetterSetter;

public function __construct($value)
public function __construct($value, $quantity = 2, $pricePerUnit = 10)
{
$this->publicProperty = $value;
$this->publicAccessor = $value;
Expand All @@ -47,6 +51,8 @@ public function __construct($value)
$this->publicHasAccessor = $value;
$this->publicGetter = $value;
$this->customGetterSetter = $value;
$this->quantity = $quantity;
$this->pricePerUnit = $pricePerUnit;
}

public function setPublicAccessor($value)
Expand Down Expand Up @@ -201,4 +207,30 @@ public function customSetterTest($value)
{
$this->customGetterSetter = $value;
}

/**
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}

/**
* @Getter(property="total")
*/
public function getTotal()
{
return $this->quantity * $this->pricePerUnit;
}

/**
* @Setter(property="total")
*
* @param mixed $total
*/
public function setTotal($total)
{
$this->quantity = $total / $this->pricePerUnit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\PropertyAccess\Annotation\Adder;
use Symfony\Component\PropertyAccess\Annotation\Getter;
use Symfony\Component\PropertyAccess\Annotation\Remover;
use Symfony\Component\PropertyAccess\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\PropertyAccess\Mapping\Loader\AnnotationLoader;
use Symfony\Component\PropertyAccess\PropertyAccessor;
Expand All @@ -39,6 +42,10 @@ public function addAxis($axis)
}

// In the test, use a name that StringUtil can't uniquely singularify
/**
* @Adder(property="customVirtualAxes")
* @param $axis
*/
public function addAxisTest($axis)
{
$this->customAxes[] = $axis;
Expand All @@ -55,6 +62,10 @@ public function removeAxis($axis)
}
}

/**
* @Remover(property="customVirtualAxes")
* @param $axis
*/
public function removeAxisTest($axis)
{
foreach ($this->customAxes as $key => $value) {
Expand All @@ -71,6 +82,10 @@ public function getAxes()
return $this->axes;
}

/**
* @Getter(property="customVirtualAxes")
* @return null
*/
public function getCustomAxes()
{
return $this->customAxes;
Expand Down Expand Up @@ -202,6 +217,28 @@ public function testSetValueCallsCustomAdderAndRemoverForCollections()
$this->assertEquals($axesMergedCopy, $axesMerged);
}

public function testSetValueCallsCustomAdderAndRemoverForCollectionsMethodAnnotation()
{
$axesBefore = $this->getContainer(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));
$axesMerged = $this->getContainer(array(1 => 'first', 2 => 'second', 3 => 'third'));
$axesAfter = $this->getContainer(array(1 => 'second', 5 => 'first', 6 => 'third'));
$axesMergedCopy = is_object($axesMerged) ? clone $axesMerged : $axesMerged;

// Don't use a mock in order to test whether the collections are
// modified while iterating them
$car = new PropertyAccessorCollectionTest_Car($axesBefore);

AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

$this->propertyAccessor = new PropertyAccessor(false, false, new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$this->propertyAccessor->setValue($car, 'customVirtualAxes', $axesMerged);

$this->assertEquals($axesAfter, $car->getCustomAxes());

// The passed collection was not modified
$this->assertEquals($axesMergedCopy, $axesMerged);
}

/**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
* @expectedExceptionMessage Neither the property "axes" nor one of the methods "addAx()"/"removeAx()", "addAxe()"/"removeAxe()", "addAxis()"/"removeAxis()", "setAxes()", "axes()", "__set()" or "__call()" exist and have public access in class "Mock_PropertyAccessorCollectionTest_CarNoAdderAndRemover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ public function testGetWithCustomGetter()
$this->assertSame('webmozart', $this->propertyAccessor->getValue(new TestClass('webmozart'), 'customGetterSetter'));
}

public function testGetWithCustomGetterMethodAnnotation()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
$this->assertSame(200, $this->propertyAccessor->getValue(new TestClass('webmozart', 10, 20), 'total'));
}

/**
* @dataProvider getValidPropertyPaths
*/
Expand Down Expand Up @@ -321,6 +328,18 @@ public function testSetValueWithCustomSetter()
$this->assertEquals('it works!', $custom->customGetterTest());
}

public function testSetValueWithCustomSetterMethodAnnotation()
{
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
$this->propertyAccessor = new PropertyAccessor(false, false, new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));

$custom = new TestClass('webmozart', 10, 20);

$this->propertyAccessor->setValue($custom, 'total', 5);

$this->assertEquals(5, $custom->getTotal());
}

public function testGetValueWhenArrayValueIsNull()
{
$this->propertyAccessor = new PropertyAccessor(false, true);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.