Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Validator.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;
13
14 @trigger_error('The '.__NAMESPACE__.'\Validator class is deprecated since version 2.5 and will be removed in 3.0. Use the Symfony\Component\Validator\Validator\RecursiveValidator class instead.', E_USER_DEPRECATED);
15
16 use Symfony\Component\Translation\TranslatorInterface;
17 use Symfony\Component\Validator\Constraints\Valid;
18 use Symfony\Component\Validator\Exception\ValidatorException;
19
20 /**
21  * Default implementation of {@link ValidatorInterface}.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  * @author Bernhard Schussek <bschussek@gmail.com>
25  *
26  * @deprecated since version 2.5, to be removed in 3.0.
27  *             Use {@link Validator\RecursiveValidator} instead.
28  */
29 class Validator implements ValidatorInterface, Mapping\Factory\MetadataFactoryInterface
30 {
31     /**
32      * @var MetadataFactoryInterface
33      */
34     private $metadataFactory;
35
36     /**
37      * @var ConstraintValidatorFactoryInterface
38      */
39     private $validatorFactory;
40
41     /**
42      * @var TranslatorInterface
43      */
44     private $translator;
45
46     /**
47      * @var null|string
48      */
49     private $translationDomain;
50
51     /**
52      * @var array
53      */
54     private $objectInitializers;
55
56     public function __construct(
57         MetadataFactoryInterface $metadataFactory,
58         ConstraintValidatorFactoryInterface $validatorFactory,
59         TranslatorInterface $translator,
60         $translationDomain = 'validators',
61         array $objectInitializers = array()
62     ) {
63         $this->metadataFactory = $metadataFactory;
64         $this->validatorFactory = $validatorFactory;
65         $this->translator = $translator;
66         $this->translationDomain = $translationDomain;
67         $this->objectInitializers = $objectInitializers;
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     public function getMetadataFactory()
74     {
75         return $this->metadataFactory;
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     public function getMetadataFor($value)
82     {
83         return $this->metadataFactory->getMetadataFor($value);
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function hasMetadataFor($value)
90     {
91         return $this->metadataFactory->hasMetadataFor($value);
92     }
93
94     /**
95      * {@inheritdoc}
96      */
97     public function validate($value, $groups = null, $traverse = false, $deep = false)
98     {
99         $visitor = $this->createVisitor($value);
100
101         foreach ($this->resolveGroups($groups) as $group) {
102             $visitor->validate($value, $group, '', $traverse, $deep);
103         }
104
105         return $visitor->getViolations();
106     }
107
108     /**
109      * {@inheritdoc}
110      *
111      * @throws ValidatorException If the metadata for the value does not support properties.
112      */
113     public function validateProperty($containingValue, $property, $groups = null)
114     {
115         $visitor = $this->createVisitor($containingValue);
116         $metadata = $this->metadataFactory->getMetadataFor($containingValue);
117
118         if (!$metadata instanceof PropertyMetadataContainerInterface) {
119             $valueAsString = is_scalar($containingValue)
120                 ? '"'.$containingValue.'"'
121                 : 'the value of type '.gettype($containingValue);
122
123             throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
124         }
125
126         foreach ($this->resolveGroups($groups) as $group) {
127             if (!$metadata->hasPropertyMetadata($property)) {
128                 continue;
129             }
130
131             foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
132                 $propMeta->accept($visitor, $propMeta->getPropertyValue($containingValue), $group, $property);
133             }
134         }
135
136         return $visitor->getViolations();
137     }
138
139     /**
140      * {@inheritdoc}
141      *
142      * @throws ValidatorException If the metadata for the value does not support properties.
143      */
144     public function validatePropertyValue($containingValue, $property, $value, $groups = null)
145     {
146         $visitor = $this->createVisitor(is_object($containingValue) ? $containingValue : $value);
147         $metadata = $this->metadataFactory->getMetadataFor($containingValue);
148
149         if (!$metadata instanceof PropertyMetadataContainerInterface) {
150             $valueAsString = is_scalar($containingValue)
151                 ? '"'.$containingValue.'"'
152                 : 'the value of type '.gettype($containingValue);
153
154             throw new ValidatorException(sprintf('The metadata for %s does not support properties.', $valueAsString));
155         }
156
157         // If $containingValue is passed as class name, take $value as root
158         // and start the traversal with an empty property path
159         $propertyPath = is_object($containingValue) ? $property : '';
160
161         foreach ($this->resolveGroups($groups) as $group) {
162             if (!$metadata->hasPropertyMetadata($property)) {
163                 continue;
164             }
165
166             foreach ($metadata->getPropertyMetadata($property) as $propMeta) {
167                 $propMeta->accept($visitor, $value, $group, $propertyPath);
168             }
169         }
170
171         return $visitor->getViolations();
172     }
173
174     /**
175      * {@inheritdoc}
176      */
177     public function validateValue($value, $constraints, $groups = null)
178     {
179         $context = new ExecutionContext($this->createVisitor($value), $this->translator, $this->translationDomain);
180
181         $constraints = is_array($constraints) ? $constraints : array($constraints);
182
183         foreach ($constraints as $constraint) {
184             if ($constraint instanceof Valid) {
185                 // Why can't the Valid constraint be executed directly?
186                 //
187                 // It cannot be executed like regular other constraints, because regular
188                 // constraints are only executed *if they belong to the validated group*.
189                 // The Valid constraint, on the other hand, is always executed and propagates
190                 // the group to the cascaded object. The propagated group depends on
191                 //
192                 //  * Whether a group sequence is currently being executed. Then the default
193                 //    group is propagated.
194                 //
195                 //  * Otherwise the validated group is propagated.
196
197                 throw new ValidatorException(
198                     sprintf(
199                         'The constraint %s cannot be validated. Use the method validate() instead.',
200                         get_class($constraint)
201                     )
202                 );
203             }
204
205             $context->validateValue($value, $constraint, '', $groups);
206         }
207
208         return $context->getViolations();
209     }
210
211     /**
212      * @param mixed $root
213      *
214      * @return ValidationVisitor
215      */
216     private function createVisitor($root)
217     {
218         return new ValidationVisitor(
219             $root,
220             $this->metadataFactory,
221             $this->validatorFactory,
222             $this->translator,
223             $this->translationDomain,
224             $this->objectInitializers
225         );
226     }
227
228     /**
229      * @param null|string|string[] $groups
230      *
231      * @return string[]
232      */
233     private function resolveGroups($groups)
234     {
235         return $groups ? (array) $groups : array(Constraint::DEFAULT_GROUP);
236     }
237 }