Drupal investigation

MemberMetadata.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * @var string
  30. *
  31. * @internal This property is public in order to reduce the size of the
  32. * class' serialized representation. Do not access it. Use
  33. * {@link getClassName()} instead.
  34. */
  35. public $class;
  36. /**
  37. * @var string
  38. *
  39. * @internal This property is public in order to reduce the size of the
  40. * class' serialized representation. Do not access it. Use
  41. * {@link getName()} instead.
  42. */
  43. public $name;
  44. /**
  45. * @var string
  46. *
  47. * @internal This property is public in order to reduce the size of the
  48. * class' serialized representation. Do not access it. Use
  49. * {@link getPropertyName()} instead.
  50. */
  51. public $property;
  52. /**
  53. * @var \ReflectionMethod[]|\ReflectionProperty[]
  54. */
  55. private $reflMember = array();
  56. /**
  57. * Constructor.
  58. *
  59. * @param string $class The name of the class this member is defined on
  60. * @param string $name The name of the member
  61. * @param string $property The property the member belongs to
  62. */
  63. public function __construct($class, $name, $property)
  64. {
  65. $this->class = $class;
  66. $this->name = $name;
  67. $this->property = $property;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * @deprecated since version 2.5, to be removed in 3.0.
  73. */
  74. public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
  75. {
  76. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  77. $visitor->visit($this, $value, $group, $propertyPath);
  78. if ($this->isCascaded()) {
  79. $visitor->validate($value, $propagatedGroup ?: $group, $propertyPath, $this->isCollectionCascaded(), $this->isCollectionCascadedDeeply());
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function addConstraint(Constraint $constraint)
  86. {
  87. if (!in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets())) {
  88. throw new ConstraintDefinitionException(sprintf(
  89. 'The constraint %s cannot be put on properties or getters',
  90. get_class($constraint)
  91. ));
  92. }
  93. parent::addConstraint($constraint);
  94. return $this;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function __sleep()
  100. {
  101. return array_merge(parent::__sleep(), array(
  102. 'class',
  103. 'name',
  104. 'property',
  105. ));
  106. }
  107. /**
  108. * Returns the name of the member.
  109. *
  110. * @return string
  111. */
  112. public function getName()
  113. {
  114. return $this->name;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getClassName()
  120. {
  121. return $this->class;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getPropertyName()
  127. {
  128. return $this->property;
  129. }
  130. /**
  131. * Returns whether this member is public.
  132. *
  133. * @param object|string $objectOrClassName The object or the class name
  134. *
  135. * @return bool
  136. */
  137. public function isPublic($objectOrClassName)
  138. {
  139. return $this->getReflectionMember($objectOrClassName)->isPublic();
  140. }
  141. /**
  142. * Returns whether this member is protected.
  143. *
  144. * @param object|string $objectOrClassName The object or the class name
  145. *
  146. * @return bool
  147. */
  148. public function isProtected($objectOrClassName)
  149. {
  150. return $this->getReflectionMember($objectOrClassName)->isProtected();
  151. }
  152. /**
  153. * Returns whether this member is private.
  154. *
  155. * @param object|string $objectOrClassName The object or the class name
  156. *
  157. * @return bool
  158. */
  159. public function isPrivate($objectOrClassName)
  160. {
  161. return $this->getReflectionMember($objectOrClassName)->isPrivate();
  162. }
  163. /**
  164. * Returns whether objects stored in this member should be validated.
  165. *
  166. * @return bool
  167. *
  168. * @deprecated since version 2.5, to be removed in 3.0.
  169. * Use {@link getCascadingStrategy()} instead.
  170. */
  171. public function isCascaded()
  172. {
  173. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getCascadingStrategy() method instead.', E_USER_DEPRECATED);
  174. return (bool) ($this->cascadingStrategy & CascadingStrategy::CASCADE);
  175. }
  176. /**
  177. * Returns whether arrays or traversable objects stored in this member
  178. * should be traversed and validated in each entry.
  179. *
  180. * @return bool
  181. *
  182. * @deprecated since version 2.5, to be removed in 3.0.
  183. * Use {@link getTraversalStrategy()} instead.
  184. */
  185. public function isCollectionCascaded()
  186. {
  187. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
  188. return (bool) ($this->traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE));
  189. }
  190. /**
  191. * Returns whether arrays or traversable objects stored in this member
  192. * should be traversed recursively for inner arrays/traversable objects.
  193. *
  194. * @return bool
  195. *
  196. * @deprecated since version 2.5, to be removed in 3.0.
  197. * Use {@link getTraversalStrategy()} instead.
  198. */
  199. public function isCollectionCascadedDeeply()
  200. {
  201. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the getTraversalStrategy() method instead.', E_USER_DEPRECATED);
  202. return !($this->traversalStrategy & TraversalStrategy::STOP_RECURSION);
  203. }
  204. /**
  205. * Returns the reflection instance for accessing the member's value.
  206. *
  207. * @param object|string $objectOrClassName The object or the class name
  208. *
  209. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  210. */
  211. public function getReflectionMember($objectOrClassName)
  212. {
  213. $className = is_string($objectOrClassName) ? $objectOrClassName : get_class($objectOrClassName);
  214. if (!isset($this->reflMember[$className])) {
  215. $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
  216. }
  217. return $this->reflMember[$className];
  218. }
  219. /**
  220. * Creates a new reflection instance for accessing the member's value.
  221. *
  222. * Must be implemented by subclasses.
  223. *
  224. * @param object|string $objectOrClassName The object or the class name
  225. *
  226. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  227. */
  228. abstract protected function newReflectionMember($objectOrClassName);
  229. }