Drupal investigation

Validator.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 version 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. /**
  27. * @var MetadataFactoryInterface
  28. */
  29. private $metadataFactory;
  30. /**
  31. * @var ConstraintValidatorFactoryInterface
  32. */
  33. private $validatorFactory;
  34. /**
  35. * @var TranslatorInterface
  36. */
  37. private $translator;
  38. /**
  39. * @var null|string
  40. */
  41. private $translationDomain;
  42. /**
  43. * @var array
  44. */
  45. private $objectInitializers;
  46. public function __construct(
  47. MetadataFactoryInterface $metadataFactory,
  48. ConstraintValidatorFactoryInterface $validatorFactory,
  49. TranslatorInterface $translator,
  50. $translationDomain = 'validators',
  51. array $objectInitializers = array()
  52. ) {
  53. $this->metadataFactory = $metadataFactory;
  54. $this->validatorFactory = $validatorFactory;
  55. $this->translator = $translator;
  56. $this->translationDomain = $translationDomain;
  57. $this->objectInitializers = $objectInitializers;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getMetadataFactory()
  63. {
  64. return $this->metadataFactory;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getMetadataFor($value)
  70. {
  71. return $this->metadataFactory->getMetadataFor($value);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function hasMetadataFor($value)
  77. {
  78. return $this->metadataFactory->hasMetadataFor($value);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function validate($value, $groups = null, $traverse = false, $deep = false)
  84. {
  85. $visitor = $this->createVisitor($value);
  86. foreach ($this->resolveGroups($groups) as $group) {
  87. $visitor->validate($value, $group, '', $traverse, $deep);
  88. }
  89. return $visitor->getViolations();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. *
  94. * @throws ValidatorException If the metadata for the value does not support properties.
  95. */
  96. public function validateProperty($containingValue, $property, $groups = null)
  97. {
  98. $visitor = $this->createVisitor($containingValue);
  99. $metadata = $this->metadataFactory->getMetadataFor($containingValue);
  100. if (!$metadata instanceof PropertyMetadataContainerInterface) {
  101. $valueAsString = is_scalar($containingValue)
  102. ? '"'.$containingValue.'"'
  103. : 'the value of type '.gettype($containingValue);
  104. throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
  105. }
  106. foreach ($this->resolveGroups($groups) as $group) {
  107. if (!$metadata->hasPropertyMetadata($property)) {
  108. continue;
  109. }
  110. foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
  111. $propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
  112. }
  113. }
  114. return $visitor->getViolations();
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @throws ValidatorException If the metadata for the value does not support properties.
  120. */
  121. public function validatePropertyValue($containingValue, $property, $value, $groups = null)
  122. {
  123. $visitor = $this->createVisitor(is_object($containingValue) ? $containingValue : $value);
  124. $metadata = $this->metadataFactory->getMetadataFor($containingValue);
  125. if (!$metadata instanceof PropertyMetadataContainerInterface) {
  126. $valueAsString = is_scalar($containingValue)
  127. ? '"'.$containingValue.'"'
  128. : 'the value of type '.gettype($containingValue);
  129. throw new ValidatorException(sprintf('The metadata for '.$valueAsString.' does not support properties.'));
  130. }
  131. // If $containingValue is passed as class name, take $value as root
  132. // and start the traversal with an empty property path
  133. $propertyPath = is_object($containingValue) ? $property : '';
  134. foreach ($this->resolveGroups($groups) as $group) {
  135. if (!$metadata->hasPropertyMetadata($property)) {
  136. continue;
  137. }
  138. foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
  139. $propMeta->accept($visitor, $value, $group, $propertyPath);
  140. }
  141. }
  142. return $visitor->getViolations();
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function validateValue($value, $constraints, $groups = null)
  148. {
  149. $context = new ExecutionContext($this->createVisitor($value), $this->translator, $this->translationDomain);
  150. $constraints = is_array($constraints) ? $constraints : array($constraints);
  151. foreach ($constraints as $constraint) {
  152. if ($constraint instanceof Valid) {
  153. // Why can't the Valid constraint be executed directly?
  154. //
  155. // It cannot be executed like regular other constraints, because regular
  156. // constraints are only executed *if they belong to the validated group*.
  157. // The Valid constraint, on the other hand, is always executed and propagates
  158. // the group to the cascaded object. The propagated group depends on
  159. //
  160. // * Whether a group sequence is currently being executed. Then the default
  161. // group is propagated.
  162. //
  163. // * Otherwise the validated group is propagated.
  164. throw new ValidatorException(
  165. sprintf(
  166. 'The constraint %s cannot be validated. Use the method validate() instead.',
  167. get_class($constraint)
  168. )
  169. );
  170. }
  171. $context->validateValue($value, $constraint, '', $groups);
  172. }
  173. return $context->getViolations();
  174. }
  175. /**
  176. * @param mixed $root
  177. *
  178. * @return ValidationVisitor
  179. */
  180. private function createVisitor($root)
  181. {
  182. return new ValidationVisitor(
  183. $root,
  184. $this->metadataFactory,
  185. $this->validatorFactory,
  186. $this->translator,
  187. $this->translationDomain,
  188. $this->objectInitializers
  189. );
  190. }
  191. /**
  192. * @param null|string|string[] $groups
  193. *
  194. * @return string[]
  195. */
  196. private function resolveGroups($groups)
  197. {
  198. return $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP);
  199. }
  200. }