ConstraintViolationBuilder.php 4.7 KB

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