ExecutionContextInterface.php 13 KB

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