- commit 31814d662b1e0a843a909a9159491a8894795403
- Author: Fabien Potencier <fabien.potencier@gmail.com>
- Date: Wed Jul 6 08:35:10 2011 +0200
- [Validator] removed the possibility to define constraints on non-public elements
- diff --git a/src/Symfony/Component/Form/Resources/config/validation.xml b/src/Symfony/Component/Form/Resources/config/validation.xml
- index 5512560..cc9d0b0 100644
- --- a/src/Symfony/Component/Form/Resources/config/validation.xml
- +++ b/src/Symfony/Component/Form/Resources/config/validation.xml
- @@ -11,8 +11,8 @@
- <value>validateFormData</value>
- </value>
- </constraint>
- - <property name="children">
- + <getter property="children">
- <constraint name="Valid" />
- - </property>
- + </getter>
- </class>
- </constraint-mapping>
- diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
- index 1235aa3..9ef2e46 100644
- --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
- +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
- @@ -182,14 +182,12 @@ class ClassMetadata extends ElementMetadata
- $this->addMemberMetadata($member);
- - if (!$member->isPrivate()) {
- - $property = $member->getPropertyName();
- -
- - if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
- - $this->properties[$property] = $member;
- - } else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
- - $this->getters[$property] = $member;
- - }
- + $property = $member->getPropertyName();
- +
- + if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
- + $this->properties[$property] = $member;
- + } else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
- + $this->getters[$property] = $member;
- }
- }
- }
- diff --git a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
- index f27f4a2..a947a70 100644
- --- a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
- +++ b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
- @@ -26,12 +26,17 @@ class GetterMetadata extends MemberMetadata
- $getMethod = 'get'.ucfirst($property);
- $isMethod = 'is'.ucfirst($property);
- - if (method_exists($class, $getMethod)) {
- + $r = new \ReflectionClass($class);
- + if ($r->hasMethod($getMethod)) {
- $method = $getMethod;
- - } else if (method_exists($class, $isMethod)) {
- + } elseif ($r->hasMethod($isMethod)) {
- $method = $isMethod;
- } else {
- - throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
- + throw new ValidatorException(sprintf('Neither method "%s" nor "%s" exists in class "%s".', $getMethod, $isMethod, $class));
- + }
- +
- + if (!$r->getMethod($method)->isPublic()) {
- + throw new ValidatorException(sprintf('Unable to add constraint on a non-public method ("%s::%s").', $class, $method));
- }
- parent::__construct($class, $method, $property);
- @@ -42,14 +47,6 @@ class GetterMetadata extends MemberMetadata
- */
- public function getValue($object)
- {
- - return $this->getReflectionMember()->invoke($object);
- - }
- -
- - /**
- - * {@inheritDoc}
- - */
- - protected function newReflectionMember()
- - {
- - return new \ReflectionMethod($this->getClassName(), $this->getName());
- + return $object->{$this->getName()}();
- }
- }
- diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
- index 2474dba..524a41b 100644
- --- a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
- +++ b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
- @@ -49,7 +49,12 @@ class AnnotationLoader implements LoaderInterface
- if ($property->getDeclaringClass()->getName() == $className) {
- foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
- if ($constraint instanceof Constraint) {
- - $metadata->addPropertyConstraint($property->getName(), $constraint);
- + // if property is not public, add the constraint on the getter instead
- + if ($property->isPublic()) {
- + $metadata->addPropertyConstraint($property->getName(), $constraint);
- + } else {
- + $metadata->addGetterConstraint($property->getName(), $constraint);
- + }
- }
- $loaded = true;
- diff --git a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
- index dcb4e75..91395f8 100644
- --- a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
- +++ b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
- @@ -107,36 +107,6 @@ abstract class MemberMetadata extends ElementMetadata
- }
- /**
- - * Returns whether this member is public
- - *
- - * @return Boolean
- - */
- - public function isPublic()
- - {
- - return $this->getReflectionMember()->isPublic();
- - }
- -
- - /**
- - * Returns whether this member is protected
- - *
- - * @return Boolean
- - */
- - public function isProtected()
- - {
- - return $this->getReflectionMember()->isProtected();
- - }
- -
- - /**
- - * Returns whether this member is private
- - *
- - * @return Boolean
- - */
- - public function isPrivate()
- - {
- - return $this->getReflectionMember()->isPrivate();
- - }
- -
- - /**
- * Returns whether objects stored in this member should be validated
- *
- * @return Boolean
- @@ -165,25 +135,4 @@ abstract class MemberMetadata extends ElementMetadata
- * @return mixed The property value
- */
- abstract public function getValue($object);
- -
- - /**
- - * Returns the Reflection instance of the member
- - *
- - * @return object
- - */
- - public function getReflectionMember()
- - {
- - if (!$this->reflMember) {
- - $this->reflMember = $this->newReflectionMember();
- - }
- -
- - return $this->reflMember;
- - }
- -
- - /**
- - * Creates a new Reflection instance for the member
- - *
- - * @return object
- - */
- - abstract protected function newReflectionMember();
- }
- diff --git a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
- index cb60c70..314cb2b 100644
- --- a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
- +++ b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
- @@ -23,8 +23,13 @@ class PropertyMetadata extends MemberMetadata
- */
- public function __construct($class, $name)
- {
- - if (!property_exists($class, $name)) {
- - throw new ValidatorException(sprintf('Property %s does not exists in class %s', $name, $class));
- + $r = new \ReflectionClass($class);
- + if (!$r->hasProperty($name)) {
- + throw new ValidatorException(sprintf('Property "%s" does not exists in class "%s".', $name, $class));
- + }
- +
- + if (!$r->getProperty($name)->isPublic()) {
- + throw new ValidatorException(sprintf('Unable to add constraint on a non-public property ("%s::%s").', $class, $name));
- }
- parent::__construct($class, $name, $name);
- @@ -35,17 +40,6 @@ class PropertyMetadata extends MemberMetadata
- */
- public function getValue($object)
- {
- - return $this->getReflectionMember()->getValue($object);
- - }
- -
- - /**
- - * {@inheritDoc}
- - */
- - protected function newReflectionMember()
- - {
- - $member = new \ReflectionProperty($this->getClassName(), $this->getName());
- - $member->setAccessible(true);
- -
- - return $member;
- + return $object->{$this->getName()};
- }
- }