ClassMetadata.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\Exception\GroupDefinitionException;
  14. /**
  15. * Represents all the configured constraints on a given class.
  16. *
  17. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ClassMetadata extends ElementMetadata
  21. {
  22. public $name;
  23. public $defaultGroup;
  24. public $members = array();
  25. public $properties = array();
  26. public $getters = array();
  27. public $groupSequence = array();
  28. private $reflClass;
  29. /**
  30. * Constructs a metadata for the given class
  31. *
  32. * @param string $class
  33. */
  34. public function __construct($class)
  35. {
  36. $this->name = $class;
  37. // class name without namespace
  38. if (false !== $nsSep = strrpos($class, '\\')) {
  39. $this->defaultGroup = substr($class, $nsSep + 1);
  40. } else {
  41. $this->defaultGroup = $class;
  42. }
  43. }
  44. /**
  45. * Returns the properties to be serialized
  46. *
  47. * @return array
  48. */
  49. public function __sleep()
  50. {
  51. return array_merge(parent::__sleep(), array(
  52. 'getters',
  53. 'groupSequence',
  54. 'members',
  55. 'name',
  56. 'properties',
  57. 'defaultGroup'
  58. ));
  59. }
  60. /**
  61. * Returns the fully qualified name of the class
  62. *
  63. * @return string The fully qualified class name
  64. */
  65. public function getClassName()
  66. {
  67. return $this->name;
  68. }
  69. /**
  70. * Returns the name of the default group for this class
  71. *
  72. * For each class, the group "Default" is an alias for the group
  73. * "<ClassName>", where <ClassName> is the non-namespaced name of the
  74. * class. All constraints implicitly or explicitly assigned to group
  75. * "Default" belong to both of these groups, unless the class defines
  76. * a group sequence.
  77. *
  78. * If a class defines a group sequence, validating the class in "Default"
  79. * will validate the group sequence. The constraints assigned to "Default"
  80. * can still be validated by validating the class in "<ClassName>".
  81. *
  82. * @return string The name of the default group
  83. */
  84. public function getDefaultGroup()
  85. {
  86. return $this->defaultGroup;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function addConstraint(Constraint $constraint)
  92. {
  93. if (!in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
  94. throw new ConstraintDefinitionException(sprintf(
  95. 'The constraint %s cannot be put on classes',
  96. get_class($constraint)
  97. ));
  98. }
  99. $constraint->addImplicitGroupName($this->getDefaultGroup());
  100. parent::addConstraint($constraint);
  101. }
  102. /**
  103. * Adds a constraint to the given property.
  104. *
  105. * @param string $property The name of the property
  106. * @param Constraint $constraint The constraint
  107. *
  108. * @return ClassMetadata This object
  109. */
  110. public function addPropertyConstraint($property, Constraint $constraint)
  111. {
  112. if (!isset($this->properties[$property])) {
  113. $this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
  114. $this->addMemberMetadata($this->properties[$property]);
  115. }
  116. $constraint->addImplicitGroupName($this->getDefaultGroup());
  117. $this->properties[$property]->addConstraint($constraint);
  118. return $this;
  119. }
  120. /**
  121. * Adds a constraint to the getter of the given property.
  122. *
  123. * The name of the getter is assumed to be the name of the property with an
  124. * uppercased first letter and either the prefix "get" or "is".
  125. *
  126. * @param string $property The name of the property
  127. * @param Constraint $constraint The constraint
  128. *
  129. * @return ClassMetadata This object
  130. */
  131. public function addGetterConstraint($property, Constraint $constraint)
  132. {
  133. if (!isset($this->getters[$property])) {
  134. $this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
  135. $this->addMemberMetadata($this->getters[$property]);
  136. }
  137. $constraint->addImplicitGroupName($this->getDefaultGroup());
  138. $this->getters[$property]->addConstraint($constraint);
  139. return $this;
  140. }
  141. /**
  142. * Merges the constraints of the given metadata into this object.
  143. *
  144. * @param ClassMetadata $source The source metadata
  145. */
  146. public function mergeConstraints(ClassMetadata $source)
  147. {
  148. foreach ($source->getConstraints() as $constraint) {
  149. $this->addConstraint(clone $constraint);
  150. }
  151. foreach ($source->getConstrainedProperties() as $property) {
  152. foreach ($source->getMemberMetadatas($property) as $member) {
  153. $member = clone $member;
  154. foreach ($member->getConstraints() as $constraint) {
  155. $constraint->addImplicitGroupName($this->getDefaultGroup());
  156. }
  157. $this->addMemberMetadata($member);
  158. if (!$member->isPrivate()) {
  159. $property = $member->getPropertyName();
  160. if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
  161. $this->properties[$property] = $member;
  162. } else if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
  163. $this->getters[$property] = $member;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * Adds a member metadata
  171. *
  172. * @param MemberMetadata $metadata
  173. */
  174. protected function addMemberMetadata(MemberMetadata $metadata)
  175. {
  176. $property = $metadata->getPropertyName();
  177. $this->members[$property][] = $metadata;
  178. }
  179. /**
  180. * Returns true if metadatas of members is present for the given property.
  181. *
  182. * @param string $property The name of the property
  183. *
  184. * @return Boolean
  185. */
  186. public function hasMemberMetadatas($property)
  187. {
  188. return array_key_exists($property, $this->members);
  189. }
  190. /**
  191. * Returns all metadatas of members describing the given property
  192. *
  193. * @param string $property The name of the property
  194. * @array of MemberMetadata
  195. */
  196. public function getMemberMetadatas($property)
  197. {
  198. return $this->members[$property];
  199. }
  200. /**
  201. * Returns all properties for which constraints are defined.
  202. *
  203. * @return array An array of property names
  204. */
  205. public function getConstrainedProperties()
  206. {
  207. return array_keys($this->members);
  208. }
  209. /**
  210. * Sets the default group sequence for this class.
  211. *
  212. * @param array $groups An array of group names
  213. */
  214. public function setGroupSequence(array $groups)
  215. {
  216. if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
  217. throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
  218. }
  219. if (!in_array($this->getDefaultGroup(), $groups, true)) {
  220. throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
  221. }
  222. $this->groupSequence = $groups;
  223. return $this;
  224. }
  225. /**
  226. * Returns whether this class has an overridden default group sequence.
  227. *
  228. * @return Boolean
  229. */
  230. public function hasGroupSequence()
  231. {
  232. return count($this->groupSequence) > 0;
  233. }
  234. /**
  235. * Returns the default group sequence for this class.
  236. *
  237. * @return array An array of group names
  238. */
  239. public function getGroupSequence()
  240. {
  241. return $this->groupSequence;
  242. }
  243. /**
  244. * Returns a ReflectionClass instance for this class.
  245. *
  246. * @return ReflectionClass
  247. */
  248. public function getReflectionClass()
  249. {
  250. if (!$this->reflClass) {
  251. $this->reflClass = new \ReflectionClass($this->getClassName());
  252. }
  253. return $this->reflClass;
  254. }
  255. }