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\Validator;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\Constraints\GroupSequence;
16 use Symfony\Component\Validator\Constraints\Valid;
17 use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
18 use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
19 use Symfony\Component\Validator\Context\ExecutionContextInterface;
20 use Symfony\Component\Validator\MetadataFactoryInterface;
21 use Symfony\Component\Validator\ObjectInitializerInterface;
22 use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
25 * Recursive implementation of {@link ValidatorInterface}.
27 * @author Bernhard Schussek <bschussek@gmail.com>
29 class RecursiveValidator implements ValidatorInterface, LegacyValidatorInterface
32 * @var ExecutionContextFactoryInterface
34 protected $contextFactory;
37 * @var MetadataFactoryInterface
39 protected $metadataFactory;
42 * @var ConstraintValidatorFactoryInterface
44 protected $validatorFactory;
47 * @var ObjectInitializerInterface[]
49 protected $objectInitializers;
52 * Creates a new validator.
54 * @param ExecutionContextFactoryInterface $contextFactory The factory for
55 * creating new contexts
56 * @param MetadataFactoryInterface $metadataFactory The factory for
57 * fetching the metadata
58 * of validated objects
59 * @param ConstraintValidatorFactoryInterface $validatorFactory The factory for creating
60 * constraint validators
61 * @param ObjectInitializerInterface[] $objectInitializers The object initializers
63 public function __construct(ExecutionContextFactoryInterface $contextFactory, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = array())
65 $this->contextFactory = $contextFactory;
66 $this->metadataFactory = $metadataFactory;
67 $this->validatorFactory = $validatorFactory;
68 $this->objectInitializers = $objectInitializers;
74 public function startContext($root = null)
76 return new RecursiveContextualValidator(
77 $this->contextFactory->createContext($this, $root),
78 $this->metadataFactory,
79 $this->validatorFactory,
80 $this->objectInitializers
87 public function inContext(ExecutionContextInterface $context)
89 return new RecursiveContextualValidator(
91 $this->metadataFactory,
92 $this->validatorFactory,
93 $this->objectInitializers
100 public function getMetadataFor($object)
102 return $this->metadataFactory->getMetadataFor($object);
108 public function hasMetadataFor($object)
110 return $this->metadataFactory->hasMetadataFor($object);
116 public function validate($value, $groups = null, $traverse = false, $deep = false)
118 $numArgs = func_num_args();
120 // Use new signature if constraints are given in the second argument
121 if (self::testConstraints($groups) && ($numArgs < 3 || 3 === $numArgs && self::testGroups($traverse))) {
122 // Rename to avoid total confusion ;)
123 $constraints = $groups;
126 @trigger_error('The Symfony\Component\Validator\ValidatorInterface::validate method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\Component\Validator\Validator\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);
128 $constraints = new Valid(array('traverse' => $traverse, 'deep' => $deep));
131 return $this->startContext($value)
132 ->validate($value, $constraints, $groups)
139 public function validateProperty($object, $propertyName, $groups = null)
141 return $this->startContext($object)
142 ->validateProperty($object, $propertyName, $groups)
149 public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
151 // If a class name is passed, take $value as root
152 return $this->startContext(is_object($objectOrClass) ? $objectOrClass : $value)
153 ->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
160 public function validateValue($value, $constraints, $groups = null)
162 @trigger_error('The '.__METHOD__.' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\Component\Validator\Validator\ValidatorInterface::validate method instead.', E_USER_DEPRECATED);
164 return $this->validate($value, $constraints, $groups);
170 public function getMetadataFactory()
172 @trigger_error('The '.__METHOD__.' method is deprecated in version 2.5 and will be removed in version 3.0. Use the Symfony\Component\Validator\Validator\ValidatorInterface::getMetadataFor or Symfony\Component\Validator\Validator\ValidatorInterface::hasMetadataFor method instead.', E_USER_DEPRECATED);
174 return $this->metadataFactory;
177 private static function testConstraints($constraints)
179 return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && (0 === count($constraints) || current($constraints) instanceof Constraint));
182 private static function testGroups($groups)
184 return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (0 === count($groups) || is_string(current($groups)) || current($groups) instanceof GroupSequence));