ExecutionContextInterface.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * @api
  82. */
  83. interface ExecutionContextInterface
  84. {
  85. /**
  86. * Adds a violation at the current node of the validation graph.
  87. *
  88. * @param string $message The error message.
  89. * @param array $params The parameters substituted in the error message.
  90. * @param mixed $invalidValue The invalid, validated value.
  91. * @param integer|null $pluralization The number to use to pluralize of the message.
  92. * @param integer|null $code The violation code.
  93. *
  94. * @api
  95. */
  96. public function addViolation($message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null);
  97. /**
  98. * Adds a violation at the validation graph node with the given property
  99. * path relative to the current property path.
  100. *
  101. * @param string $subPath The relative property path for the violation.
  102. * @param string $message The error message.
  103. * @param array $params The parameters substituted in the error message.
  104. * @param mixed $invalidValue The invalid, validated value.
  105. * @param integer|null $pluralization The number to use to pluralize of the message.
  106. * @param integer|null $code The violation code.
  107. *
  108. * @api
  109. */
  110. public function addViolationAt($subPath, $message, array $params = array(), $invalidValue = null, $pluralization = null, $code = null);
  111. /**
  112. * Validates the given value within the scope of the current validation.
  113. *
  114. * The value may be any value recognized by the used metadata factory
  115. * (see {@link MetadataFactoryInterface::getMetadata}), or an array or a
  116. * traversable object of such values.
  117. *
  118. * Usually you validate a value that is not the current node of the
  119. * execution context. For this case, you can pass the {@link $subPath}
  120. * argument which is appended to the current property path when a violation
  121. * is created. For example, take the following object graph:
  122. *
  123. * <pre>
  124. * (Person)---($address: Address)---($phoneNumber: PhoneNumber)
  125. * ^
  126. * </pre>
  127. *
  128. * When the execution context stops at the <tt>Person</tt> instance, the
  129. * property path is "address". When you validate the <tt>PhoneNumber</tt>
  130. * instance now, pass "phoneNumber" as sub path to correct the property path
  131. * to "address.phoneNumber":
  132. *
  133. * <pre>
  134. * $context->validate($address->phoneNumber, 'phoneNumber');
  135. * </pre>
  136. *
  137. * Any violations generated during the validation will be added to the
  138. * violation list that you can access with {@link getViolations}.
  139. *
  140. * @param mixed $value The value to validate.
  141. * @param string $subPath The path to append to the context's property path.
  142. * @param null|string|string[] $groups The groups to validate in. If you don't pass any
  143. * groups here, the current group of the context
  144. * will be used.
  145. * @param Boolean $traverse Whether to traverse the value if it is an array
  146. * or an instance of <tt>\Traversable</tt>.
  147. * @param Boolean $deep Whether to traverse the value recursively if
  148. * it is a collection of collections.
  149. */
  150. public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false);
  151. /**
  152. * Validates a value against a constraint.
  153. *
  154. * Use the parameter <tt>$subPath</tt> to adapt the property path for the
  155. * validated value. For example, take the following object graph:
  156. *
  157. * <pre>
  158. * (Person)---($address: Address)---($street: string)
  159. * ^
  160. * </pre>
  161. *
  162. * When the validator validates the <tt>Address</tt> instance, the
  163. * property path stored in the execution context is "address". When you
  164. * manually validate the property <tt>$street</tt> now, pass the sub path
  165. * "street" to adapt the full property path to "address.street":
  166. *
  167. * <pre>
  168. * $context->validate($address->street, new NotNull(), 'street');
  169. * </pre>
  170. *
  171. * @param mixed $value The value to validate.
  172. * @param Constraint|Constraint[] $constraints The constraint(s) to validate against.
  173. * @param string $subPath The path to append to the context's property path.
  174. * @param null|string|string[] $groups The groups to validate in. If you don't pass any
  175. * groups here, the current group of the context
  176. * will be used.
  177. */
  178. public function validateValue($value, $constraints, $subPath = '', $groups = null);
  179. /**
  180. * Returns the violations generated by the validator so far.
  181. *
  182. * @return ConstraintViolationListInterface The constraint violation list.
  183. *
  184. * @api
  185. */
  186. public function getViolations();
  187. /**
  188. * Returns the value at which validation was started in the object graph.
  189. *
  190. * The validator, when given an object, traverses the properties and
  191. * related objects and their properties. The root of the validation is the
  192. * object from which the traversal started.
  193. *
  194. * The current value is returned by {@link getValue}.
  195. *
  196. * @return mixed The root value of the validation.
  197. */
  198. public function getRoot();
  199. /**
  200. * Returns the value that the validator is currently validating.
  201. *
  202. * If you want to retrieve the object that was originally passed to the
  203. * validator, use {@link getRoot}.
  204. *
  205. * @return mixed The currently validated value.
  206. */
  207. public function getValue();
  208. /**
  209. * Returns the metadata for the currently validated value.
  210. *
  211. * With the core implementation, this method returns a
  212. * {@link Mapping\ClassMetadata} instance if the current value is an object,
  213. * a {@link Mapping\PropertyMetadata} instance if the current value is
  214. * the value of a property and a {@link Mapping\GetterMetadata} instance if
  215. * the validated value is the result of a getter method.
  216. *
  217. * If the validated value is neither of these, for example if the validator
  218. * has been called with a plain value and constraint, this method returns
  219. * null.
  220. *
  221. * @return MetadataInterface|null The metadata of the currently validated
  222. * value.
  223. */
  224. public function getMetadata();
  225. /**
  226. * Returns the used metadata factory.
  227. *
  228. * @return MetadataFactoryInterface The metadata factory.
  229. */
  230. public function getMetadataFactory();
  231. /**
  232. * Returns the validation group that is currently being validated.
  233. *
  234. * @return string The current validation group.
  235. */
  236. public function getGroup();
  237. /**
  238. * Returns the class name of the current node.
  239. *
  240. * If the metadata of the current node does not implement
  241. * {@link ClassBasedInterface} or if no metadata is available for the
  242. * current node, this method returns null.
  243. *
  244. * @return string|null The class name or null, if no class name could be found.
  245. */
  246. public function getClassName();
  247. /**
  248. * Returns the property name of the current node.
  249. *
  250. * If the metadata of the current node does not implement
  251. * {@link PropertyMetadataInterface} or if no metadata is available for the
  252. * current node, this method returns null.
  253. *
  254. * @return string|null The property name or null, if no property name could be found.
  255. */
  256. public function getPropertyName();
  257. /**
  258. * Returns the property path to the value that the validator is currently
  259. * validating.
  260. *
  261. * For example, take the following object graph:
  262. *
  263. * <pre>
  264. * (Person)---($address: Address)---($street: string)
  265. * </pre>
  266. *
  267. * When the <tt>Person</tt> instance is passed to the validator, the
  268. * property path is initially empty. When the <tt>$address</tt> property
  269. * of that person is validated, the property path is "address". When
  270. * the <tt>$street</tt> property of the related <tt>Address</tt> instance
  271. * is validated, the property path is "address.street".
  272. *
  273. * Properties of objects are prefixed with a dot in the property path.
  274. * Indices of arrays or objects implementing the {@link \ArrayAccess}
  275. * interface are enclosed in brackets. For example, if the property in
  276. * the previous example is <tt>$addresses</tt> and contains an array
  277. * of <tt>Address</tt> instance, the property path generated for the
  278. * <tt>$street</tt> property of one of these addresses is for example
  279. * "addresses[0].street".
  280. *
  281. * @param string $subPath Optional. The suffix appended to the current
  282. * property path.
  283. *
  284. * @return string The current property path. The result may be an empty
  285. * string if the validator is currently validating the
  286. * root value of the validation graph.
  287. */
  288. public function getPropertyPath($subPath = '');
  289. }