ConstraintViolationInterface.php 4.1 KB

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