ExecutionContext.php 12 KB

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