Validator.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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;
  11. @trigger_error('The '.__NAMESPACE__.'\Validator class is deprecated since Symfony 2.5 and will be removed in 3.0. Use the Symfony\Component\Validator\Validator\RecursiveValidator class instead.', E_USER_DEPRECATED);
  12. use Symfony\Component\Translation\TranslatorInterface;
  13. use Symfony\Component\Validator\Constraints\Valid;
  14. use Symfony\Component\Validator\Exception\ValidatorException;
  15. /**
  16. * Default implementation of {@link ValidatorInterface}.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @deprecated since version 2.5, to be removed in 3.0.
  22. * Use {@link Validator\RecursiveValidator} instead.
  23. */
  24. class Validator implements ValidatorInterface, Mapping\Factory\MetadataFactoryInterface
  25. {
  26. private $metadataFactory;
  27. private $validatorFactory;
  28. private $translator;
  29. private $translationDomain;
  30. private $objectInitializers;
  31. public function __construct(MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, TranslatorInterface $translator, $translationDomain = 'validators', array $objectInitializers = array())
  32. {
  33. $this->metadataFactory = $metadataFactory;
  34. $this->validatorFactory = $validatorFactory;
  35. $this->translator = $translator;
  36. $this->translationDomain = $translationDomain;
  37. $this->objectInitializers = $objectInitializers;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getMetadataFactory()
  43. {
  44. return $this->metadataFactory;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getMetadataFor($value)
  50. {
  51. return $this->metadataFactory->getMetadataFor($value);
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function hasMetadataFor($value)
  57. {
  58. return $this->metadataFactory->hasMetadataFor($value);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function validate($value, $groups = null, $traverse = false, $deep = false)
  64. {
  65. $visitor = $this->createVisitor($value);
  66. foreach ($this->resolveGroups($groups) as $group) {
  67. $visitor->validate($value, $group, '', $traverse, $deep);
  68. }
  69. return $visitor->getViolations();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. *
  74. * @throws ValidatorException if the metadata for the value does not support properties
  75. */
  76. public function validateProperty($containingValue, $property, $groups = null)
  77. {
  78. $visitor = $this->createVisitor($containingValue);
  79. $metadata = $this->metadataFactory->getMetadataFor($containingValue);
  80. if (!$metadata instanceof PropertyMetadataContainerInterface) {
  81. $valueAsString = is_scalar($containingValue)
  82. ? '"'.$containingValue.'"'
  83. : 'the value of type '.\gettype($containingValue);
  84. throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
  85. }
  86. foreach ($this->resolveGroups($groups) as $group) {
  87. if (!$metadata->hasPropertyMetadata($property)) {
  88. continue;
  89. }
  90. foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
  91. $propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
  92. }
  93. }
  94. return $visitor->getViolations();
  95. }
  96. /**
  97. * {@inheritdoc}
  98. *
  99. * @throws ValidatorException if the metadata for the value does not support properties
  100. */
  101. public function validatePropertyValue($containingValue, $property, $value, $groups = null)
  102. {
  103. $visitor = $this->createVisitor(\is_object($containingValue) ? $containingValue : $value);
  104. $metadata = $this->metadataFactory->getMetadataFor($containingValue);
  105. if (!$metadata instanceof PropertyMetadataContainerInterface) {
  106. $valueAsString = is_scalar($containingValue)
  107. ? '"'.$containingValue.'"'
  108. : 'the value of type '.\gettype($containingValue);
  109. throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
  110. }
  111. // If $containingValue is passed as class name, take $value as root
  112. // and start the traversal with an empty property path
  113. $propertyPath = \is_object($containingValue) ? $property : '';
  114. foreach ($this->resolveGroups($groups) as $group) {
  115. if (!$metadata->hasPropertyMetadata($property)) {
  116. continue;
  117. }
  118. foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
  119. $propMeta->accept($visitor, $value, $group, $propertyPath);
  120. }
  121. }
  122. return $visitor->getViolations();
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function validateValue($value, $constraints, $groups = null)
  128. {
  129. $context = new ExecutionContext($this->createVisitor($value), $this->translator, $this->translationDomain);
  130. $constraints = \is_array($constraints) ? $constraints : array($constraints);
  131. foreach ($constraints as $constraint) {
  132. if ($constraint instanceof Valid) {
  133. // Why can't the Valid constraint be executed directly?
  134. //
  135. // It cannot be executed like regular other constraints, because regular
  136. // constraints are only executed *if they belong to the validated group*.
  137. // The Valid constraint, on the other hand, is always executed and propagates
  138. // the group to the cascaded object. The propagated group depends on
  139. //
  140. // * Whether a group sequence is currently being executed. Then the default
  141. // group is propagated.
  142. //
  143. // * Otherwise the validated group is propagated.
  144. throw new ValidatorException(sprintf('The constraint %s cannot be validated. Use the method validate() instead.', \get_class($constraint)));
  145. }
  146. $context->validateValue($value, $constraint, '', $groups);
  147. }
  148. return $context->getViolations();
  149. }
  150. /**
  151. * @param mixed $root
  152. *
  153. * @return ValidationVisitor
  154. */
  155. private function createVisitor($root)
  156. {
  157. return new ValidationVisitor(
  158. $root,
  159. $this->metadataFactory,
  160. $this->validatorFactory,
  161. $this->translator,
  162. $this->translationDomain,
  163. $this->objectInitializers
  164. );
  165. }
  166. /**
  167. * @param string|string[]|null $groups
  168. *
  169. * @return string[]
  170. */
  171. private function resolveGroups($groups)
  172. {
  173. return $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP);
  174. }
  175. }