ExecutionContextInterface.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * @since 2.5
  56. *
  57. * @author Bernhard Schussek <bschussek@gmail.com>
  58. */
  59. interface ExecutionContextInterface extends LegacyExecutionContextInterface
  60. {
  61. /**
  62. * Returns a builder for adding a violation with extended information.
  63. *
  64. * Call {@link ConstraintViolationBuilderInterface::addViolation()} to
  65. * add the violation when you're done with the configuration:
  66. *
  67. * $context->buildViolation('Please enter a number between %min% and %max%.')
  68. * ->setParameter('%min%', 3)
  69. * ->setParameter('%max%', 10)
  70. * ->setTranslationDomain('number_validation')
  71. * ->addViolation();
  72. *
  73. * @param string $message The error message
  74. * @param array $parameters The parameters substituted in the error message
  75. *
  76. * @return ConstraintViolationBuilderInterface The violation builder
  77. */
  78. public function buildViolation($message, array $parameters = array());
  79. /**
  80. * Returns the validator.
  81. *
  82. * Useful if you want to validate additional constraints:
  83. *
  84. * public function validate($value, Constraint $constraint)
  85. * {
  86. * $validator = $this->context->getValidator();
  87. *
  88. * $violations = $validator->validateValue($value, new Length(array('min' => 3)));
  89. *
  90. * if (count($violations) > 0) {
  91. * // ...
  92. * }
  93. * }
  94. *
  95. * @return ValidatorInterface
  96. */
  97. public function getValidator();
  98. /**
  99. * Returns the currently validated object.
  100. *
  101. * If the validator is currently validating a class constraint, the
  102. * object of that class is returned. If it is a validating a property or
  103. * getter constraint, the object that the property/getter belongs to is
  104. * returned.
  105. *
  106. * In other cases, null is returned.
  107. *
  108. * @return object|null The currently validated object or null.
  109. */
  110. public function getObject();
  111. /**
  112. * Sets the currently validated value.
  113. *
  114. * @param mixed $value The validated value
  115. * @param object|null $object The currently validated object
  116. * @param MetadataInterface|null $metadata The validation metadata
  117. * @param string $propertyPath The property path to the current value
  118. *
  119. * @internal Used by the validator engine. Should not be called by user
  120. * code.
  121. */
  122. public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath);
  123. /**
  124. * Sets the currently validated group.
  125. *
  126. * @param string|null $group The validated group
  127. *
  128. * @internal Used by the validator engine. Should not be called by user
  129. * code.
  130. */
  131. public function setGroup($group);
  132. /**
  133. * Sets the currently validated constraint.
  134. *
  135. * @param Constraint $constraint The validated constraint
  136. *
  137. * @internal Used by the validator engine. Should not be called by user
  138. * code.
  139. */
  140. public function setConstraint(Constraint $constraint);
  141. /**
  142. * Marks an object as validated in a specific validation group.
  143. *
  144. * @param string $cacheKey The hash of the object
  145. * @param string $groupHash The group's name or hash, if it is group
  146. * sequence
  147. *
  148. * @internal Used by the validator engine. Should not be called by user
  149. * code.
  150. */
  151. public function markGroupAsValidated($cacheKey, $groupHash);
  152. /**
  153. * Returns whether an object was validated in a specific validation group.
  154. *
  155. * @param string $cacheKey The hash of the object
  156. * @param string $groupHash The group's name or hash, if it is group
  157. * sequence
  158. *
  159. * @return bool Whether the object was already validated for that
  160. * group
  161. *
  162. * @internal Used by the validator engine. Should not be called by user
  163. * code.
  164. */
  165. public function isGroupValidated($cacheKey, $groupHash);
  166. /**
  167. * Marks a constraint as validated for an object.
  168. *
  169. * @param string $cacheKey The hash of the object
  170. * @param string $constraintHash The hash of the constraint
  171. *
  172. * @internal Used by the validator engine. Should not be called by user
  173. * code.
  174. */
  175. public function markConstraintAsValidated($cacheKey, $constraintHash);
  176. /**
  177. * Returns whether a constraint was validated for an object.
  178. *
  179. * @param string $cacheKey The hash of the object
  180. * @param string $constraintHash The hash of the constraint
  181. *
  182. * @return bool Whether the constraint was already validated
  183. *
  184. * @internal Used by the validator engine. Should not be called by user
  185. * code.
  186. */
  187. public function isConstraintValidated($cacheKey, $constraintHash);
  188. /**
  189. * Marks that an object was initialized.
  190. *
  191. * @param string $cacheKey The hash of the object
  192. *
  193. * @internal Used by the validator engine. Should not be called by user
  194. * code.
  195. *
  196. * @see ObjectInitializerInterface
  197. */
  198. public function markObjectAsInitialized($cacheKey);
  199. /**
  200. * Returns whether an object was initialized.
  201. *
  202. * @param string $cacheKey The hash of the object
  203. *
  204. * @return bool Whether the object was already initialized
  205. *
  206. * @internal Used by the validator engine. Should not be called by user
  207. * code.
  208. *
  209. * @see ObjectInitializerInterface
  210. */
  211. public function isObjectInitialized($cacheKey);
  212. }