ConstraintViolationBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\Violation;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintViolation;
  13. use Symfony\Component\Validator\ConstraintViolationList;
  14. use Symfony\Component\Validator\Util\PropertyPath;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17. * Default implementation of {@link ConstraintViolationBuilderInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @internal since version 2.5. Code against ConstraintViolationBuilderInterface instead.
  22. */
  23. class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
  24. {
  25. private $violations;
  26. private $message;
  27. private $parameters;
  28. private $root;
  29. private $invalidValue;
  30. private $propertyPath;
  31. private $translator;
  32. private $translationDomain;
  33. private $plural;
  34. private $constraint;
  35. private $code;
  36. /**
  37. * @var mixed
  38. */
  39. private $cause;
  40. /**
  41. * @param string $message The error message as a string or a stringable object
  42. */
  43. public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
  44. {
  45. $this->violations = $violations;
  46. $this->message = $message;
  47. $this->parameters = $parameters;
  48. $this->root = $root;
  49. $this->propertyPath = $propertyPath;
  50. $this->invalidValue = $invalidValue;
  51. $this->translator = $translator;
  52. $this->translationDomain = $translationDomain;
  53. $this->constraint = $constraint;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function atPath(string $path)
  59. {
  60. $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
  61. return $this;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setParameter(string $key, string $value)
  67. {
  68. $this->parameters[$key] = $value;
  69. return $this;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function setParameters(array $parameters)
  75. {
  76. $this->parameters = $parameters;
  77. return $this;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function setTranslationDomain(string $translationDomain)
  83. {
  84. $this->translationDomain = $translationDomain;
  85. return $this;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function setInvalidValue($invalidValue)
  91. {
  92. $this->invalidValue = $invalidValue;
  93. return $this;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function setPlural(int $number)
  99. {
  100. $this->plural = $number;
  101. return $this;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setCode(?string $code)
  107. {
  108. $this->code = $code;
  109. return $this;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function setCause($cause)
  115. {
  116. $this->cause = $cause;
  117. return $this;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function addViolation()
  123. {
  124. if (null === $this->plural) {
  125. $translatedMessage = $this->translator->trans(
  126. $this->message,
  127. $this->parameters,
  128. $this->translationDomain
  129. );
  130. } else {
  131. $translatedMessage = $this->translator->trans(
  132. $this->message,
  133. ['%count%' => $this->plural] + $this->parameters,
  134. $this->translationDomain
  135. );
  136. }
  137. $this->violations->add(new ConstraintViolation(
  138. $translatedMessage,
  139. $this->message,
  140. $this->parameters,
  141. $this->root,
  142. $this->propertyPath,
  143. $this->invalidValue,
  144. $this->plural,
  145. $this->code,
  146. $this->constraint,
  147. $this->cause
  148. ));
  149. }
  150. }