Version 1
[yaffs-website] / vendor / symfony / validator / Validator / RecursiveValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Validator;
13
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;
23
24 /**
25  * Recursive implementation of {@link ValidatorInterface}.
26  *
27  * @author Bernhard Schussek <bschussek@gmail.com>
28  */
29 class RecursiveValidator implements ValidatorInterface, LegacyValidatorInterface
30 {
31     /**
32      * @var ExecutionContextFactoryInterface
33      */
34     protected $contextFactory;
35
36     /**
37      * @var MetadataFactoryInterface
38      */
39     protected $metadataFactory;
40
41     /**
42      * @var ConstraintValidatorFactoryInterface
43      */
44     protected $validatorFactory;
45
46     /**
47      * @var ObjectInitializerInterface[]
48      */
49     protected $objectInitializers;
50
51     /**
52      * Creates a new validator.
53      *
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
62      */
63     public function __construct(ExecutionContextFactoryInterface $contextFactory, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, array $objectInitializers = array())
64     {
65         $this->contextFactory = $contextFactory;
66         $this->metadataFactory = $metadataFactory;
67         $this->validatorFactory = $validatorFactory;
68         $this->objectInitializers = $objectInitializers;
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function startContext($root = null)
75     {
76         return new RecursiveContextualValidator(
77             $this->contextFactory->createContext($this, $root),
78             $this->metadataFactory,
79             $this->validatorFactory,
80             $this->objectInitializers
81         );
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function inContext(ExecutionContextInterface $context)
88     {
89         return new RecursiveContextualValidator(
90             $context,
91             $this->metadataFactory,
92             $this->validatorFactory,
93             $this->objectInitializers
94         );
95     }
96
97     /**
98      * {@inheritdoc}
99      */
100     public function getMetadataFor($object)
101     {
102         return $this->metadataFactory->getMetadataFor($object);
103     }
104
105     /**
106      * {@inheritdoc}
107      */
108     public function hasMetadataFor($object)
109     {
110         return $this->metadataFactory->hasMetadataFor($object);
111     }
112
113     /**
114      * {@inheritdoc}
115      */
116     public function validate($value, $groups = null, $traverse = false, $deep = false)
117     {
118         $numArgs = func_num_args();
119
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;
124             $groups = $traverse;
125         } else {
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);
127
128             $constraints = new Valid(array('traverse' => $traverse, 'deep' => $deep));
129         }
130
131         return $this->startContext($value)
132             ->validate($value, $constraints, $groups)
133             ->getViolations();
134     }
135
136     /**
137      * {@inheritdoc}
138      */
139     public function validateProperty($object, $propertyName, $groups = null)
140     {
141         return $this->startContext($object)
142             ->validateProperty($object, $propertyName, $groups)
143             ->getViolations();
144     }
145
146     /**
147      * {@inheritdoc}
148      */
149     public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null)
150     {
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)
154             ->getViolations();
155     }
156
157     /**
158      * {@inheritdoc}
159      */
160     public function validateValue($value, $constraints, $groups = null)
161     {
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);
163
164         return $this->validate($value, $constraints, $groups);
165     }
166
167     /**
168      * {@inheritdoc}
169      */
170     public function getMetadataFactory()
171     {
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);
173
174         return $this->metadataFactory;
175     }
176
177     private static function testConstraints($constraints)
178     {
179         return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && (0 === count($constraints) || current($constraints) instanceof Constraint));
180     }
181
182     private static function testGroups($groups)
183     {
184         return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (0 === count($groups) || is_string(current($groups)) || current($groups) instanceof GroupSequence));
185     }
186 }