ConstraintViolation.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * Default implementation of {@ConstraintViolationInterface}.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class ConstraintViolation implements ConstraintViolationInterface
  17. {
  18. private $message;
  19. private $messageTemplate;
  20. private $parameters;
  21. private $plural;
  22. private $root;
  23. private $propertyPath;
  24. private $invalidValue;
  25. private $constraint;
  26. private $code;
  27. private $cause;
  28. /**
  29. * Creates a new constraint violation.
  30. *
  31. * @param string $message The violation message
  32. * @param string $messageTemplate The raw violation message
  33. * @param array $parameters The parameters to substitute in the
  34. * raw violation message
  35. * @param mixed $root The value originally passed to the
  36. * validator
  37. * @param string $propertyPath The property path from the root
  38. * value to the invalid value
  39. * @param mixed $invalidValue The invalid value that caused this
  40. * violation
  41. * @param int|null $plural The number for determining the plural
  42. * form when translating the message
  43. * @param mixed $code The error code of the violation
  44. * @param Constraint|null $constraint The constraint whose validation
  45. * caused the violation
  46. * @param mixed $cause The cause of the violation
  47. */
  48. public function __construct($message, $messageTemplate, array $parameters, $root, $propertyPath, $invalidValue, $plural = null, $code = null, Constraint $constraint = null, $cause = null)
  49. {
  50. $this->message = $message;
  51. $this->messageTemplate = $messageTemplate;
  52. $this->parameters = $parameters;
  53. $this->plural = $plural;
  54. $this->root = $root;
  55. $this->propertyPath = $propertyPath;
  56. $this->invalidValue = $invalidValue;
  57. $this->constraint = $constraint;
  58. $this->code = $code;
  59. $this->cause = $cause;
  60. }
  61. /**
  62. * Converts the violation into a string for debugging purposes.
  63. *
  64. * @return string The violation as string
  65. */
  66. public function __toString()
  67. {
  68. if (\is_object($this->root)) {
  69. $class = 'Object('.\get_class($this->root).')';
  70. } elseif (\is_array($this->root)) {
  71. $class = 'Array';
  72. } else {
  73. $class = (string) $this->root;
  74. }
  75. $propertyPath = (string) $this->propertyPath;
  76. $code = $this->code;
  77. if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
  78. $class .= '.';
  79. }
  80. if (!empty($code)) {
  81. $code = ' (code '.$code.')';
  82. }
  83. return $class.$propertyPath.":\n ".$this->getMessage().$code;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getMessageTemplate()
  89. {
  90. return $this->messageTemplate;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. *
  95. * @deprecated since version 2.7, to be removed in 3.0.
  96. * Use getParameters() instead
  97. */
  98. public function getMessageParameters()
  99. {
  100. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7, to be removed in 3.0. Use the ConstraintViolation::getParameters() method instead.', E_USER_DEPRECATED);
  101. return $this->parameters;
  102. }
  103. /**
  104. * Alias of {@link getMessageParameters()}.
  105. */
  106. public function getParameters()
  107. {
  108. return $this->parameters;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. *
  113. * @deprecated since version 2.7, to be removed in 3.0.
  114. * Use getPlural() instead
  115. */
  116. public function getMessagePluralization()
  117. {
  118. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7, to be removed in 3.0. Use the ConstraintViolation::getPlural() method instead.', E_USER_DEPRECATED);
  119. return $this->plural;
  120. }
  121. /**
  122. * Alias of {@link getMessagePluralization()}.
  123. */
  124. public function getPlural()
  125. {
  126. return $this->plural;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getMessage()
  132. {
  133. return $this->message;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function getRoot()
  139. {
  140. return $this->root;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getPropertyPath()
  146. {
  147. return $this->propertyPath;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function getInvalidValue()
  153. {
  154. return $this->invalidValue;
  155. }
  156. /**
  157. * Returns the constraint whose validation caused the violation.
  158. *
  159. * @return Constraint|null The constraint or null if it is not known
  160. */
  161. public function getConstraint()
  162. {
  163. return $this->constraint;
  164. }
  165. /**
  166. * Returns the cause of the violation.
  167. *
  168. * @return mixed
  169. */
  170. public function getCause()
  171. {
  172. return $this->cause;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getCode()
  178. {
  179. return $this->code;
  180. }
  181. }