PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Validator/Mapping/ClassMetadata.php

https://bitbucket.org/vladap/symfony
PHP | 421 lines | 365 code | 12 blank | 44 comment | 1 complexity | b7ce80520c8d175471f396247948cdc3 MD5 | raw file
  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\ValidationVisitorInterface;
  12. use Symfony\Component\Validator\PropertyMetadataContainerInterface;
  13. use Symfony\Component\Validator\ClassBasedInterface;
  14. use Symfony\Component\Validator\MetadataInterface;
  15. use Symfony\Component\Validator\Constraint;
  16. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  17. use Symfony\Component\Validator\Exception\GroupDefinitionException;
  18. /**
  19. * Represents all the configured constraints on a given class.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class ClassMetadata extends ElementMetadata implements MetadataInterface, ClassBasedInterface, PropertyMetadataContainerInterface
  25. {
  26. /**
  27. * @var string
  28. */
  29. public $name;
  30. /**
  31. * @var string
  32. */
  33. public $defaultGroup;
  34. /**
  35. * @var MemberMetadata[]
  36. */
  37. public $members = array();
  38. /**
  39. * @var PropertyMetadata[]
  40. */
  41. public $properties = array();
  42. /**
  43. * @var GetterMetadata[]
  44. */
  45. public $getters = array();
  46. /**
  47. * @var array
  48. */
  49. public $groupSequence = array();
  50. /**
  51. * @var Boolean
  52. */
  53. public $groupSequenceProvider = false;
  54. /**
  55. * @var \ReflectionClass
  56. */
  57. private $reflClass;
  58. /**
  59. * Constructs a metadata for the given class
  60. *
  61. * @param string $class
  62. */
  63. public function __construct($class)
  64. {
  65. $this->name = $class;
  66. // class name without namespace
  67. if (false !== $nsSep = strrpos($class, '\\')) {
  68. $this->defaultGroup = substr($class, $nsSep + 1);
  69. } else {
  70. $this->defaultGroup = $class;
  71. }
  72. }
  73. public function accept(ValidationVisitorInterface $visitor, $value, $group, $propertyPath, $propagatedGroup = null)
  74. {
  75. if (null === $propagatedGroup && Constraint::DEFAULT_GROUP === $group
  76. && ($this->hasGroupSequence() || $this->isGroupSequenceProvider())) {
  77. if ($this->hasGroupSequence()) {
  78. $groups = $this->getGroupSequence();
  79. } else {
  80. $groups = $value->getGroupSequence();
  81. }
  82. foreach ($groups as $group) {
  83. $this->accept($visitor, $value, $group, $propertyPath, Constraint::DEFAULT_GROUP);
  84. if (count($visitor->getViolations()) > 0) {
  85. break;
  86. }
  87. }
  88. return;
  89. }
  90. $visitor->visit($this, $value, $group, $propertyPath);
  91. if (null !== $value) {
  92. $pathPrefix = empty($propertyPath) ? '' : $propertyPath.'.';
  93. foreach ($this->getConstrainedProperties() as $property) {
  94. foreach ($this->getMemberMetadatas($property) as $member) {
  95. $member->accept($visitor, $member->getPropertyValue($value), $group, $pathPrefix.$property, $propagatedGroup);
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Returns the properties to be serialized
  102. *
  103. * @return array
  104. */
  105. public function __sleep()
  106. {
  107. return array_merge(parent::__sleep(), array(
  108. 'getters',
  109. 'groupSequence',
  110. 'groupSequenceProvider',
  111. 'members',
  112. 'name',
  113. 'properties',
  114. 'defaultGroup'
  115. ));
  116. }
  117. /**
  118. * Returns the fully qualified name of the class
  119. *
  120. * @return string The fully qualified class name
  121. */
  122. public function getClassName()
  123. {
  124. return $this->name;
  125. }
  126. /**
  127. * Returns the name of the default group for this class
  128. *
  129. * For each class, the group "Default" is an alias for the group
  130. * "<ClassName>", where <ClassName> is the non-namespaced name of the
  131. * class. All constraints implicitly or explicitly assigned to group
  132. * "Default" belong to both of these groups, unless the class defines
  133. * a group sequence.
  134. *
  135. * If a class defines a group sequence, validating the class in "Default"
  136. * will validate the group sequence. The constraints assigned to "Default"
  137. * can still be validated by validating the class in "<ClassName>".
  138. *
  139. * @return string The name of the default group
  140. */
  141. public function getDefaultGroup()
  142. {
  143. return $this->defaultGroup;
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function addConstraint(Constraint $constraint)
  149. {
  150. if (!in_array(Constraint::CLASS_CONSTRAINT, (array) $constraint->getTargets())) {
  151. throw new ConstraintDefinitionException(sprintf(
  152. 'The constraint %s cannot be put on classes',
  153. get_class($constraint)
  154. ));
  155. }
  156. $constraint->addImplicitGroupName($this->getDefaultGroup());
  157. parent::addConstraint($constraint);
  158. }
  159. /**
  160. * Adds a constraint to the given property.
  161. *
  162. * @param string $property The name of the property
  163. * @param Constraint $constraint The constraint
  164. *
  165. * @return ClassMetadata This object
  166. */
  167. public function addPropertyConstraint($property, Constraint $constraint)
  168. {
  169. if (!isset($this->properties[$property])) {
  170. $this->properties[$property] = new PropertyMetadata($this->getClassName(), $property);
  171. $this->addMemberMetadata($this->properties[$property]);
  172. }
  173. $constraint->addImplicitGroupName($this->getDefaultGroup());
  174. $this->properties[$property]->addConstraint($constraint);
  175. return $this;
  176. }
  177. /**
  178. * Adds a constraint to the getter of the given property.
  179. *
  180. * The name of the getter is assumed to be the name of the property with an
  181. * uppercased first letter and either the prefix "get" or "is".
  182. *
  183. * @param string $property The name of the property
  184. * @param Constraint $constraint The constraint
  185. *
  186. * @return ClassMetadata This object
  187. */
  188. public function addGetterConstraint($property, Constraint $constraint)
  189. {
  190. if (!isset($this->getters[$property])) {
  191. $this->getters[$property] = new GetterMetadata($this->getClassName(), $property);
  192. $this->addMemberMetadata($this->getters[$property]);
  193. }
  194. $constraint->addImplicitGroupName($this->getDefaultGroup());
  195. $this->getters[$property]->addConstraint($constraint);
  196. return $this;
  197. }
  198. /**
  199. * Merges the constraints of the given metadata into this object.
  200. *
  201. * @param ClassMetadata $source The source metadata
  202. */
  203. public function mergeConstraints(ClassMetadata $source)
  204. {
  205. foreach ($source->getConstraints() as $constraint) {
  206. $this->addConstraint(clone $constraint);
  207. }
  208. foreach ($source->getConstrainedProperties() as $property) {
  209. foreach ($source->getMemberMetadatas($property) as $member) {
  210. $member = clone $member;
  211. foreach ($member->getConstraints() as $constraint) {
  212. $constraint->addImplicitGroupName($this->getDefaultGroup());
  213. }
  214. $this->addMemberMetadata($member);
  215. if (!$member->isPrivate($this->name)) {
  216. $property = $member->getPropertyName();
  217. if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
  218. $this->properties[$property] = $member;
  219. } elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
  220. $this->getters[$property] = $member;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. /**
  227. * Adds a member metadata.
  228. *
  229. * @param MemberMetadata $metadata
  230. */
  231. protected function addMemberMetadata(MemberMetadata $metadata)
  232. {
  233. $property = $metadata->getPropertyName();
  234. $this->members[$property][] = $metadata;
  235. }
  236. /**
  237. * Returns true if metadatas of members is present for the given property.
  238. *
  239. * @param string $property The name of the property
  240. *
  241. * @return Boolean
  242. */
  243. public function hasMemberMetadatas($property)
  244. {
  245. return array_key_exists($property, $this->members);
  246. }
  247. /**
  248. * Returns all metadatas of members describing the given property.
  249. *
  250. * @param string $property The name of the property
  251. *
  252. * @return MemberMetadata[] An array of MemberMetadata
  253. */
  254. public function getMemberMetadatas($property)
  255. {
  256. return $this->members[$property];
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function hasPropertyMetadata($property)
  262. {
  263. return array_key_exists($property, $this->members);
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function getPropertyMetadata($property)
  269. {
  270. return $this->members[$property];
  271. }
  272. /**
  273. * Returns all properties for which constraints are defined.
  274. *
  275. * @return array An array of property names
  276. */
  277. public function getConstrainedProperties()
  278. {
  279. return array_keys($this->members);
  280. }
  281. /**
  282. * Sets the default group sequence for this class.
  283. *
  284. * @param array $groups An array of group names
  285. *
  286. * @return ClassMetadata
  287. *
  288. * @throws GroupDefinitionException
  289. */
  290. public function setGroupSequence(array $groups)
  291. {
  292. if ($this->isGroupSequenceProvider()) {
  293. throw new GroupDefinitionException('Defining a static group sequence is not allowed with a group sequence provider');
  294. }
  295. if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
  296. throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
  297. }
  298. if (!in_array($this->getDefaultGroup(), $groups, true)) {
  299. throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
  300. }
  301. $this->groupSequence = $groups;
  302. return $this;
  303. }
  304. /**
  305. * Returns whether this class has an overridden default group sequence.
  306. *
  307. * @return Boolean
  308. */
  309. public function hasGroupSequence()
  310. {
  311. return count($this->groupSequence) > 0;
  312. }
  313. /**
  314. * Returns the default group sequence for this class.
  315. *
  316. * @return array An array of group names
  317. */
  318. public function getGroupSequence()
  319. {
  320. return $this->groupSequence;
  321. }
  322. /**
  323. * Returns a ReflectionClass instance for this class.
  324. *
  325. * @return \ReflectionClass
  326. */
  327. public function getReflectionClass()
  328. {
  329. if (!$this->reflClass) {
  330. $this->reflClass = new \ReflectionClass($this->getClassName());
  331. }
  332. return $this->reflClass;
  333. }
  334. /**
  335. * Sets whether a group sequence provider should be used.
  336. *
  337. * @param Boolean $active
  338. *
  339. * @throws GroupDefinitionException
  340. */
  341. public function setGroupSequenceProvider($active)
  342. {
  343. if ($this->hasGroupSequence()) {
  344. throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
  345. }
  346. if (!$this->getReflectionClass()->implementsInterface('Symfony\Component\Validator\GroupSequenceProviderInterface')) {
  347. throw new GroupDefinitionException(sprintf('Class "%s" must implement GroupSequenceProviderInterface', $this->name));
  348. }
  349. $this->groupSequenceProvider = $active;
  350. }
  351. /**
  352. * Returns whether the class is a group sequence provider.
  353. *
  354. * @return Boolean
  355. */
  356. public function isGroupSequenceProvider()
  357. {
  358. return $this->groupSequenceProvider;
  359. }
  360. }