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\ClassBasedInterface;
16 use Symfony\Component\Validator\Constraint;
17 use Symfony\Component\Validator\Constraints\Valid;
18 use Symfony\Component\Validator\ConstraintViolation;
19 use Symfony\Component\Validator\ConstraintViolationList;
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\ValidatorInterface as LegacyValidatorInterface;
25 use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
28 * The context used and created by {@link ExecutionContextFactory}.
30 * @author Bernhard Schussek <bschussek@gmail.com>
32 * @see ExecutionContextInterface
34 * @internal You should not instantiate or use this class. Code against
35 * {@link ExecutionContextInterface} instead.
37 class ExecutionContext implements ExecutionContextInterface
40 * @var ValidatorInterface
45 * The root value of the validated object graph.
52 * @var TranslatorInterface
59 private $translationDomain;
62 * The violations generated in the current context.
64 * @var ConstraintViolationList
69 * The currently validated value.
76 * The currently validated object.
83 * The property path leading to the current value.
87 private $propertyPath = '';
90 * The current validation metadata.
92 * @var MetadataInterface|null
97 * The currently validated group.
104 * The currently validated constraint.
106 * @var Constraint|null
111 * Stores which objects have been validated in which group.
115 private $validatedObjects = array();
118 * Stores which class constraint has been validated for which object.
122 private $validatedConstraints = array();
125 * Stores which objects have been initialized.
129 private $initializedObjects;
132 * Creates a new execution context.
134 * @param ValidatorInterface $validator The validator
135 * @param mixed $root The root value of the
136 * validated object graph
137 * @param TranslatorInterface $translator The translator
138 * @param string|null $translationDomain The translation domain to
139 * use for translating
142 * @internal Called by {@link ExecutionContextFactory}. Should not be used
145 public function __construct(ValidatorInterface $validator, $root, TranslatorInterface $translator, $translationDomain = null)
147 $this->validator = $validator;
149 $this->translator = $translator;
150 $this->translationDomain = $translationDomain;
151 $this->violations = new ConstraintViolationList();
157 public function setNode($value, $object, MetadataInterface $metadata = null, $propertyPath)
159 $this->value = $value;
160 $this->object = $object;
161 $this->metadata = $metadata;
162 $this->propertyPath = (string) $propertyPath;
168 public function setGroup($group)
170 $this->group = $group;
176 public function setConstraint(Constraint $constraint)
178 $this->constraint = $constraint;
184 public function addViolation($message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
186 // The parameters $invalidValue and following are ignored by the new
187 // API, as they are not present in the new interface anymore.
188 // You should use buildViolation() instead.
189 if (func_num_args() > 2) {
190 @trigger_error('The parameters $invalidValue, $plural and $code in method '.__METHOD__.' are deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
193 ->buildViolation($message, $parameters)
194 ->setInvalidValue($invalidValue)
203 $this->violations->add(new ConstraintViolation(
204 $this->translator->trans($message, $parameters, $this->translationDomain),
219 public function buildViolation($message, array $parameters = array())
221 return new ConstraintViolationBuilder(
230 $this->translationDomain
237 public function getViolations()
239 return $this->violations;
245 public function getValidator()
247 return $this->validator;
253 public function getRoot()
261 public function getValue()
269 public function getObject()
271 return $this->object;
277 public function getMetadata()
279 return $this->metadata;
285 public function getGroup()
290 public function getConstraint()
292 return $this->constraint;
298 public function getClassName()
300 return $this->metadata instanceof ClassBasedInterface ? $this->metadata->getClassName() : null;
306 public function getPropertyName()
308 return $this->metadata instanceof PropertyMetadataInterface ? $this->metadata->getPropertyName() : null;
314 public function getPropertyPath($subPath = '')
316 return PropertyPath::append($this->propertyPath, $subPath);
322 public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
324 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::buildViolation method instead.', E_USER_DEPRECATED);
326 if (func_num_args() > 2) {
328 ->buildViolation($message, $parameters)
330 ->setInvalidValue($invalidValue)
340 ->buildViolation($message, $parameters)
349 public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
351 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
353 if (is_array($value)) {
354 // The $traverse flag is ignored for arrays
355 $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
361 ->validate($value, $constraint, $groups)
365 if ($traverse && $value instanceof \Traversable) {
366 $constraint = new Valid(array('traverse' => true, 'deep' => $deep));
372 ->validate($value, $constraint, $groups)
380 ->validate($value, null, $groups)
387 public function validateValue($value, $constraints, $subPath = '', $groups = null)
389 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use the '.__CLASS__.'::getValidator() method instead.', E_USER_DEPRECATED);
395 ->validate($value, $constraints, $groups)
402 public function getMetadataFactory()
404 @trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0. Use the new Symfony\Component\Validator\Context\ExecutionContext::getValidator method in combination with Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
406 $validator = $this->getValidator();
408 if ($validator instanceof LegacyValidatorInterface) {
409 return $validator->getMetadataFactory();
412 // The ValidatorInterface extends from the deprecated MetadataFactoryInterface, so return it when we don't have the factory instance itself
419 public function markGroupAsValidated($cacheKey, $groupHash)
421 if (!isset($this->validatedObjects[$cacheKey])) {
422 $this->validatedObjects[$cacheKey] = array();
425 $this->validatedObjects[$cacheKey][$groupHash] = true;
431 public function isGroupValidated($cacheKey, $groupHash)
433 return isset($this->validatedObjects[$cacheKey][$groupHash]);
439 public function markConstraintAsValidated($cacheKey, $constraintHash)
441 $this->validatedConstraints[$cacheKey.':'.$constraintHash] = true;
447 public function isConstraintValidated($cacheKey, $constraintHash)
449 return isset($this->validatedConstraints[$cacheKey.':'.$constraintHash]);
455 public function markObjectAsInitialized($cacheKey)
457 $this->initializedObjects[$cacheKey] = true;
463 public function isObjectInitialized($cacheKey)
465 return isset($this->initializedObjects[$cacheKey]);