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\MetadataInterface;
20 use Symfony\Component\Validator\Mapping\MemberMetadata;
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 You should not instantiate or use this class. Code against
34 * {@link ExecutionContextInterface} instead.
36 class ExecutionContext implements ExecutionContextInterface
39 * @var ValidatorInterface
44 * The root value of the validated object graph.
51 * @var TranslatorInterface
58 private $translationDomain;
61 * The violations generated in the current context.
63 * @var ConstraintViolationList
68 * The currently validated value.
75 * The currently validated object.
82 * The property path leading to the current value.
86 private $propertyPath = '';
89 * The current validation metadata.
91 * @var MetadataInterface|null
96 * The currently validated group.
103 * The currently validated constraint.
105 * @var Constraint|null
110 * Stores which objects have been validated in which group.
114 private $validatedObjects = array();
117 * Stores which class constraint has been validated for which object.
121 private $validatedConstraints = array();
124 * Stores which objects have been initialized.
128 private $initializedObjects;
131 * Creates a new execution context.
133 * @param ValidatorInterface $validator The validator
134 * @param mixed $root The root value of the
135 * validated object graph
136 * @param TranslatorInterface $translator The translator
137 * @param string|null $translationDomain The translation domain to
138 * use for translating
141 * @internal Called by {@link ExecutionContextFactory}. Should not be used
144 public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
146 $this->validator = $validator;
148 $this->translator = $translator;
149 $this->translationDomain = $translationDomain;
150 $this->violations = new ConstraintViolationList();
156 public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
158 $this->value = $value;
159 $this->object = $object;
160 $this->metadata = $metadata;
161 $this->propertyPath = (string) $propertyPath;
167 public function setGroup($group)
169 $this->group = $group;
175 public function setConstraint(Constraint $constraint)
177 $this->constraint = $constraint;
183 public function addViolation($message, array $parameters = array())
185 $this->violations->add(new ConstraintViolation(
186 $this->translator->trans($message, $parameters, $this->translationDomain),
201 public function buildViolation($message, array $parameters = array())
203 return new ConstraintViolationBuilder(
212 $this->translationDomain
219 public function getViolations()
221 return $this->violations;
227 public function getValidator()
229 return $this->validator;
235 public function getRoot()
243 public function getValue()
251 public function getObject()
253 return $this->object;
259 public function getMetadata()
261 return $this->metadata;
267 public function getGroup()
272 public function getConstraint()
274 return $this->constraint;
280 public function getClassName()
282 return $this->metadata instanceof MemberMetadata || $this->metadata instanceof ClassMetadataInterface ? $this->metadata->getClassName() : null;
288 public function getPropertyName()
290 return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
296 public function getPropertyPath($subPath = '')
298 return PropertyPath::append($this->propertyPath, $subPath);
304 public function markGroupAsValidated($cacheKey, $groupHash)
306 if (!isset($this->validatedObjects[$cacheKey])) {
307 $this->validatedObjects[$cacheKey] = array();
310 $this->validatedObjects[$cacheKey][$groupHash] = true;
316 public function isGroupValidated($cacheKey, $groupHash)
318 return isset($this->validatedObjects[$cacheKey][$groupHash]);
324 public function markConstraintAsValidated($cacheKey, $constraintHash)
326 $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
332 public function isConstraintValidated($cacheKey, $constraintHash)
334 return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
340 public function markObjectAsInitialized($cacheKey)
342 $this->initializedObjects[$cacheKey] = true;
348 public function isObjectInitialized($cacheKey)
350 return isset($this->initializedObjects[$cacheKey]);