ExecutionContext.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\Context\ExecutionContextInterface;
  8. use Symfony\Component\Validator\Mapping\MetadataInterface;
  9. use Symfony\Component\Validator\Util\PropertyPath;
  10. use Symfony\Component\Validator\Validator\ValidatorInterface;
  11. /**
  12. * Defines an execution context class.
  13. *
  14. * We do not use the context provided by Symfony as it is marked internal, so
  15. * this class is pretty much the same, but has some code style changes as well
  16. * as exceptions for methods we don't support.
  17. */
  18. class ExecutionContext implements ExecutionContextInterface {
  19. /**
  20. * @var \Symfony\Component\Validator\Validator\ValidatorInterface
  21. */
  22. protected $validator;
  23. /**
  24. * The root value of the validated object graph.
  25. *
  26. * @var mixed
  27. */
  28. protected $root;
  29. /**
  30. * @var \Drupal\Core\Validation\TranslatorInterface
  31. */
  32. protected $translator;
  33. /**
  34. * @var string
  35. */
  36. protected $translationDomain;
  37. /**
  38. * The violations generated in the current context.
  39. *
  40. * @var \Symfony\Component\Validator\ConstraintViolationList
  41. */
  42. protected $violations;
  43. /**
  44. * The currently validated value.
  45. *
  46. * @var mixed
  47. */
  48. protected $value;
  49. /**
  50. * The currently validated typed data object.
  51. *
  52. * @var \Drupal\Core\TypedData\TypedDataInterface
  53. */
  54. protected $data;
  55. /**
  56. * The property path leading to the current value.
  57. *
  58. * @var string
  59. */
  60. protected $propertyPath = '';
  61. /**
  62. * The current validation metadata.
  63. *
  64. * @var \Symfony\Component\Validator\Mapping\MetadataInterface|null
  65. */
  66. protected $metadata;
  67. /**
  68. * The currently validated group.
  69. *
  70. * @var string|null
  71. */
  72. protected $group;
  73. /**
  74. * The currently validated constraint.
  75. *
  76. * @var \Symfony\Component\Validator\Constraint|null
  77. */
  78. protected $constraint;
  79. /**
  80. * Stores which objects have been validated in which group.
  81. *
  82. * @var array
  83. */
  84. protected $validatedObjects = [];
  85. /**
  86. * Stores which class constraint has been validated for which object.
  87. *
  88. * @var array
  89. */
  90. protected $validatedConstraints = [];
  91. /**
  92. * Creates a new ExecutionContext.
  93. *
  94. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  95. * The validator.
  96. * @param mixed $root
  97. * The root.
  98. * @param \Drupal\Core\Validation\TranslatorInterface $translator
  99. * The translator.
  100. * @param string $translationDomain
  101. * (optional) The translation domain.
  102. *
  103. * @internal Called by \Drupal\Core\TypedData\Validation\ExecutionContextFactory.
  104. * Should not be used in user code.
  105. */
  106. public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = NULL) {
  107. $this->validator = $validator;
  108. $this->root = $root;
  109. $this->translator = $translator;
  110. $this->translationDomain = $translationDomain;
  111. $this->violations = new ConstraintViolationList();
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function setNode($value, $object, MetadataInterface $metadata = NULL, $propertyPath) {
  117. $this->value = $value;
  118. $this->data = $object;
  119. $this->metadata = $metadata;
  120. $this->propertyPath = (string) $propertyPath;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function setGroup($group) {
  126. $this->group = $group;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function setConstraint(Constraint $constraint) {
  132. $this->constraint = $constraint;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function addViolation($message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
  138. // The parameters $invalidValue and following are ignored by the new
  139. // API, as they are not present in the new interface anymore.
  140. // You should use buildViolation() instead.
  141. if (func_num_args() > 2) {
  142. throw new \LogicException('Legacy validator API is unsupported.');
  143. }
  144. $this->violations->add(new ConstraintViolation($this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->propertyPath, $this->value, NULL, NULL, $this->constraint));
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function buildViolation($message, array $parameters = []) {
  150. return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->value, $this->translator, $this->translationDomain);
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getViolations() {
  156. return $this->violations;
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getValidator() {
  162. return $this->validator;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getRoot() {
  168. return $this->root;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function getValue() {
  174. return $this->value;
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function getObject() {
  180. return $this->data;
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public function getMetadata() {
  186. return $this->metadata;
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function getGroup() {
  192. return Constraint::DEFAULT_GROUP;
  193. }
  194. /**
  195. * {@inheritdoc}
  196. */
  197. public function getClassName() {
  198. return get_class($this->data);
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getPropertyName() {
  204. return $this->data->getName();
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function getPropertyPath($sub_path = '') {
  210. return PropertyPath::append($this->propertyPath, $sub_path);
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function addViolationAt($subPath, $message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
  216. throw new \LogicException('Legacy validator API is unsupported.');
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function validate($value, $subPath = '', $groups = NULL, $traverse = FALSE, $deep = FALSE) {
  222. throw new \LogicException('Legacy validator API is unsupported.');
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function markConstraintAsValidated($cache_key, $constraint_hash) {
  228. $this->validatedConstraints[$cache_key . ':' . $constraint_hash] = TRUE;
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function isConstraintValidated($cache_key, $constraint_hash) {
  234. return isset($this->validatedConstraints[$cache_key . ':' . $constraint_hash]);
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function validateValue($value, $constraints, $subPath = '', $groups = NULL) {
  240. throw new \LogicException('Legacy validator API is unsupported.');
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function markGroupAsValidated($cache_key, $group_hash) {
  246. $this->validatedObjects[$cache_key][$group_hash] = TRUE;
  247. }
  248. /**
  249. * {@inheritdoc}
  250. */
  251. public function isGroupValidated($cache_key, $group_hash) {
  252. return isset($this->validatedObjects[$cache_key][$group_hash]);
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function markObjectAsInitialized($cache_key) {
  258. // Not supported, so nothing todo.
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function isObjectInitialized($cache_key) {
  264. // Not supported, so nothing todo.
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function getMetadataFactory() {
  270. throw new \LogicException('Legacy validator API is unsupported.');
  271. }
  272. }