MemberMetadata.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Mapping;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  13. use Symfony\Component\Validator\ValidationVisitorInterface;
  14. /**
  15. * Stores all metadata needed for validating a class property.
  16. *
  17. * The method of accessing the property's value must be specified by subclasses
  18. * by implementing the {@link newReflectionMember()} method.
  19. *
  20. * This class supports serialization and cloning.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. *
  24. * @see PropertyMetadataInterface
  25. */
  26. abstract class MemberMetadata extends ElementMetadata implements PropertyMetadataInterface
  27. {
  28. /**
  29. * @internal This property is public in order to reduce the size of the
  30. * class' serialized representation. Do not access it. Use
  31. * {@link getClassName()} instead.
  32. */
  33. public $class;
  34. /**
  35. * @internal This property is public in order to reduce the size of the
  36. * class' serialized representation. Do not access it. Use
  37. * {@link getName()} instead.
  38. */
  39. public $name;
  40. /**
  41. * @internal This property is public in order to reduce the size of the
  42. * class' serialized representation. Do not access it. Use
  43. * {@link getPropertyName()} instead.
  44. */
  45. public $property;
  46. /**
  47. * @var \ReflectionMethod[]|\ReflectionProperty[]
  48. */
  49. private $reflMember = array();
  50. /**
  51. * @param string $class The name of the class this member is defined on
  52. * @param string $name The name of the member
  53. * @param string $property The property the member belongs to
  54. */
  55. public function __construct($class, $name, $property)
  56. {
  57. $this->class = $class;
  58. $this->name = $name;
  59. $this->property = $property;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. *
  64. * @deprecated since version 2.5, to be removed in 3.0.
  65. */
  66. public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
  67. {
  68. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  69. $visitor->visit($this, $value, $group, $propertyPath);
  70. if ($this->isCascaded()) {
  71. $visitor->validate($value, $propagatedGroup ?: $group, $propertyPath, $this->isCollectionCascaded(), $this->isCollectionCascadedDeeply());
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function addConstraint(Constraint $constraint)
  78. {
  79. if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
  80. throw new ConstraintDefinitionException(sprintf('The constraint %s cannot be put on properties or getters', \get_class($constraint)));
  81. }
  82. parent::addConstraint($constraint);
  83. return $this;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function __sleep()
  89. {
  90. return array_merge(parent::__sleep(), array(
  91. 'class',
  92. 'name',
  93. 'property',
  94. ));
  95. }
  96. /**
  97. * Returns the name of the member.
  98. *
  99. * @return string
  100. */
  101. public function getName()
  102. {
  103. return $this->name;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function getClassName()
  109. {
  110. return $this->class;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getPropertyName()
  116. {
  117. return $this->property;
  118. }
  119. /**
  120. * Returns whether this member is public.
  121. *
  122. * @param object|string $objectOrClassName The object or the class name
  123. *
  124. * @return bool
  125. */
  126. public function isPublic($objectOrClassName)
  127. {
  128. return $this->getReflectionMember($objectOrClassName)->isPublic();
  129. }
  130. /**
  131. * Returns whether this member is protected.
  132. *
  133. * @param object|string $objectOrClassName The object or the class name
  134. *
  135. * @return bool
  136. */
  137. public function isProtected($objectOrClassName)
  138. {
  139. return $this->getReflectionMember($objectOrClassName)->isProtected();
  140. }
  141. /**
  142. * Returns whether this member is private.
  143. *
  144. * @param object|string $objectOrClassName The object or the class name
  145. *
  146. * @return bool
  147. */
  148. public function isPrivate($objectOrClassName)
  149. {
  150. return $this->getReflectionMember($objectOrClassName)->isPrivate();
  151. }
  152. /**
  153. * Returns whether objects stored in this member should be validated.
  154. *
  155. * @return bool
  156. *
  157. * @deprecated since version 2.5, to be removed in 3.0.
  158. * Use {@link getCascadingStrategy()} instead.
  159. */
  160. public function isCascaded()
  161. {
  162. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0. Use the getCascadingStrategy() method instead.', E_USER_DEPRECATED);
  163. return (bool) ($this->cascadingStrategy & CascadingStrategy::CASCADE);
  164. }
  165. /**
  166. * Returns whether arrays or traversable objects stored in this member
  167. * should be traversed and validated in each entry.
  168. *
  169. * @return bool
  170. *
  171. * @deprecated since version 2.5, to be removed in 3.0.
  172. * Use {@link getTraversalStrategy()} instead.
  173. */
  174. public function isCollectionCascaded()
  175. {
  176. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
  177. return (bool) ($this->traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE));
  178. }
  179. /**
  180. * Returns whether arrays or traversable objects stored in this member
  181. * should be traversed recursively for inner arrays/traversable objects.
  182. *
  183. * @return bool
  184. *
  185. * @deprecated since version 2.5, to be removed in 3.0.
  186. * Use {@link getTraversalStrategy()} instead.
  187. */
  188. public function isCollectionCascadedDeeply()
  189. {
  190. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
  191. return !($this->traversalStrategy & TraversalStrategy::STOP_RECURSION);
  192. }
  193. /**
  194. * Returns the reflection instance for accessing the member's value.
  195. *
  196. * @param object|string $objectOrClassName The object or the class name
  197. *
  198. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  199. */
  200. public function getReflectionMember($objectOrClassName)
  201. {
  202. $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
  203. if (!isset($this->reflMember[$className])) {
  204. $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
  205. }
  206. return $this->reflMember[$className];
  207. }
  208. /**
  209. * Creates a new reflection instance for accessing the member's value.
  210. *
  211. * Must be implemented by subclasses.
  212. *
  213. * @param object|string $objectOrClassName The object or the class name
  214. *
  215. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  216. */
  217. abstract protected function newReflectionMember($objectOrClassName);
  218. }