3 namespace Drupal\Core\TypedData\Validation;
5 use Drupal\Core\Validation\TranslatorInterface;
6 use Symfony\Component\Validator\Constraint;
7 use Symfony\Component\Validator\ConstraintViolation;
8 use Symfony\Component\Validator\ConstraintViolationList;
9 use Symfony\Component\Validator\Context\ExecutionContextInterface;
10 use Symfony\Component\Validator\Mapping\MetadataInterface;
11 use Symfony\Component\Validator\Util\PropertyPath;
12 use Symfony\Component\Validator\Validator\ValidatorInterface;
15 * Defines an execution context class.
17 * We do not use the context provided by Symfony as it is marked internal, so
18 * this class is pretty much the same, but has some code style changes as well
19 * as exceptions for methods we don't support.
21 class ExecutionContext implements ExecutionContextInterface {
24 * @var \Symfony\Component\Validator\Validator\ValidatorInterface
29 * The root value of the validated object graph.
36 * @var \Drupal\Core\Validation\TranslatorInterface
38 protected $translator;
43 protected $translationDomain;
46 * The violations generated in the current context.
48 * @var \Symfony\Component\Validator\ConstraintViolationList
50 protected $violations;
53 * The currently validated value.
60 * The currently validated typed data object.
62 * @var \Drupal\Core\TypedData\TypedDataInterface
67 * The property path leading to the current value.
71 protected $propertyPath = '';
74 * The current validation metadata.
76 * @var \Symfony\Component\Validator\Mapping\MetadataInterface|null
81 * The currently validated group.
88 * The currently validated constraint.
90 * @var \Symfony\Component\Validator\Constraint|null
92 protected $constraint;
95 * Stores which objects have been validated in which group.
99 protected $validatedObjects = [];
102 * Stores which class constraint has been validated for which object.
106 protected $validatedConstraints = [];
109 * Creates a new ExecutionContext.
111 * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
115 * @param \Drupal\Core\Validation\TranslatorInterface $translator
117 * @param string $translationDomain
118 * (optional) The translation domain.
120 * @internal Called by \Drupal\Core\TypedData\Validation\ExecutionContextFactory.
121 * Should not be used in user code.
123 public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = NULL) {
124 $this->validator = $validator;
126 $this->translator = $translator;
127 $this->translationDomain = $translationDomain;
128 $this->violations = new ConstraintViolationList();
134 public function setNode($value, $object, MetadataInterface $metadata = NULL, $propertyPath) {
135 $this->value = $value;
136 $this->data = $object;
137 $this->metadata = $metadata;
138 $this->propertyPath = (string) $propertyPath;
144 public function setGroup($group) {
145 $this->group = $group;
151 public function setConstraint(Constraint $constraint) {
152 $this->constraint = $constraint;
158 public function addViolation($message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
159 // The parameters $invalidValue and following are ignored by the new
160 // API, as they are not present in the new interface anymore.
161 // You should use buildViolation() instead.
162 if (func_num_args() > 2) {
163 throw new \LogicException('Legacy validator API is unsupported.');
166 $this->violations->add(new ConstraintViolation($this->translator->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->propertyPath, $this->value, NULL, NULL, $this->constraint));
172 public function buildViolation($message, array $parameters = []) {
173 return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->value, $this->translator, $this->translationDomain);
179 public function getViolations() {
180 return $this->violations;
186 public function getValidator() {
187 return $this->validator;
193 public function getRoot() {
200 public function getValue() {
207 public function getObject() {
214 public function getMetadata() {
215 return $this->metadata;
221 public function getGroup() {
222 return Constraint::DEFAULT_GROUP;
228 public function getClassName() {
229 return get_class($this->data);
235 public function getPropertyName() {
236 return $this->data->getName();
242 public function getPropertyPath($sub_path = '') {
243 return PropertyPath::append($this->propertyPath, $sub_path);
249 public function addViolationAt($subPath, $message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
250 throw new \LogicException('Legacy validator API is unsupported.');
256 public function validate($value, $subPath = '', $groups = NULL, $traverse = FALSE, $deep = FALSE) {
257 throw new \LogicException('Legacy validator API is unsupported.');
263 public function markConstraintAsValidated($cache_key, $constraint_hash) {
264 $this->validatedConstraints[$cache_key . ':' . $constraint_hash] = TRUE;
270 public function isConstraintValidated($cache_key, $constraint_hash) {
271 return isset($this->validatedConstraints[$cache_key . ':' . $constraint_hash]);
277 public function validateValue($value, $constraints, $subPath = '', $groups = NULL) {
278 throw new \LogicException('Legacy validator API is unsupported.');
284 public function markGroupAsValidated($cache_key, $group_hash) {
285 $this->validatedObjects[$cache_key][$group_hash] = TRUE;
291 public function isGroupValidated($cache_key, $group_hash) {
292 return isset($this->validatedObjects[$cache_key][$group_hash]);
298 public function markObjectAsInitialized($cache_key) {
299 // Not supported, so nothing todo.
305 public function isObjectInitialized($cache_key) {
306 // Not supported, so nothing todo.
312 public function getMetadataFactory() {
313 throw new \LogicException('Legacy validator API is unsupported.');