Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 8.12 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. commit 31814d662b1e0a843a909a9159491a8894795403
  2. Author: Fabien Potencier <fabien.potencier@gmail.com>
  3. Date:   Wed Jul 6 08:35:10 2011 +0200
  4.  
  5.     [Validator] removed the possibility to define constraints on non-public elements
  6.  
  7. diff --git a/src/Symfony/Component/Form/Resources/config/validation.xml b/src/Symfony/Component/Form/Resources/config/validation.xml
  8. index 5512560..cc9d0b0 100644
  9. --- a/src/Symfony/Component/Form/Resources/config/validation.xml
  10. +++ b/src/Symfony/Component/Form/Resources/config/validation.xml
  11. @@ -11,8 +11,8 @@
  12.          <value>validateFormData</value>
  13.        </value>
  14.      </constraint>
  15. -    <property name="children">
  16. +    <getter property="children">
  17.        <constraint name="Valid" />
  18. -    </property>
  19. +    </getter>
  20.    </class>
  21.  </constraint-mapping>
  22. diff --git a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
  23. index 1235aa3..9ef2e46 100644
  24. --- a/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
  25. +++ b/src/Symfony/Component/Validator/Mapping/ClassMetadata.php
  26. @@ -182,14 +182,12 @@ class ClassMetadata extends ElementMetadata
  27.  
  28.                  $this->addMemberMetadata($member);
  29.  
  30. -                if (!$member->isPrivate()) {
  31. -                    $property = $member->getPropertyName();
  32. -
  33. -                    if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
  34. -                        $this->properties[$property] = $member;
  35. -                    } else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
  36. -                        $this->getters[$property] = $member;
  37. -                    }
  38. +                $property = $member->getPropertyName();
  39. +
  40. +                if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
  41. +                    $this->properties[$property] = $member;
  42. +                } else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
  43. +                    $this->getters[$property] = $member;
  44.                  }
  45.              }
  46.          }
  47. diff --git a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
  48. index f27f4a2..a947a70 100644
  49. --- a/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
  50. +++ b/src/Symfony/Component/Validator/Mapping/GetterMetadata.php
  51. @@ -26,12 +26,17 @@ class GetterMetadata extends MemberMetadata
  52.          $getMethod = 'get'.ucfirst($property);
  53.          $isMethod = 'is'.ucfirst($property);
  54.  
  55. -        if (method_exists($class, $getMethod)) {
  56. +        $r = new \ReflectionClass($class);
  57. +        if ($r->hasMethod($getMethod)) {
  58.              $method = $getMethod;
  59. -        } else if (method_exists($class, $isMethod)) {
  60. +        } elseif ($r->hasMethod($isMethod)) {
  61.              $method = $isMethod;
  62.          } else {
  63. -            throw new ValidatorException(sprintf('Neither method %s nor %s exists in class %s', $getMethod, $isMethod, $class));
  64. +            throw new ValidatorException(sprintf('Neither method "%s" nor "%s" exists in class "%s".', $getMethod, $isMethod, $class));
  65. +        }
  66. +
  67. +        if (!$r->getMethod($method)->isPublic()) {
  68. +            throw new ValidatorException(sprintf('Unable to add constraint on a non-public method ("%s::%s").', $class, $method));
  69.          }
  70.  
  71.          parent::__construct($class, $method, $property);
  72. @@ -42,14 +47,6 @@ class GetterMetadata extends MemberMetadata
  73.       */
  74.      public function getValue($object)
  75.      {
  76. -        return $this->getReflectionMember()->invoke($object);
  77. -    }
  78. -
  79. -    /**
  80. -     * {@inheritDoc}
  81. -     */
  82. -    protected function newReflectionMember()
  83. -    {
  84. -        return new \ReflectionMethod($this->getClassName(), $this->getName());
  85. +        return $object->{$this->getName()}();
  86.      }
  87.  }
  88. diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
  89. index 2474dba..524a41b 100644
  90. --- a/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
  91. +++ b/src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
  92. @@ -49,7 +49,12 @@ class AnnotationLoader implements LoaderInterface
  93.              if ($property->getDeclaringClass()->getName() == $className) {
  94.                  foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
  95.                      if ($constraint instanceof Constraint) {
  96. -                        $metadata->addPropertyConstraint($property->getName(), $constraint);
  97. +                        // if property is not public, add the constraint on the getter instead
  98. +                        if ($property->isPublic()) {
  99. +                            $metadata->addPropertyConstraint($property->getName(), $constraint);
  100. +                        } else {
  101. +                            $metadata->addGetterConstraint($property->getName(), $constraint);
  102. +                        }
  103.                      }
  104.  
  105.                      $loaded = true;
  106. diff --git a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
  107. index dcb4e75..91395f8 100644
  108. --- a/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
  109. +++ b/src/Symfony/Component/Validator/Mapping/MemberMetadata.php
  110. @@ -107,36 +107,6 @@ abstract class MemberMetadata extends ElementMetadata
  111.      }
  112.  
  113.      /**
  114. -     * Returns whether this member is public
  115. -     *
  116. -     * @return Boolean
  117. -     */
  118. -    public function isPublic()
  119. -    {
  120. -        return $this->getReflectionMember()->isPublic();
  121. -    }
  122. -
  123. -    /**
  124. -     * Returns whether this member is protected
  125. -     *
  126. -     * @return Boolean
  127. -     */
  128. -    public function isProtected()
  129. -    {
  130. -        return $this->getReflectionMember()->isProtected();
  131. -    }
  132. -
  133. -    /**
  134. -     * Returns whether this member is private
  135. -     *
  136. -     * @return Boolean
  137. -     */
  138. -    public function isPrivate()
  139. -    {
  140. -        return $this->getReflectionMember()->isPrivate();
  141. -    }
  142. -
  143. -    /**
  144.       * Returns whether objects stored in this member should be validated
  145.       *
  146.       * @return Boolean
  147. @@ -165,25 +135,4 @@ abstract class MemberMetadata extends ElementMetadata
  148.       * @return mixed The property value
  149.       */
  150.      abstract public function getValue($object);
  151. -
  152. -    /**
  153. -     * Returns the Reflection instance of the member
  154. -     *
  155. -     * @return object
  156. -     */
  157. -    public function getReflectionMember()
  158. -    {
  159. -        if (!$this->reflMember) {
  160. -            $this->reflMember = $this->newReflectionMember();
  161. -        }
  162. -
  163. -        return $this->reflMember;
  164. -    }
  165. -
  166. -    /**
  167. -     * Creates a new Reflection instance for the member
  168. -     *
  169. -     * @return object
  170. -     */
  171. -    abstract protected function newReflectionMember();
  172.  }
  173. diff --git a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
  174. index cb60c70..314cb2b 100644
  175. --- a/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
  176. +++ b/src/Symfony/Component/Validator/Mapping/PropertyMetadata.php
  177. @@ -23,8 +23,13 @@ class PropertyMetadata extends MemberMetadata
  178.       */
  179.      public function __construct($class, $name)
  180.      {
  181. -        if (!property_exists($class, $name)) {
  182. -            throw new ValidatorException(sprintf('Property %s does not exists in class %s', $name, $class));
  183. +        $r = new \ReflectionClass($class);
  184. +        if (!$r->hasProperty($name)) {
  185. +            throw new ValidatorException(sprintf('Property "%s" does not exists in class "%s".', $name, $class));
  186. +        }
  187. +
  188. +        if (!$r->getProperty($name)->isPublic()) {
  189. +            throw new ValidatorException(sprintf('Unable to add constraint on a non-public property ("%s::%s").', $class, $name));
  190.          }
  191.  
  192.          parent::__construct($class, $name, $name);
  193. @@ -35,17 +40,6 @@ class PropertyMetadata extends MemberMetadata
  194.       */
  195.      public function getValue($object)
  196.      {
  197. -        return $this->getReflectionMember()->getValue($object);
  198. -    }
  199. -
  200. -    /**
  201. -     * {@inheritDoc}
  202. -     */
  203. -    protected function newReflectionMember()
  204. -    {
  205. -        $member = new \ReflectionProperty($this->getClassName(), $this->getName());
  206. -        $member->setAccessible(true);
  207. -
  208. -        return $member;
  209. +        return $object->{$this->getName()};
  210.      }
  211.  }