From 9b6cd808c759178b6a271e9e3dfd838555fc43c8 Mon Sep 17 00:00:00 2001 From: Gladhon Date: Thu, 14 Feb 2013 14:33:52 +0100 Subject: [PATCH 1/2] Update src/Symfony/Component/Validator/Mapping/GetterMetadata.php Please use the concrete Class Methods to check validations. Its realy unexpected that Validator don't use the normal Object way and can call anything... --- src/Symfony/Component/Validator/Mapping/GetterMetadata.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php index d655b46d172a4..2e2652c47d60f 100644 --- a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php @@ -44,7 +44,8 @@ public function __construct($class, $property) */ public function getPropertyValue($object) { - return $this->getReflectionMember()->invoke($object); + $refl = new \ReflectionMethod(get_class($object), $this->getName()); + return $refl->invoke($object); } /** From b6a545711a5bddf96679bcaa19c34950a46c1b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Tue, 5 Mar 2013 16:41:51 +0100 Subject: [PATCH 2/2] [Validator] Fixed member (getter/property) metadata readers. [Validator] added overridden getter metadata test [Validator] class member reflection build requires object instance [Validator] fixed error [Validation] fixed member metadata reflection cache [Validation] added property metedata test [Validation] fixed class/member metadata mapping [Validation] removed var_dump --- .../Validator/Mapping/ClassMetadata.php | 2 +- .../Validator/Mapping/GetterMetadata.php | 7 ++-- .../Validator/Mapping/MemberMetadata.php | 37 ++++++++++++------- .../Validator/Mapping/PropertyMetadata.php | 11 ++++-- .../Validator/Tests/Fixtures/Entity.php | 7 +++- .../Validator/Tests/Fixtures/EntityParent.php | 6 +++ .../Tests/Mapping/GetterMetadataTest.php | 8 ++++ .../Tests/Mapping/MemberMetadataTest.php | 2 +- .../Tests/Mapping/PropertyMetadataTest.php | 10 +++++ 9 files changed, 67 insertions(+), 23 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php index 6ea9f2f6d957d..b7e003ec3da40 100644 --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php @@ -252,7 +252,7 @@ public function mergeConstraints(ClassMetadata $source) $this->addMemberMetadata($member); - if (!$member->isPrivate()) { + if (!$member->isPrivate($this->name)) { $property = $member->getPropertyName(); if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) { diff --git a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php index 2e2652c47d60f..1e44062463728 100644 --- a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php @@ -44,15 +44,14 @@ public function __construct($class, $property) */ public function getPropertyValue($object) { - $refl = new \ReflectionMethod(get_class($object), $this->getName()); - return $refl->invoke($object); + return $this->newReflectionMember($object)->invoke($object); } /** * {@inheritDoc} */ - protected function newReflectionMember() + protected function newReflectionMember($objectOrClassName) { - return new \ReflectionMethod($this->getClassName(), $this->getName()); + return new \ReflectionMethod($objectOrClassName, $this->getName()); } } diff --git a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php index ee9c6839ec50c..9f17f4fd03832 100644 --- a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php @@ -26,7 +26,7 @@ abstract class MemberMetadata extends ElementMetadata implements PropertyMetadat public $cascaded = false; public $collectionCascaded = false; public $collectionCascadedDeeply = false; - private $reflMember; + private $reflMember = array(); /** * Constructor. @@ -123,31 +123,37 @@ public function getPropertyName() /** * Returns whether this member is public * + * @param object|string $objectOrClassName The object or the class name + * * @return Boolean */ - public function isPublic() + public function isPublic($objectOrClassName) { - return $this->getReflectionMember()->isPublic(); + return $this->getReflectionMember($objectOrClassName)->isPublic(); } /** * Returns whether this member is protected * + * @param object|string $objectOrClassName The object or the class name + * * @return Boolean */ - public function isProtected() + public function isProtected($objectOrClassName) { - return $this->getReflectionMember()->isProtected(); + return $this->getReflectionMember($objectOrClassName)->isProtected(); } /** * Returns whether this member is private * + * @param object|string $objectOrClassName The object or the class name + * * @return Boolean */ - public function isPrivate() + public function isPrivate($objectOrClassName) { - return $this->getReflectionMember()->isPrivate(); + return $this->getReflectionMember($objectOrClassName)->isPrivate(); } /** @@ -202,21 +208,26 @@ public function getValue($object) /** * Returns the Reflection instance of the member * + * @param object|string $objectOrClassName The object or the class name + * * @return object */ - public function getReflectionMember() + public function getReflectionMember($objectOrClassName) { - if (!$this->reflMember) { - $this->reflMember = $this->newReflectionMember(); + $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName); + if (!isset($this->reflMember[$className])) { + $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName); } - return $this->reflMember; + return $this->reflMember[$className]; } /** * Creates a new Reflection instance for the member * - * @return object + * @param object|string $objectOrClassName The object or the class name + * + * @return mixed Reflection class */ - abstract protected function newReflectionMember(); + abstract protected function newReflectionMember($objectOrClassName); } diff --git a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php index c68968b0d221c..468f196f04a8e 100644 --- a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php @@ -37,15 +37,20 @@ public function __construct($class, $name) */ public function getPropertyValue($object) { - return $this->getReflectionMember()->getValue($object); + return $this->getReflectionMember($object)->getValue($object); } /** * {@inheritDoc} */ - protected function newReflectionMember() + protected function newReflectionMember($objectOrClassName) { - $member = new \ReflectionProperty($this->getClassName(), $this->getName()); + $class = new \ReflectionClass($objectOrClassName); + while (!$class->hasProperty($this->getName())) { + $class = $class->getParentClass(); + } + + $member = new \ReflectionProperty($class->getName(), $this->getName()); $member->setAccessible(true); return $member; diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/Entity.php b/src/Symfony/Component/Validator/Tests/Fixtures/Entity.php index 002b6ed1d4ad4..6784cdf882e36 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/Entity.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/Entity.php @@ -33,8 +33,8 @@ class Entity extends EntityParent implements EntityInterface protected $firstName; protected $lastName; public $reference; - private $internal; + public $data = 'Overridden data'; public function __construct($internal = null) { @@ -53,4 +53,9 @@ public function getLastName() { return $this->lastName; } + + public function getData() + { + return 'Overridden data'; + } } diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php b/src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php index d12d9435f456e..422bb28d0b5ea 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/EntityParent.php @@ -17,9 +17,15 @@ class EntityParent { protected $firstName; private $internal; + private $data = 'Data'; /** * @NotNull */ protected $other; + + public function getData() + { + return 'Data'; + } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php index d38225aa2e06f..6549bc8b99276 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/GetterMetadataTest.php @@ -35,4 +35,12 @@ public function testGetPropertyValueFromPublicGetter() $this->assertEquals('foobar from getter', $metadata->getPropertyValue($entity)); } + + public function testGetPropertyValueFromOverriddenPublicGetter() + { + $entity = new Entity(); + $metadata = new GetterMetadata(self::CLASSNAME, 'data'); + + $this->assertEquals('Overridden data', $metadata->getPropertyValue($entity)); + } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php index 747a36b39a77e..1abe58de72384 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/MemberMetadataTest.php @@ -77,7 +77,7 @@ public function getPropertyValue($object) { } - protected function newReflectionMember() + protected function newReflectionMember($object) { } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php b/src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php index aeabefc5bc450..f411d950e1708 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/PropertyMetadataTest.php @@ -17,6 +17,7 @@ class PropertyMetadataTest extends \PHPUnit_Framework_TestCase { const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity'; + const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent'; public function testInvalidPropertyName() { @@ -32,4 +33,13 @@ public function testGetPropertyValueFromPrivateProperty() $this->assertEquals('foobar', $metadata->getPropertyValue($entity)); } + + public function testGetPropertyValueFromOverriddenPrivateProperty() + { + $entity = new Entity('foobar'); + $metadata = new PropertyMetadata(self::PARENTCLASS, 'data'); + + $this->assertTrue($metadata->isPublic($entity)); + $this->assertEquals('Overridden data', $metadata->getPropertyValue($entity)); + } }