ConstraintViolation.php 5.8 KB

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