ConstraintViolation.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 $messageParameters;
  30. /**
  31. * @var integer|null
  32. */
  33. private $messagePluralization;
  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 mixed
  48. */
  49. private $code;
  50. /**
  51. * Creates a new constraint violation.
  52. *
  53. * @param string $message The violation message.
  54. * @param string $messageTemplate The raw violation message.
  55. * @param array $messageParameters The parameters to substitute
  56. * in the raw message.
  57. * @param mixed $root The value originally passed
  58. * to the validator.
  59. * @param string $propertyPath The property path from the
  60. * root value to the invalid
  61. * value.
  62. * @param mixed $invalidValue The invalid value causing the
  63. * violation.
  64. * @param integer|null $messagePluralization The pluralization parameter.
  65. * @param mixed $code The error code of the
  66. * violation, if any.
  67. */
  68. public function __construct($message, $messageTemplate, array $messageParameters, $root, $propertyPath, $invalidValue, $messagePluralization = null, $code = null)
  69. {
  70. $this->message = $message;
  71. $this->messageTemplate = $messageTemplate;
  72. $this->messageParameters = $messageParameters;
  73. $this->messagePluralization = $messagePluralization;
  74. $this->root = $root;
  75. $this->propertyPath = $propertyPath;
  76. $this->invalidValue = $invalidValue;
  77. $this->code = $code;
  78. }
  79. /**
  80. * Converts the violation into a string for debugging purposes.
  81. *
  82. * @return string The violation as string.
  83. */
  84. public function __toString()
  85. {
  86. $class = (string) (is_object($this->root) ? get_class($this->root) : $this->root);
  87. $propertyPath = (string) $this->propertyPath;
  88. $code = $this->code;
  89. if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
  90. $class .= '.';
  91. }
  92. if (!empty($code)) {
  93. $code = ' (code '.$code.')';
  94. }
  95. return $class.$propertyPath.":\n ".$this->getMessage().$code;
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function getMessageTemplate()
  101. {
  102. return $this->messageTemplate;
  103. }
  104. /**
  105. * {@inheritDoc}
  106. */
  107. public function getMessageParameters()
  108. {
  109. return $this->messageParameters;
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public function getMessagePluralization()
  115. {
  116. return $this->messagePluralization;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getMessage()
  122. {
  123. return $this->message;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function getRoot()
  129. {
  130. return $this->root;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getPropertyPath()
  136. {
  137. return $this->propertyPath;
  138. }
  139. /**
  140. * {@inheritDoc}
  141. */
  142. public function getInvalidValue()
  143. {
  144. return $this->invalidValue;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getCode()
  150. {
  151. return $this->code;
  152. }
  153. }