MemberMetadata.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Constraints\Composite;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  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 GenericMetadata 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 = [];
  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(string $class, string $name, string $property)
  56. {
  57. $this->class = $class;
  58. $this->name = $name;
  59. $this->property = $property;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function addConstraint(Constraint $constraint)
  65. {
  66. $this->checkConstraint($constraint);
  67. parent::addConstraint($constraint);
  68. return $this;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function __sleep()
  74. {
  75. return array_merge(parent::__sleep(), [
  76. 'class',
  77. 'name',
  78. 'property',
  79. ]);
  80. }
  81. /**
  82. * Returns the name of the member.
  83. *
  84. * @return string
  85. */
  86. public function getName()
  87. {
  88. return $this->name;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getClassName()
  94. {
  95. return $this->class;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getPropertyName()
  101. {
  102. return $this->property;
  103. }
  104. /**
  105. * Returns whether this member is public.
  106. *
  107. * @param object|string $objectOrClassName The object or the class name
  108. *
  109. * @return bool
  110. */
  111. public function isPublic($objectOrClassName)
  112. {
  113. return $this->getReflectionMember($objectOrClassName)->isPublic();
  114. }
  115. /**
  116. * Returns whether this member is protected.
  117. *
  118. * @param object|string $objectOrClassName The object or the class name
  119. *
  120. * @return bool
  121. */
  122. public function isProtected($objectOrClassName)
  123. {
  124. return $this->getReflectionMember($objectOrClassName)->isProtected();
  125. }
  126. /**
  127. * Returns whether this member is private.
  128. *
  129. * @param object|string $objectOrClassName The object or the class name
  130. *
  131. * @return bool
  132. */
  133. public function isPrivate($objectOrClassName)
  134. {
  135. return $this->getReflectionMember($objectOrClassName)->isPrivate();
  136. }
  137. /**
  138. * Returns the reflection instance for accessing the member's value.
  139. *
  140. * @param object|string $objectOrClassName The object or the class name
  141. *
  142. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  143. */
  144. public function getReflectionMember($objectOrClassName)
  145. {
  146. $className = \is_string($objectOrClassName) ? $objectOrClassName : \get_class($objectOrClassName);
  147. if (!isset($this->reflMember[$className])) {
  148. $this->reflMember[$className] = $this->newReflectionMember($objectOrClassName);
  149. }
  150. return $this->reflMember[$className];
  151. }
  152. /**
  153. * Creates a new reflection instance for accessing the member's value.
  154. *
  155. * Must be implemented by subclasses.
  156. *
  157. * @param object|string $objectOrClassName The object or the class name
  158. *
  159. * @return \ReflectionMethod|\ReflectionProperty The reflection instance
  160. */
  161. abstract protected function newReflectionMember($objectOrClassName);
  162. private function checkConstraint(Constraint $constraint)
  163. {
  164. if (!\in_array(Constraint::PROPERTY_CONSTRAINT, (array) $constraint->getTargets(), true)) {
  165. throw new ConstraintDefinitionException(sprintf('The constraint "%s" cannot be put on properties or getters.', get_debug_type($constraint)));
  166. }
  167. if ($constraint instanceof Composite) {
  168. foreach ($constraint->getNestedContraints() as $nestedContraint) {
  169. $this->checkConstraint($nestedContraint);
  170. }
  171. }
  172. }
  173. }