ExecutionContextInterface.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /**
  12. * Stores the validator's state during validation.
  13. *
  14. * For example, let's validate the following object graph:
  15. *
  16. * (Person)---($firstName: string)
  17. * \
  18. * ($address: Address)---($street: string)
  19. *
  20. * We validate the <tt>Person</tt> instance, which becomes the "root" of the
  21. * validation run (see {@link getRoot}). The state of the context after the
  22. * first step will be like this:
  23. *
  24. * (Person)---($firstName: string)
  25. * ^ \
  26. * ($address: Address)---($street: string)
  27. *
  28. * The validator is stopped at the <tt>Person</tt> node, both the root and the
  29. * value (see {@link getValue}) of the context point to the <tt>Person</tt>
  30. * instance. The property path is empty at this point (see {@link getPropertyPath}).
  31. * The metadata of the context is the metadata of the <tt>Person</tt> node
  32. * (see {@link getMetadata}).
  33. *
  34. * After advancing to the property <tt>$firstName</tt> of the <tt>Person</tt>
  35. * instance, the state of the context looks like this:
  36. *
  37. * (Person)---($firstName: string)
  38. * \ ^
  39. * ($address: Address)---($street: string)
  40. *
  41. * The validator is stopped at the property <tt>$firstName</tt>. The root still
  42. * points to the <tt>Person</tt> instance, because this is where the validation
  43. * started. The property path is now "firstName" and the current value is the
  44. * value of that property.
  45. *
  46. * After advancing to the <tt>$address</tt> property and then to the
  47. * <tt>$street</tt> property of the <tt>Address</tt> instance, the context state
  48. * looks like this:
  49. *
  50. * (Person)---($firstName: string)
  51. * \
  52. * ($address: Address)---($street: string)
  53. * ^
  54. *
  55. * The validator is stopped at the property <tt>$street</tt>. The root still
  56. * points to the <tt>Person</tt> instance, but the property path is now
  57. * "address.street" and the validated value is the value of that property.
  58. *
  59. * Apart from the root, the property path and the currently validated value,
  60. * the execution context also knows the metadata of the current node (see
  61. * {@link getMetadata}) which for example returns a {@link Mapping\PropertyMetadata}
  62. * or a {@link Mapping\ClassMetadata} object. he context also contains the
  63. * validation group that is currently being validated (see {@link getGroup}) and
  64. * the violations that happened up until now (see {@link getViolations}).
  65. *
  66. * Apart from reading the execution context, you can also use
  67. * {@link addViolation} or {@link addViolationAt} to add new violations and
  68. * {@link validate} or {@link validateValue} to validate values that the
  69. * validator otherwise would not reach.
  70. *
  71. * @author Bernhard Schussek <bschussek@gmail.com>
  72. *
  73. * @deprecated since version 2.5, to be removed in 3.0.
  74. * Use {@link Context\ExecutionContextInterface} instead.
  75. */
  76. interface ExecutionContextInterface
  77. {
  78. /**
  79. * Adds a violation at the current node of the validation graph.
  80. *
  81. * Note: the parameters $invalidValue, $plural and $code are deprecated since version 2.5 and will be removed in 3.0.
  82. *
  83. * @param string $message The error message
  84. * @param array $params The parameters substituted in the error message
  85. * @param mixed $invalidValue The invalid, validated value
  86. * @param int|null $plural The number to use to pluralize of the message
  87. * @param int|null $code The violation code
  88. */
  89. public function addViolation($message, array $params = array(), $invalidValue = null, $plural = null, $code = null);
  90. /**
  91. * Adds a violation at the validation graph node with the given property
  92. * path relative to the current property path.
  93. *
  94. * @param string $subPath The relative property path for the violation
  95. * @param string $message The error message
  96. * @param array $parameters The parameters substituted in the error message
  97. * @param mixed $invalidValue The invalid, validated value
  98. * @param int|null $plural The number to use to pluralize of the message
  99. * @param int|null $code The violation code
  100. *
  101. * @deprecated since version 2.5, to be removed in 3.0.
  102. * Use {@link Context\ExecutionContextInterface::buildViolation()}
  103. * instead.
  104. */
  105. public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null);
  106. /**
  107. * Validates the given value within the scope of the current validation.
  108. *
  109. * The value may be any value recognized by the used metadata factory
  110. * (see {@link MetadataFactoryInterface::getMetadata}), or an array or a
  111. * traversable object of such values.
  112. *
  113. * Usually you validate a value that is not the current node of the
  114. * execution context. For this case, you can pass the {@link $subPath}
  115. * argument which is appended to the current property path when a violation
  116. * is created. For example, take the following object graph:
  117. *
  118. * (Person)---($address: Address)---($phoneNumber: PhoneNumber)
  119. *
  120. * When the execution context stops at the <tt>Person</tt> instance, the
  121. * property path is "address". When you validate the <tt>PhoneNumber</tt>
  122. * instance now, pass "phoneNumber" as sub path to correct the property path
  123. * to "address.phoneNumber":
  124. *
  125. * $context->validate($address->phoneNumber, 'phoneNumber');
  126. *
  127. * Any violations generated during the validation will be added to the
  128. * violation list that you can access with {@link getViolations}.
  129. *
  130. * @param mixed $value The value to validate
  131. * @param string $subPath The path to append to the context's property path
  132. * @param string|string[]|null $groups The groups to validate in. If you don't pass any
  133. * groups here, the current group of the context
  134. * will be used.
  135. * @param bool $traverse Whether to traverse the value if it is an array
  136. * or an instance of <tt>\Traversable</tt>
  137. * @param bool $deep Whether to traverse the value recursively if
  138. * it is a collection of collections
  139. *
  140. * @deprecated since version 2.5, to be removed in 3.0.
  141. * Use {@link Context\ExecutionContextInterface::getValidator()}
  142. * instead.
  143. */
  144. public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false);
  145. /**
  146. * Validates a value against a constraint.
  147. *
  148. * Use the parameter <tt>$subPath</tt> to adapt the property path for the
  149. * validated value. For example, take the following object graph:
  150. *
  151. * (Person)---($address: Address)---($street: string)
  152. *
  153. * When the validator validates the <tt>Address</tt> instance, the
  154. * property path stored in the execution context is "address". When you
  155. * manually validate the property <tt>$street</tt> now, pass the sub path
  156. * "street" to adapt the full property path to "address.street":
  157. *
  158. * $context->validate($address->street, new NotNull(), 'street');
  159. *
  160. * @param mixed $value The value to validate
  161. * @param Constraint|Constraint[] $constraints The constraint(s) to validate against
  162. * @param string $subPath The path to append to the context's property path
  163. * @param string|string[]|null $groups The groups to validate in. If you don't pass any
  164. * groups here, the current group of the context
  165. * will be used.
  166. *
  167. * @deprecated since version 2.5, to be removed in 3.0.
  168. * Use {@link Context\ExecutionContextInterface::getValidator()}
  169. * instead.
  170. */
  171. public function validateValue($value, $constraints, $subPath = '', $groups = null);
  172. /**
  173. * Returns the violations generated by the validator so far.
  174. *
  175. * @return ConstraintViolationListInterface The constraint violation list
  176. */
  177. public function getViolations();
  178. /**
  179. * Returns the value at which validation was started in the object graph.
  180. *
  181. * The validator, when given an object, traverses the properties and
  182. * related objects and their properties. The root of the validation is the
  183. * object from which the traversal started.
  184. *
  185. * The current value is returned by {@link getValue}.
  186. *
  187. * @return mixed The root value of the validation
  188. */
  189. public function getRoot();
  190. /**
  191. * Returns the value that the validator is currently validating.
  192. *
  193. * If you want to retrieve the object that was originally passed to the
  194. * validator, use {@link getRoot}.
  195. *
  196. * @return mixed The currently validated value
  197. */
  198. public function getValue();
  199. /**
  200. * Returns the metadata for the currently validated value.
  201. *
  202. * With the core implementation, this method returns a
  203. * {@link Mapping\ClassMetadata} instance if the current value is an object,
  204. * a {@link Mapping\PropertyMetadata} instance if the current value is
  205. * the value of a property and a {@link Mapping\GetterMetadata} instance if
  206. * the validated value is the result of a getter method.
  207. *
  208. * If the validated value is neither of these, for example if the validator
  209. * has been called with a plain value and constraint, this method returns
  210. * null.
  211. *
  212. * @return MetadataInterface|null the metadata of the currently validated
  213. * value
  214. */
  215. public function getMetadata();
  216. /**
  217. * Returns the used metadata factory.
  218. *
  219. * @return MetadataFactoryInterface The metadata factory
  220. *
  221. * @deprecated since version 2.5, to be removed in 3.0.
  222. * Use {@link Context\ExecutionContextInterface::getValidator()}
  223. * instead and call
  224. * {@link Validator\ValidatorInterface::getMetadataFor()} or
  225. * {@link Validator\ValidatorInterface::hasMetadataFor()} there.
  226. */
  227. public function getMetadataFactory();
  228. /**
  229. * Returns the validation group that is currently being validated.
  230. *
  231. * @return string The current validation group
  232. */
  233. public function getGroup();
  234. /**
  235. * Returns the class name of the current node.
  236. *
  237. * If the metadata of the current node does not implement
  238. * {@link ClassBasedInterface} or if no metadata is available for the
  239. * current node, this method returns null.
  240. *
  241. * @return string|null The class name or null, if no class name could be found
  242. */
  243. public function getClassName();
  244. /**
  245. * Returns the property name of the current node.
  246. *
  247. * If the metadata of the current node does not implement
  248. * {@link PropertyMetadataInterface} or if no metadata is available for the
  249. * current node, this method returns null.
  250. *
  251. * @return string|null The property name or null, if no property name could be found
  252. */
  253. public function getPropertyName();
  254. /**
  255. * Returns the property path to the value that the validator is currently
  256. * validating.
  257. *
  258. * For example, take the following object graph:
  259. *
  260. * (Person)---($address: Address)---($street: string)
  261. *
  262. * When the <tt>Person</tt> instance is passed to the validator, the
  263. * property path is initially empty. When the <tt>$address</tt> property
  264. * of that person is validated, the property path is "address". When
  265. * the <tt>$street</tt> property of the related <tt>Address</tt> instance
  266. * is validated, the property path is "address.street".
  267. *
  268. * Properties of objects are prefixed with a dot in the property path.
  269. * Indices of arrays or objects implementing the {@link \ArrayAccess}
  270. * interface are enclosed in brackets. For example, if the property in
  271. * the previous example is <tt>$addresses</tt> and contains an array
  272. * of <tt>Address</tt> instance, the property path generated for the
  273. * <tt>$street</tt> property of one of these addresses is for example
  274. * "addresses[0].street".
  275. *
  276. * @param string $subPath Optional. The suffix appended to the current
  277. * property path.
  278. *
  279. * @return string The current property path. The result may be an empty
  280. * string if the validator is currently validating the
  281. * root value of the validation graph.
  282. */
  283. public function getPropertyPath($subPath = '');
  284. }