PageRenderTime 66ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Validator/ConstraintViolation.php

http://github.com/symfony/symfony
PHP | 181 lines | 89 code | 20 blank | 72 comment | 10 complexity | b9e88cf6711c551503905cb3995822a8 MD5 | raw file
  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|\Stringable $message The violation message as a string or a stringable object
  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, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, string $code = null, Constraint $constraint = null, $cause = null)
  49. {
  50. if (!\is_string($message) && !(\is_object($message) && method_exists($message, '__toString'))) {
  51. throw new \TypeError('Constraint violation message should be a string or an object which implements the __toString() method.');
  52. }
  53. $this->message = $message;
  54. $this->messageTemplate = $messageTemplate;
  55. $this->parameters = $parameters;
  56. $this->plural = $plural;
  57. $this->root = $root;
  58. $this->propertyPath = $propertyPath;
  59. $this->invalidValue = $invalidValue;
  60. $this->constraint = $constraint;
  61. $this->code = $code;
  62. $this->cause = $cause;
  63. }
  64. /**
  65. * Converts the violation into a string for debugging purposes.
  66. *
  67. * @return string The violation as string
  68. */
  69. public function __toString()
  70. {
  71. if (\is_object($this->root)) {
  72. $class = 'Object('.\get_class($this->root).')';
  73. } elseif (\is_array($this->root)) {
  74. $class = 'Array';
  75. } else {
  76. $class = (string) $this->root;
  77. }
  78. $propertyPath = (string) $this->propertyPath;
  79. if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
  80. $class .= '.';
  81. }
  82. if (null !== ($code = $this->code) && '' !== $code) {
  83. $code = ' (code '.$code.')';
  84. }
  85. return $class.$propertyPath.":\n ".$this->getMessage().$code;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getMessageTemplate()
  91. {
  92. return $this->messageTemplate;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getParameters()
  98. {
  99. return $this->parameters;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getPlural()
  105. {
  106. return $this->plural;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getMessage()
  112. {
  113. return $this->message;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getRoot()
  119. {
  120. return $this->root;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getPropertyPath()
  126. {
  127. return $this->propertyPath;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function getInvalidValue()
  133. {
  134. return $this->invalidValue;
  135. }
  136. /**
  137. * Returns the constraint whose validation caused the violation.
  138. *
  139. * @return Constraint|null The constraint or null if it is not known
  140. */
  141. public function getConstraint()
  142. {
  143. return $this->constraint;
  144. }
  145. /**
  146. * Returns the cause of the violation.
  147. *
  148. * @return mixed
  149. */
  150. public function getCause()
  151. {
  152. return $this->cause;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function getCode()
  158. {
  159. return $this->code;
  160. }
  161. }