ConstraintViolationInterface.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * A violation of a constraint that happened during validation.
  13. *
  14. * For each constraint that fails during validation one or more violations are
  15. * created. The violations store the violation message, the path to the failing
  16. * element in the validation graph and the root element that was originally
  17. * passed to the validator. For example, take the following graph:
  18. *
  19. * <pre>
  20. * (Person)---(firstName: string)
  21. * \
  22. * (address: Address)---(street: string)
  23. * </pre>
  24. *
  25. * If the <tt>Person</tt> object is validated and validation fails for the
  26. * "firstName" property, the generated violation has the <tt>Person</tt>
  27. * instance as root and the property path "firstName". If validation fails
  28. * for the "street" property of the related <tt>Address</tt> instance, the root
  29. * element is still the person, but the property path is "address.street".
  30. *
  31. * @author Bernhard Schussek <bschussek@gmail.com>
  32. *
  33. * @api
  34. */
  35. interface ConstraintViolationInterface
  36. {
  37. /**
  38. * Returns the violation message.
  39. *
  40. * @return string The violation message.
  41. *
  42. * @api
  43. */
  44. public function getMessage();
  45. /**
  46. * Returns the raw violation message.
  47. *
  48. * The raw violation message contains placeholders for the parameters
  49. * returned by {@link getMessageParameters}. Typically you'll pass the
  50. * message template and parameters to a translation engine.
  51. *
  52. * @return string The raw violation message.
  53. *
  54. * @api
  55. */
  56. public function getMessageTemplate();
  57. /**
  58. * Returns the parameters to be inserted into the raw violation message.
  59. *
  60. * @return array A possibly empty list of parameters indexed by the names
  61. * that appear in the message template.
  62. *
  63. * @see getMessageTemplate
  64. *
  65. * @api
  66. */
  67. public function getMessageParameters();
  68. /**
  69. * Returns a number for pluralizing the violation message.
  70. *
  71. * For example, the message template could have different translation based
  72. * on a parameter "choices":
  73. *
  74. * <ul>
  75. * <li>Please select exactly one entry. (choices=1)</li>
  76. * <li>Please select two entries. (choices=2)</li>
  77. * </ul>
  78. *
  79. * This method returns the value of the parameter for choosing the right
  80. * pluralization form (in this case "choices").
  81. *
  82. * @return integer|null The number to use to pluralize of the message.
  83. */
  84. public function getMessagePluralization();
  85. /**
  86. * Returns the root element of the validation.
  87. *
  88. * @return mixed The value that was passed originally to the validator when
  89. * the validation was started. Because the validator traverses
  90. * the object graph, the value at which the violation occurs
  91. * is not necessarily the value that was originally validated.
  92. *
  93. * @api
  94. */
  95. public function getRoot();
  96. /**
  97. * Returns the property path from the root element to the violation.
  98. *
  99. * @return string The property path indicates how the validator reached
  100. * the invalid value from the root element. If the root
  101. * element is a <tt>Person</tt> instance with a property
  102. * "address" that contains an <tt>Address</tt> instance
  103. * with an invalid property "street", the generated property
  104. * path is "address.street". Property access is denoted by
  105. * dots, while array access is denoted by square brackets,
  106. * for example "addresses[1].street".
  107. *
  108. * @api
  109. */
  110. public function getPropertyPath();
  111. /**
  112. * Returns the value that caused the violation.
  113. *
  114. * @return mixed The invalid value that caused the validated constraint to
  115. * fail.
  116. *
  117. * @api
  118. */
  119. public function getInvalidValue();
  120. /**
  121. * Returns a machine-digestible error code for the violation.
  122. *
  123. * @return mixed The error code.
  124. */
  125. public function getCode();
  126. }