ConstraintViolationInterface.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 The violation message
  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 getMessageParameters}. 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. * @deprecated since version 2.7, to be replaced by getParameters() in 3.0.
  57. */
  58. public function getMessageParameters();
  59. /**
  60. * Returns a number for pluralizing the violation message.
  61. *
  62. * For example, the message template could have different translation based
  63. * on a parameter "choices":
  64. *
  65. * <ul>
  66. * <li>Please select exactly one entry. (choices=1)</li>
  67. * <li>Please select two entries. (choices=2)</li>
  68. * </ul>
  69. *
  70. * This method returns the value of the parameter for choosing the right
  71. * pluralization form (in this case "choices").
  72. *
  73. * @return int|null The number to use to pluralize of the message
  74. *
  75. * @deprecated since version 2.7, to be replaced by getPlural() in 3.0.
  76. */
  77. public function getMessagePluralization();
  78. /**
  79. * Returns the root element of the validation.
  80. *
  81. * @return mixed The value that was passed originally to the validator when
  82. * the validation was started. Because the validator traverses
  83. * the object graph, the value at which the violation occurs
  84. * is not necessarily the value that was originally validated.
  85. */
  86. public function getRoot();
  87. /**
  88. * Returns the property path from the root element to the violation.
  89. *
  90. * @return string The property path indicates how the validator reached
  91. * the invalid value from the root element. If the root
  92. * element is a <tt>Person</tt> instance with a property
  93. * "address" that contains an <tt>Address</tt> instance
  94. * with an invalid property "street", the generated property
  95. * path is "address.street". Property access is denoted by
  96. * dots, while array access is denoted by square brackets,
  97. * for example "addresses[1].street".
  98. */
  99. public function getPropertyPath();
  100. /**
  101. * Returns the value that caused the violation.
  102. *
  103. * @return mixed the invalid value that caused the validated constraint to
  104. * fail
  105. */
  106. public function getInvalidValue();
  107. /**
  108. * Returns a machine-digestible error code for the violation.
  109. *
  110. * @return string|null The error code
  111. */
  112. public function getCode();
  113. }