ConstraintViolationBuilder.php 4.3 KB

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