Drupal investigation

ConstraintViolationBuilder.php 4.7KB

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