ConstraintViolationBuilder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace Drupal\Core\TypedData\Validation;
  3. use Drupal\Core\Validation\TranslatorInterface;
  4. use Symfony\Component\Validator\Constraint;
  5. use Symfony\Component\Validator\ConstraintViolation;
  6. use Symfony\Component\Validator\ConstraintViolationList;
  7. use Symfony\Component\Validator\Util\PropertyPath;
  8. use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
  9. /**
  10. * Defines a constraint violation builder for the Typed Data validator.
  11. *
  12. * We do not use the builder provided by Symfony as it is marked internal.
  13. *
  14. * @codingStandardsIgnoreStart
  15. */
  16. class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface {
  17. /**
  18. * The list of violations.
  19. *
  20. * @var \Symfony\Component\Validator\ConstraintViolationList
  21. */
  22. protected $violations;
  23. /**
  24. * The violation message.
  25. *
  26. * @var string
  27. */
  28. protected $message;
  29. /**
  30. * The message parameters.
  31. *
  32. * @var array
  33. */
  34. protected $parameters;
  35. /**
  36. * The root path.
  37. *
  38. * @var mixed
  39. */
  40. protected $root;
  41. /**
  42. * The invalid value caused the violation.
  43. *
  44. * @var mixed
  45. */
  46. protected $invalidValue;
  47. /**
  48. * The property path.
  49. *
  50. * @var string
  51. */
  52. protected $propertyPath;
  53. /**
  54. * The translator.
  55. *
  56. * @var \Drupal\Core\Validation\TranslatorInterface
  57. */
  58. protected $translator;
  59. /**
  60. * The translation domain.
  61. *
  62. * @var string|null
  63. */
  64. protected $translationDomain;
  65. /**
  66. * The number used
  67. * @var int|null
  68. */
  69. protected $plural;
  70. /**
  71. * @var Constraint
  72. */
  73. protected $constraint;
  74. /**
  75. * @var mixed
  76. */
  77. protected $code;
  78. /**
  79. * @var mixed
  80. */
  81. protected $cause;
  82. /**
  83. * Constructs a new ConstraintViolationBuilder instance.
  84. *
  85. * @param \Symfony\Component\Validator\ConstraintViolationList $violations
  86. * The violation list.
  87. * @param \Symfony\Component\Validator\Constraint $constraint
  88. * The constraint.
  89. * @param string $message
  90. * The message.
  91. * @param array $parameters
  92. * The message parameters.
  93. * @param mixed $root
  94. * The root.
  95. * @param string $propertyPath
  96. * The property string.
  97. * @param mixed $invalidValue
  98. * The invalid value.
  99. * @param \Drupal\Core\Validation\TranslatorInterface $translator
  100. * The translator.
  101. * @param null $translationDomain
  102. * (optional) The translation domain.
  103. */
  104. public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)
  105. {
  106. $this->violations = $violations;
  107. $this->message = $message;
  108. $this->parameters = $parameters;
  109. $this->root = $root;
  110. $this->propertyPath = $propertyPath;
  111. $this->invalidValue = $invalidValue;
  112. $this->translator = $translator;
  113. $this->translationDomain = $translationDomain;
  114. $this->constraint = $constraint;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function atPath($path)
  120. {
  121. $this->propertyPath = PropertyPath::append($this->propertyPath, $path);
  122. return $this;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function setParameter($key, $value)
  128. {
  129. $this->parameters[$key] = $value;
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function setParameters(array $parameters)
  136. {
  137. $this->parameters = $parameters;
  138. return $this;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function setTranslationDomain($translationDomain)
  144. {
  145. $this->translationDomain = $translationDomain;
  146. return $this;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function setInvalidValue($invalidValue)
  152. {
  153. $this->invalidValue = $invalidValue;
  154. return $this;
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function setPlural($number)
  160. {
  161. $this->plural = $number;
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function setCode($code)
  168. {
  169. $this->code = $code;
  170. return $this;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function setCause($cause)
  176. {
  177. $this->cause = $cause;
  178. return $this;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function addViolation()
  184. {
  185. if (null === $this->plural) {
  186. $translatedMessage = $this->translator->trans(
  187. $this->message,
  188. $this->parameters,
  189. $this->translationDomain
  190. );
  191. } else {
  192. try {
  193. $translatedMessage = $this->translator->transChoice(
  194. $this->message,
  195. $this->plural,
  196. $this->parameters,
  197. $this->translationDomain#
  198. );
  199. } catch (\InvalidArgumentException $e) {
  200. $translatedMessage = $this->translator->trans(
  201. $this->message,
  202. $this->parameters,
  203. $this->translationDomain
  204. );
  205. }
  206. }
  207. $this->violations->add(new ConstraintViolation(
  208. $translatedMessage,
  209. $this->message,
  210. $this->parameters,
  211. $this->root,
  212. $this->propertyPath,
  213. $this->invalidValue,
  214. $this->plural,
  215. $this->code,
  216. $this->constraint,
  217. $this->cause
  218. ));
  219. }
  220. }