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;
14 @trigger_error('The '.__NAMESPACE__.'\ExecutionContext class is deprecated since version 2.5 and will be removed in 3.0. Use the Symfony\Component\Validator\Context\ExecutionContext class instead.', E_USER_DEPRECATED);
16 use Symfony\Component\Translation\TranslatorInterface;
19 * Default implementation of {@link ExecutionContextInterface}.
21 * This class is immutable by design.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Bernhard Schussek <bschussek@gmail.com>
26 * @deprecated since version 2.5, to be removed in 3.0.
27 * Use {@link Context\ExecutionContext} instead.
29 class ExecutionContext implements ExecutionContextInterface
32 * @var GlobalExecutionContextInterface
34 private $globalContext;
37 * @var TranslatorInterface
44 private $translationDomain;
47 * @var MetadataInterface
64 private $propertyPath;
67 * Creates a new execution context.
69 * @param GlobalExecutionContextInterface $globalContext The global context storing node-independent state
70 * @param TranslatorInterface $translator The translator for translating violation messages
71 * @param null|string $translationDomain The domain of the validation messages
72 * @param MetadataInterface $metadata The metadata of the validated node
73 * @param mixed $value The value of the validated node
74 * @param string $group The current validation group
75 * @param string $propertyPath The property path to the current node
77 public function __construct(GlobalExecutionContextInterface $globalContext, TranslatorInterface $translator, $translationDomain = null, MetadataInterface $metadata = null, $value = null, $group = null, $propertyPath = '')
79 if (null === $group) {
80 $group = Constraint::DEFAULT_GROUP;
83 $this->globalContext = $globalContext;
84 $this->translator = $translator;
85 $this->translationDomain = $translationDomain;
86 $this->metadata = $metadata;
87 $this->value = $value;
88 $this->propertyPath = $propertyPath;
89 $this->group = $group;
95 public function addViolation($message, array $params = array(), $invalidValue = null, $plural = null, $code = null)
97 if (null === $plural) {
98 $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
101 $translatedMessage = $this->translator->transChoice($message, $plural, $params, $this->translationDomain);
102 } catch (\InvalidArgumentException $e) {
103 $translatedMessage = $this->translator->trans($message, $params, $this->translationDomain);
107 $this->globalContext->getViolations()->add(new ConstraintViolation(
111 $this->globalContext->getRoot(),
113 // check using func_num_args() to allow passing null values
114 func_num_args() >= 3 ? $invalidValue : $this->value,
123 public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $plural = null, $code = null)
125 $this->globalContext->getViolations()->add(new ConstraintViolation(
127 ? $this->translator->trans($message, $parameters, $this->translationDomain)
128 : $this->translator->transChoice($message, $plural, $parameters, $this->translationDomain),
131 $this->globalContext->getRoot(),
132 $this->getPropertyPath($subPath),
133 // check using func_num_args() to allow passing null values
134 func_num_args() >= 4 ? $invalidValue : $this->value,
143 public function getViolations()
145 return $this->globalContext->getViolations();
151 public function getRoot()
153 return $this->globalContext->getRoot();
159 public function getPropertyPath($subPath = '')
161 if ('' != $subPath && '' !== $this->propertyPath && '[' !== $subPath[0]) {
162 return $this->propertyPath.'.'.$subPath;
165 return $this->propertyPath.$subPath;
171 public function getClassName()
173 if ($this->metadata instanceof ClassBasedInterface) {
174 return $this->metadata->getClassName();
181 public function getPropertyName()
183 if ($this->metadata instanceof PropertyMetadataInterface) {
184 return $this->metadata->getPropertyName();
191 public function getValue()
199 public function getGroup()
207 public function getMetadata()
209 return $this->metadata;
215 public function getMetadataFor($value)
217 return $this->globalContext->getMetadataFactory()->getMetadataFor($value);
223 public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
225 $propertyPath = $this->getPropertyPath($subPath);
227 foreach ($this->resolveGroups($groups) as $group) {
228 $this->globalContext->getVisitor()->validate($value, $group, $propertyPath, $traverse, $deep);
235 public function validateValue($value, $constraints, $subPath = '', $groups = null)
237 $constraints = is_array($constraints) ? $constraints : array($constraints);
239 if (null === $groups && '' === $subPath) {
240 $context = clone $this;
241 $context->value = $value;
242 $context->executeConstraintValidators($value, $constraints);
247 $propertyPath = $this->getPropertyPath($subPath);
249 foreach ($this->resolveGroups($groups) as $group) {
250 $context = clone $this;
251 $context->value = $value;
252 $context->group = $group;
253 $context->propertyPath = $propertyPath;
254 $context->executeConstraintValidators($value, $constraints);
261 public function getMetadataFactory()
263 return $this->globalContext->getMetadataFactory();
267 * Executes the validators of the given constraints for the given value.
269 * @param mixed $value The value to validate
270 * @param Constraint[] $constraints The constraints to match against
272 private function executeConstraintValidators($value, array $constraints)
274 foreach ($constraints as $constraint) {
275 $validator = $this->globalContext->getValidatorFactory()->getInstance($constraint);
276 $validator->initialize($this);
277 $validator->validate($value, $constraint);
282 * Returns an array of group names.
284 * @param null|string|string[] $groups The groups to resolve. If a single string is
285 * passed, it is converted to an array. If null
286 * is passed, an array containing the current
287 * group of the context is returned.
289 * @return array An array of validation groups
291 private function resolveGroups($groups)
293 return $groups ? (array) $groups : (array) $this->group;