ExecutionContext.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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__.'\ExecutionContext class is deprecated since Symfony 2.5 and will be removed in 3.0. Use the Symfony\Component\Validator\Context\ExecutionContext class instead.', E_USER_DEPRECATED);
  12. use Symfony\Component\Translation\TranslatorInterface;
  13. /**
  14. * Default implementation of {@link ExecutionContextInterface}.
  15. *
  16. * This class is immutable by design.
  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 Context\ExecutionContext} instead.
  23. */
  24. class ExecutionContext implements ExecutionContextInterface
  25. {
  26. private $globalContext;
  27. private $translator;
  28. private $translationDomain;
  29. private $metadata;
  30. private $value;
  31. private $group;
  32. private $propertyPath;
  33. /**
  34. * Creates a new execution context.
  35. *
  36. * @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state
  37. * @param TranslatorInterface $translator The translator for translating violation messages
  38. * @param string|null $translationDomain The domain of the validation messages
  39. * @param MetadataInterface $metadata The metadata of the validated node
  40. * @param mixed $value The value of the validated node
  41. * @param string $group The current validation group
  42. * @param string $propertyPath The property path to the current node
  43. */
  44. public function __construct(GlobalExecutionContextInterface $globalContext, TranslatorInterface $translator, $translationDomain = null, MetadataInterface $metadata = null, $value = null, $group = null, $propertyPath = '')
  45. {
  46. if (null === $group) {
  47. $group = Constraint::DEFAULT_GROUP;
  48. }
  49. $this->globalContext = $globalContext;
  50. $this->translator = $translator;
  51. $this->translationDomain = $translationDomain;
  52. $this->metadata = $metadata;
  53. $this->value = $value;
  54. $this->propertyPath = $propertyPath;
  55. $this->group = $group;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function addViolation($message, array $params = array(), $invalidValue = null, $plural = null, $code = null)
  61. {
  62. if (null === $plural) {
  63. $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
  64. } else {
  65. try {
  66. $translatedMessage = $this->translator->transChoice($message, $plural, $params, $this->translationDomain);
  67. } catch (\InvalidArgumentException $e) {
  68. $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
  69. }
  70. }
  71. $this->globalContext->getViolations()->add(new ConstraintViolation(
  72. $translatedMessage,
  73. $message,
  74. $params,
  75. $this->globalContext->getRoot(),
  76. $this->propertyPath,
  77. // check using func_num_args() to allow passing null values
  78. \func_num_args() >= 3 ? $invalidValue : $this->value,
  79. $plural,
  80. $code
  81. ));
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
  87. {
  88. $this->globalContext->getViolations()->add(new ConstraintViolation(
  89. null === $plural
  90. ? $this->translator->trans($message, $parameters, $this->translationDomain)
  91. : $this->translator->transChoice($message, $plural, $parameters, $this->translationDomain),
  92. $message,
  93. $parameters,
  94. $this->globalContext->getRoot(),
  95. $this->getPropertyPath($subPath),
  96. // check using func_num_args() to allow passing null values
  97. \func_num_args() >= 4 ? $invalidValue : $this->value,
  98. $plural,
  99. $code
  100. ));
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getViolations()
  106. {
  107. return $this->globalContext->getViolations();
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getRoot()
  113. {
  114. return $this->globalContext->getRoot();
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getPropertyPath($subPath = '')
  120. {
  121. if ('' != $subPath && '' !== $this->propertyPath && '[' !== $subPath[0]) {
  122. return $this->propertyPath.'.'.$subPath;
  123. }
  124. return $this->propertyPath.$subPath;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getClassName()
  130. {
  131. if ($this->metadata instanceof ClassBasedInterface) {
  132. return $this->metadata->getClassName();
  133. }
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getPropertyName()
  139. {
  140. if ($this->metadata instanceof PropertyMetadataInterface) {
  141. return $this->metadata->getPropertyName();
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getValue()
  148. {
  149. return $this->value;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getGroup()
  155. {
  156. return $this->group;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getMetadata()
  162. {
  163. return $this->metadata;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function getMetadataFor($value)
  169. {
  170. return $this->globalContext->getMetadataFactory()->getMetadataFor($value);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
  176. {
  177. $propertyPath = $this->getPropertyPath($subPath);
  178. $visitor = $this->globalContext->getVisitor();
  179. foreach ($this->resolveGroups($groups) as $group) {
  180. $visitor->validate($value, $group, $propertyPath, $traverse, $deep);
  181. }
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function validateValue($value, $constraints, $subPath = '', $groups = null)
  187. {
  188. $constraints = \is_array($constraints) ? $constraints : array($constraints);
  189. if (null === $groups && '' === $subPath) {
  190. $context = clone $this;
  191. $context->value = $value;
  192. $context->executeConstraintValidators($value, $constraints);
  193. return;
  194. }
  195. $propertyPath = $this->getPropertyPath($subPath);
  196. foreach ($this->resolveGroups($groups) as $group) {
  197. $context = clone $this;
  198. $context->value = $value;
  199. $context->group = $group;
  200. $context->propertyPath = $propertyPath;
  201. $context->executeConstraintValidators($value, $constraints);
  202. }
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function getMetadataFactory()
  208. {
  209. return $this->globalContext->getMetadataFactory();
  210. }
  211. /**
  212. * Executes the validators of the given constraints for the given value.
  213. *
  214. * @param mixed $value The value to validate
  215. * @param Constraint[] $constraints The constraints to match against
  216. */
  217. private function executeConstraintValidators($value, array $constraints)
  218. {
  219. foreach ($constraints as $constraint) {
  220. $validator = $this->globalContext->getValidatorFactory()->getInstance($constraint);
  221. $validator->initialize($this);
  222. $validator->validate($value, $constraint);
  223. }
  224. }
  225. /**
  226. * Returns an array of group names.
  227. *
  228. * @param string|string[]|null $groups The groups to resolve. If a single string is
  229. * passed, it is converted to an array. If null
  230. * is passed, an array containing the current
  231. * group of the context is returned.
  232. *
  233. * @return array An array of validation groups
  234. */
  235. private function resolveGroups($groups)
  236. {
  237. return $groups ? (array) $groups : (array) $this->group;
  238. }
  239. }