ExecutionContextInterface.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\Validator\Constraint;
  12. use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
  13. use Symfony\Component\Validator\Mapping\MetadataInterface;
  14. use Symfony\Component\Validator\Validator\ValidatorInterface;
  15. use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
  16. /**
  17. * The context of a validation run.
  18. *
  19. * The context collects all violations generated during the validation. By
  20. * default, validators execute all validations in a new context:
  21. *
  22. * $violations = $validator->validate($object);
  23. *
  24. * When you make another call to the validator, while the validation is in
  25. * progress, the violations will be isolated from each other:
  26. *
  27. * public function validate($value, Constraint $constraint)
  28. * {
  29. * $validator = $this->context->getValidator();
  30. *
  31. * // The violations are not added to $this->context
  32. * $violations = $validator->validate($value);
  33. * }
  34. *
  35. * However, if you want to add the violations to the current context, use the
  36. * {@link ValidatorInterface::inContext()} method:
  37. *
  38. * public function validate($value, Constraint $constraint)
  39. * {
  40. * $validator = $this->context->getValidator();
  41. *
  42. * // The violations are added to $this->context
  43. * $validator
  44. * ->inContext($this->context)
  45. * ->validate($value)
  46. * ;
  47. * }
  48. *
  49. * Additionally, the context provides information about the current state of
  50. * the validator, such as the currently validated class, the name of the
  51. * currently validated property and more. These values change over time, so you
  52. * cannot store a context and expect that the methods still return the same
  53. * results later on.
  54. *
  55. * @author Bernhard Schussek <bschussek@gmail.com>
  56. */
  57. interface ExecutionContextInterface extends LegacyExecutionContextInterface
  58. {
  59. /**
  60. * Returns a builder for adding a violation with extended information.
  61. *
  62. * Call {@link ConstraintViolationBuilderInterface::addViolation()} to
  63. * add the violation when you're done with the configuration:
  64. *
  65. * $context->buildViolation('Please enter a number between %min% and %max%.')
  66. * ->setParameter('%min%', 3)
  67. * ->setParameter('%max%', 10)
  68. * ->setTranslationDomain('number_validation')
  69. * ->addViolation();
  70. *
  71. * @param string $message The error message
  72. * @param array $parameters The parameters substituted in the error message
  73. *
  74. * @return ConstraintViolationBuilderInterface The violation builder
  75. */
  76. public function buildViolation($message, array $parameters = array());
  77. /**
  78. * Returns the validator.
  79. *
  80. * Useful if you want to validate additional constraints:
  81. *
  82. * public function validate($value, Constraint $constraint)
  83. * {
  84. * $validator = $this->context->getValidator();
  85. *
  86. * $violations = $validator->validate($value, new Length(array('min' => 3)));
  87. *
  88. * if (count($violations) > 0) {
  89. * // ...
  90. * }
  91. * }
  92. *
  93. * @return ValidatorInterface
  94. */
  95. public function getValidator();
  96. /**
  97. * Returns the currently validated object.
  98. *
  99. * If the validator is currently validating a class constraint, the
  100. * object of that class is returned. If it is a validating a property or
  101. * getter constraint, the object that the property/getter belongs to is
  102. * returned.
  103. *
  104. * In other cases, null is returned.
  105. *
  106. * @return object|null The currently validated object or null
  107. */
  108. public function getObject();
  109. /**
  110. * Sets the currently validated value.
  111. *
  112. * @param mixed $value The validated value
  113. * @param object|null $object The currently validated object
  114. * @param MetadataInterface|null $metadata The validation metadata
  115. * @param string $propertyPath The property path to the current value
  116. *
  117. * @internal Used by the validator engine. Should not be called by user
  118. * code.
  119. */
  120. public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath);
  121. /**
  122. * Sets the currently validated group.
  123. *
  124. * @param string|null $group The validated group
  125. *
  126. * @internal Used by the validator engine. Should not be called by user
  127. * code.
  128. */
  129. public function setGroup($group);
  130. /**
  131. * Sets the currently validated constraint.
  132. *
  133. * @param Constraint $constraint The validated constraint
  134. *
  135. * @internal Used by the validator engine. Should not be called by user
  136. * code.
  137. */
  138. public function setConstraint(Constraint $constraint);
  139. /**
  140. * Marks an object as validated in a specific validation group.
  141. *
  142. * @param string $cacheKey The hash of the object
  143. * @param string $groupHash The group's name or hash, if it is group
  144. * sequence
  145. *
  146. * @internal Used by the validator engine. Should not be called by user
  147. * code.
  148. */
  149. public function markGroupAsValidated($cacheKey, $groupHash);
  150. /**
  151. * Returns whether an object was validated in a specific validation group.
  152. *
  153. * @param string $cacheKey The hash of the object
  154. * @param string $groupHash The group's name or hash, if it is group
  155. * sequence
  156. *
  157. * @return bool Whether the object was already validated for that
  158. * group
  159. *
  160. * @internal Used by the validator engine. Should not be called by user
  161. * code.
  162. */
  163. public function isGroupValidated($cacheKey, $groupHash);
  164. /**
  165. * Marks a constraint as validated for an object.
  166. *
  167. * @param string $cacheKey The hash of the object
  168. * @param string $constraintHash The hash of the constraint
  169. *
  170. * @internal Used by the validator engine. Should not be called by user
  171. * code.
  172. */
  173. public function markConstraintAsValidated($cacheKey, $constraintHash);
  174. /**
  175. * Returns whether a constraint was validated for an object.
  176. *
  177. * @param string $cacheKey The hash of the object
  178. * @param string $constraintHash The hash of the constraint
  179. *
  180. * @return bool Whether the constraint was already validated
  181. *
  182. * @internal Used by the validator engine. Should not be called by user
  183. * code.
  184. */
  185. public function isConstraintValidated($cacheKey, $constraintHash);
  186. /**
  187. * Marks that an object was initialized.
  188. *
  189. * @param string $cacheKey The hash of the object
  190. *
  191. * @internal Used by the validator engine. Should not be called by user
  192. * code.
  193. *
  194. * @see ObjectInitializerInterface
  195. */
  196. public function markObjectAsInitialized($cacheKey);
  197. /**
  198. * Returns whether an object was initialized.
  199. *
  200. * @param string $cacheKey The hash of the object
  201. *
  202. * @return bool Whether the object was already initialized
  203. *
  204. * @internal Used by the validator engine. Should not be called by user
  205. * code.
  206. *
  207. * @see ObjectInitializerInterface
  208. */
  209. public function isObjectInitialized($cacheKey);
  210. }