4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Context;
14 use Symfony\Component\Translation\TranslatorInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintViolation;
17 use Symfony\Component\Validator\ConstraintViolationList;
18 use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
19 use Symfony\Component\Validator\Mapping\MemberMetadata;
20 use Symfony\Component\Validator\Mapping\MetadataInterface;
21 use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
22 use Symfony\Component\Validator\Util\PropertyPath;
23 use Symfony\Component\Validator\Validator\ValidatorInterface;
24 use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
27 * The context used and created by {@link ExecutionContextFactory}.
29 * @author Bernhard Schussek <bschussek@gmail.com>
31 * @see ExecutionContextInterface
33 * @internal since version 2.5. Code against ExecutionContextInterface instead.
35 class ExecutionContext implements ExecutionContextInterface
38 * @var ValidatorInterface
43 * The root value of the validated object graph.
50 * @var TranslatorInterface
57 private $translationDomain;
60 * The violations generated in the current context.
62 * @var ConstraintViolationList
67 * The currently validated value.
74 * The currently validated object.
81 * The property path leading to the current value.
85 private $propertyPath = '';
88 * The current validation metadata.
90 * @var MetadataInterface|null
95 * The currently validated group.
102 * The currently validated constraint.
104 * @var Constraint|null
109 * Stores which objects have been validated in which group.
113 private $validatedObjects = array();
116 * Stores which class constraint has been validated for which object.
120 private $validatedConstraints = array();
123 * Stores which objects have been initialized.
127 private $initializedObjects;
130 * Creates a new execution context.
132 * @param ValidatorInterface $validator The validator
133 * @param mixed $root The root value of the
134 * validated object graph
135 * @param TranslatorInterface $translator The translator
136 * @param string|null $translationDomain The translation domain to
137 * use for translating
140 * @internal Called by {@link ExecutionContextFactory}. Should not be used
143 public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
145 $this->validator = $validator;
147 $this->translator = $translator;
148 $this->translationDomain = $translationDomain;
149 $this->violations = new ConstraintViolationList();
155 public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
157 $this->value = $value;
158 $this->object = $object;
159 $this->metadata = $metadata;
160 $this->propertyPath = (string) $propertyPath;
166 public function setGroup($group)
168 $this->group = $group;
174 public function setConstraint(Constraint $constraint)
176 $this->constraint = $constraint;
182 public function addViolation($message, array $parameters = array())
184 $this->violations->add(new ConstraintViolation(
185 $this->translator->trans($message, $parameters, $this->translationDomain),
200 public function buildViolation($message, array $parameters = array())
202 return new ConstraintViolationBuilder(
211 $this->translationDomain
218 public function getViolations()
220 return $this->violations;
226 public function getValidator()
228 return $this->validator;
234 public function getRoot()
242 public function getValue()
250 public function getObject()
252 return $this->object;
258 public function getMetadata()
260 return $this->metadata;
266 public function getGroup()
271 public function getConstraint()
273 return $this->constraint;
279 public function getClassName()
281 return $this->metadata instanceof MemberMetadata || $this->metadata instanceof ClassMetadataInterface ? $this->metadata->getClassName() : null;
287 public function getPropertyName()
289 return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
295 public function getPropertyPath($subPath = '')
297 return PropertyPath::append($this->propertyPath, $subPath);
303 public function markGroupAsValidated($cacheKey, $groupHash)
305 if (!isset($this->validatedObjects[$cacheKey])) {
306 $this->validatedObjects[$cacheKey] = array();
309 $this->validatedObjects[$cacheKey][$groupHash] = true;
315 public function isGroupValidated($cacheKey, $groupHash)
317 return isset($this->validatedObjects[$cacheKey][$groupHash]);
323 public function markConstraintAsValidated($cacheKey, $constraintHash)
325 $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
331 public function isConstraintValidated($cacheKey, $constraintHash)
333 return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
339 public function markObjectAsInitialized($cacheKey)
341 $this->initializedObjects[$cacheKey] = true;
347 public function isObjectInitialized($cacheKey)
349 return isset($this->initializedObjects[$cacheKey]);