ExecutionContext.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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\Context;
  11. use Symfony\Component\Translation\TranslatorInterface;
  12. use Symfony\Component\Validator\ClassBasedInterface;
  13. use Symfony\Component\Validator\Constraint;
  14. use Symfony\Component\Validator\Constraints\Valid;
  15. use Symfony\Component\Validator\ConstraintViolation;
  16. use Symfony\Component\Validator\ConstraintViolationList;
  17. use Symfony\Component\Validator\Mapping\MetadataInterface;
  18. use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
  19. use Symfony\Component\Validator\Util\PropertyPath;
  20. use Symfony\Component\Validator\Validator\ValidatorInterface;
  21. use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
  22. use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
  23. /**
  24. * The context used and created by {@link ExecutionContextFactory}.
  25. *
  26. * @since 2.5
  27. *
  28. * @author Bernhard Schussek <bschussek@gmail.com>
  29. *
  30. * @see ExecutionContextInterface
  31. *
  32. * @internal You should not instantiate or use this class. Code against
  33. * {@link ExecutionContextInterface} instead.
  34. */
  35. class ExecutionContext implements ExecutionContextInterface
  36. {
  37. /**
  38. * @var ValidatorInterface
  39. */
  40. private $validator;
  41. /**
  42. * The root value of the validated object graph.
  43. *
  44. * @var mixed
  45. */
  46. private $root;
  47. /**
  48. * @var TranslatorInterface
  49. */
  50. private $translator;
  51. /**
  52. * @var string
  53. */
  54. private $translationDomain;
  55. /**
  56. * The violations generated in the current context.
  57. *
  58. * @var ConstraintViolationList
  59. */
  60. private $violations;
  61. /**
  62. * The currently validated value.
  63. *
  64. * @var mixed
  65. */
  66. private $value;
  67. /**
  68. * The currently validated object.
  69. *
  70. * @var object|null
  71. */
  72. private $object;
  73. /**
  74. * The property path leading to the current value.
  75. *
  76. * @var string
  77. */
  78. private $propertyPath = '';
  79. /**
  80. * The current validation metadata.
  81. *
  82. * @var MetadataInterface|null
  83. */
  84. private $metadata;
  85. /**
  86. * The currently validated group.
  87. *
  88. * @var string|null
  89. */
  90. private $group;
  91. /**
  92. * The currently validated constraint.
  93. *
  94. * @var Constraint|null
  95. */
  96. private $constraint;
  97. /**
  98. * Stores which objects have been validated in which group.
  99. *
  100. * @var array
  101. */
  102. private $validatedObjects = array();
  103. /**
  104. * Stores which class constraint has been validated for which object.
  105. *
  106. * @var array
  107. */
  108. private $validatedConstraints = array();
  109. /**
  110. * Stores which objects have been initialized.
  111. *
  112. * @var array
  113. */
  114. private $initializedObjects;
  115. /**
  116. * Creates a new execution context.
  117. *
  118. * @param ValidatorInterface $validator The validator
  119. * @param mixed $root The root value of the
  120. * validated object graph
  121. * @param TranslatorInterface $translator The translator
  122. * @param string|null $translationDomain The translation domain to
  123. * use for translating
  124. * violation messages
  125. *
  126. * @internal Called by {@link ExecutionContextFactory}. Should not be used
  127. * in user code.
  128. */
  129. public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
  130. {
  131. $this->validator = $validator;
  132. $this->root = $root;
  133. $this->translator = $translator;
  134. $this->translationDomain = $translationDomain;
  135. $this->violations = new ConstraintViolationList();
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
  141. {
  142. $this->value = $value;
  143. $this->object = $object;
  144. $this->metadata = $metadata;
  145. $this->propertyPath = (string) $propertyPath;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function setGroup($group)
  151. {
  152. $this->group = $group;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function setConstraint(Constraint $constraint)
  158. {
  159. $this->constraint = $constraint;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
  165. {
  166. // The parameters $invalidValue and following are ignored by the new
  167. // API, as they are not present in the new interface anymore.
  168. // You should use buildViolation() instead.
  169. if (func_num_args() > 2) {
  170. @trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
  171. $this
  172. ->buildViolation($message, $parameters)
  173. ->setInvalidValue($invalidValue)
  174. ->setPlural($plural)
  175. ->setCode($code)
  176. ->addViolation()
  177. ;
  178. return;
  179. }
  180. $this->violations->add(new ConstraintViolation(
  181. $this->translator->trans($message, $parameters, $this->translationDomain),
  182. $message,
  183. $parameters,
  184. $this->root,
  185. $this->propertyPath,
  186. $this->value,
  187. null,
  188. null,
  189. $this->constraint
  190. ));
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function buildViolation($message, array $parameters = array())
  196. {
  197. return new ConstraintViolationBuilder(
  198. $this->violations,
  199. $this->constraint,
  200. $message,
  201. $parameters,
  202. $this->root,
  203. $this->propertyPath,
  204. $this->value,
  205. $this->translator,
  206. $this->translationDomain
  207. );
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function getViolations()
  213. {
  214. return $this->violations;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function getValidator()
  220. {
  221. return $this->validator;
  222. }
  223. /**
  224. * {@inheritdoc}
  225. */
  226. public function getRoot()
  227. {
  228. return $this->root;
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function getValue()
  234. {
  235. return $this->value;
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getObject()
  241. {
  242. return $this->object;
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getMetadata()
  248. {
  249. return $this->metadata;
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. public function getGroup()
  255. {
  256. return $this->group;
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function getClassName()
  262. {
  263. return $this->metadata instanceof ClassBasedInterface ? $this->metadata->getClassName() : null;
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function getPropertyName()
  269. {
  270. return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
  271. }
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function getPropertyPath($subPath = '')
  276. {
  277. return PropertyPath::append($this->propertyPath, $subPath);
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
  283. {
  284. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
  285. if (func_num_args() > 2) {
  286. $this
  287. ->buildViolation($message, $parameters)
  288. ->atPath($subPath)
  289. ->setInvalidValue($invalidValue)
  290. ->setPlural($plural)
  291. ->setCode($code)
  292. ->addViolation()
  293. ;
  294. return;
  295. }
  296. $this
  297. ->buildViolation($message, $parameters)
  298. ->atPath($subPath)
  299. ->addViolation()
  300. ;
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
  306. {
  307. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
  308. if (is_array($value)) {
  309. // The $traverse flag is ignored for arrays
  310. $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
  311. return $this
  312. ->getValidator()
  313. ->inContext($this)
  314. ->atPath($subPath)
  315. ->validate($value, $constraint, $groups)
  316. ;
  317. }
  318. if ($traverse && $value instanceof \Traversable) {
  319. $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
  320. return $this
  321. ->getValidator()
  322. ->inContext($this)
  323. ->atPath($subPath)
  324. ->validate($value, $constraint, $groups)
  325. ;
  326. }
  327. return $this
  328. ->getValidator()
  329. ->inContext($this)
  330. ->atPath($subPath)
  331. ->validate($value, null, $groups)
  332. ;
  333. }
  334. /**
  335. * {@inheritdoc}
  336. */
  337. public function validateValue($value, $constraints, $subPath = '', $groups = null)
  338. {
  339. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
  340. return $this
  341. ->getValidator()
  342. ->inContext($this)
  343. ->atPath($subPath)
  344. ->validate($value, $constraints, $groups)
  345. ;
  346. }
  347. /**
  348. * {@inheritdoc}
  349. */
  350. public function getMetadataFactory()
  351. {
  352. @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
  353. $validator = $this->getValidator();
  354. if ($validator instanceof LegacyValidatorInterface) {
  355. return $validator->getMetadataFactory();
  356. }
  357. // The ValidatorInterface extends from the deprecated MetadataFactoryInterface, so return it when we don't have the factory instance itself
  358. return $validator;
  359. }
  360. /**
  361. * {@inheritdoc}
  362. */
  363. public function markGroupAsValidated($cacheKey, $groupHash)
  364. {
  365. if (!isset($this->validatedObjects[$cacheKey])) {
  366. $this->validatedObjects[$cacheKey] = array();
  367. }
  368. $this->validatedObjects[$cacheKey][$groupHash] = true;
  369. }
  370. /**
  371. * {@inheritdoc}
  372. */
  373. public function isGroupValidated($cacheKey, $groupHash)
  374. {
  375. return isset($this->validatedObjects[$cacheKey][$groupHash]);
  376. }
  377. /**
  378. * {@inheritdoc}
  379. */
  380. public function markConstraintAsValidated($cacheKey, $constraintHash)
  381. {
  382. $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
  383. }
  384. /**
  385. * {@inheritdoc}
  386. */
  387. public function isConstraintValidated($cacheKey, $constraintHash)
  388. {
  389. return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
  390. }
  391. /**
  392. * {@inheritdoc}
  393. */
  394. public function markObjectAsInitialized($cacheKey)
  395. {
  396. $this->initializedObjects[$cacheKey] = true;
  397. }
  398. /**
  399. * {@inheritdoc}
  400. */
  401. public function isObjectInitialized($cacheKey)
  402. {
  403. return isset($this->initializedObjects[$cacheKey]);
  404. }
  405. }