View source
<?php
namespace Drupal\Core\TypedData\Validation;
use Drupal\Core\Validation\TranslatorInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\MetadataInterface;
use Symfony\Component\Validator\Util\PropertyPath;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ExecutionContext implements ExecutionContextInterface {
protected $validator;
protected $root;
protected $translator;
protected $translationDomain;
protected $violations;
protected $value;
protected $data;
protected $propertyPath = '';
protected $metadata;
protected $group;
protected $constraint;
protected $validatedObjects = [];
protected $validatedConstraints = [];
public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = NULL) {
$this->validator = $validator;
$this->root = $root;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->violations = new ConstraintViolationList();
}
public function setNode($value, $object, MetadataInterface $metadata = NULL, $propertyPath) {
$this->value = $value;
$this->data = $object;
$this->metadata = $metadata;
$this->propertyPath = (string) $propertyPath;
}
public function setGroup($group) {
$this->group = $group;
}
public function setConstraint(Constraint $constraint) {
$this->constraint = $constraint;
}
public function addViolation($message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
if (func_num_args() > 2) {
throw new \LogicException('Legacy validator API is unsupported.');
}
$this->violations
->add(new ConstraintViolation($this->translator
->trans($message, $parameters, $this->translationDomain), $message, $parameters, $this->root, $this->propertyPath, $this->value, NULL, NULL, $this->constraint));
}
public function buildViolation($message, array $parameters = []) {
return new ConstraintViolationBuilder($this->violations, $this->constraint, $message, $parameters, $this->root, $this->propertyPath, $this->value, $this->translator, $this->translationDomain);
}
public function getViolations() {
return $this->violations;
}
public function getValidator() {
return $this->validator;
}
public function getRoot() {
return $this->root;
}
public function getValue() {
return $this->value;
}
public function getObject() {
return $this->data;
}
public function getMetadata() {
return $this->metadata;
}
public function getGroup() {
return Constraint::DEFAULT_GROUP;
}
public function getClassName() {
return get_class($this->data);
}
public function getPropertyName() {
return $this->data
->getName();
}
public function getPropertyPath($sub_path = '') {
return PropertyPath::append($this->propertyPath, $sub_path);
}
public function addViolationAt($subPath, $message, array $parameters = [], $invalidValue = NULL, $plural = NULL, $code = NULL) {
throw new \LogicException('Legacy validator API is unsupported.');
}
public function validate($value, $subPath = '', $groups = NULL, $traverse = FALSE, $deep = FALSE) {
throw new \LogicException('Legacy validator API is unsupported.');
}
public function markConstraintAsValidated($cache_key, $constraint_hash) {
$this->validatedConstraints[$cache_key . ':' . $constraint_hash] = TRUE;
}
public function isConstraintValidated($cache_key, $constraint_hash) {
return isset($this->validatedConstraints[$cache_key . ':' . $constraint_hash]);
}
public function validateValue($value, $constraints, $subPath = '', $groups = NULL) {
throw new \LogicException('Legacy validator API is unsupported.');
}
public function markGroupAsValidated($cache_key, $group_hash) {
$this->validatedObjects[$cache_key][$group_hash] = TRUE;
}
public function isGroupValidated($cache_key, $group_hash) {
return isset($this->validatedObjects[$cache_key][$group_hash]);
}
public function markObjectAsInitialized($cache_key) {
}
public function isObjectInitialized($cache_key) {
}
public function getMetadataFactory() {
throw new \LogicException('Legacy validator API is unsupported.');
}
}