db backup prior to drupal security update
[yaffs-website] / vendor / symfony / validator / ValidationVisitor.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__.'\ValidationVisitor class is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
15
16 use Symfony\Component\Translation\TranslatorInterface;
17 use Symfony\Component\Validator\Exception\NoSuchMetadataException;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20 /**
21  * Default implementation of {@link ValidationVisitorInterface} and
22  * {@link GlobalExecutionContextInterface}.
23  *
24  * @author Bernhard Schussek <bschussek@gmail.com>
25  *
26  * @deprecated since version 2.5, to be removed in 3.0.
27  */
28 class ValidationVisitor implements ValidationVisitorInterface, GlobalExecutionContextInterface
29 {
30     /**
31      * @var mixed
32      */
33     private $root;
34
35     /**
36      * @var MetadataFactoryInterface
37      */
38     private $metadataFactory;
39
40     /**
41      * @var ConstraintValidatorFactoryInterface
42      */
43     private $validatorFactory;
44
45     /**
46      * @var TranslatorInterface
47      */
48     private $translator;
49
50     /**
51      * @var null|string
52      */
53     private $translationDomain;
54
55     /**
56      * @var array
57      */
58     private $objectInitializers;
59
60     /**
61      * @var ConstraintViolationList
62      */
63     private $violations;
64
65     /**
66      * @var array
67      */
68     private $validatedObjects = array();
69
70     /**
71      * Creates a new validation visitor.
72      *
73      * @param mixed                               $root               The value passed to the validator
74      * @param MetadataFactoryInterface            $metadataFactory    The factory for obtaining metadata instances
75      * @param ConstraintValidatorFactoryInterface $validatorFactory   The factory for creating constraint validators
76      * @param TranslatorInterface                 $translator         The translator for translating violation messages
77      * @param string|null                         $translationDomain  The domain of the translation messages
78      * @param ObjectInitializerInterface[]        $objectInitializers The initializers for preparing objects before validation
79      *
80      * @throws UnexpectedTypeException If any of the object initializers is not an instance of ObjectInitializerInterface
81      */
82     public function __construct($root, MetadataFactoryInterface $metadataFactory, ConstraintValidatorFactoryInterface $validatorFactory, TranslatorInterface $translator, $translationDomain = null, array $objectInitializers = array())
83     {
84         foreach ($objectInitializers as $initializer) {
85             if (!$initializer instanceof ObjectInitializerInterface) {
86                 throw new UnexpectedTypeException($initializer, 'Symfony\Component\Validator\ObjectInitializerInterface');
87             }
88         }
89
90         $this->root = $root;
91         $this->metadataFactory = $metadataFactory;
92         $this->validatorFactory = $validatorFactory;
93         $this->translator = $translator;
94         $this->translationDomain = $translationDomain;
95         $this->objectInitializers = $objectInitializers;
96         $this->violations = new ConstraintViolationList();
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function visit(MetadataInterface $metadata, $value, $group, $propertyPath)
103     {
104         $context = new ExecutionContext(
105             $this,
106             $this->translator,
107             $this->translationDomain,
108             $metadata,
109             $value,
110             $group,
111             $propertyPath
112         );
113
114         $context->validateValue($value, $metadata->findConstraints($group));
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function validate($value, $group, $propertyPath, $traverse = false, $deep = false)
121     {
122         if (null === $value) {
123             return;
124         }
125
126         if (is_object($value)) {
127             $hash = spl_object_hash($value);
128
129             // Exit, if the object is already validated for the current group
130             if (isset($this->validatedObjects[$hash][$group])) {
131                 return;
132             }
133
134             // Initialize if the object wasn't initialized before
135             if (!isset($this->validatedObjects[$hash])) {
136                 foreach ($this->objectInitializers as $initializer) {
137                     if (!$initializer instanceof ObjectInitializerInterface) {
138                         throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
139                     }
140                     $initializer->initialize($value);
141                 }
142             }
143
144             // Remember validating this object before starting and possibly
145             // traversing the object graph
146             $this->validatedObjects[$hash][$group] = true;
147         }
148
149         // Validate arrays recursively by default, otherwise every driver needs
150         // to implement special handling for arrays.
151         // https://github.com/symfony/symfony/issues/6246
152         if (is_array($value) || ($traverse && $value instanceof \Traversable)) {
153             foreach ($value as $key => $element) {
154                 // Ignore any scalar values in the collection
155                 if (is_object($element) || is_array($element)) {
156                     // Only repeat the traversal if $deep is set
157                     $this->validate($element, $group, $propertyPath.'['.$key.']', $deep, $deep);
158                 }
159             }
160
161             try {
162                 $this->metadataFactory->getMetadataFor($value)->accept($this, $value, $group, $propertyPath);
163             } catch (NoSuchMetadataException $e) {
164                 // Metadata doesn't necessarily have to exist for
165                 // traversable objects, because we know how to validate
166                 // them anyway. Optionally, additional metadata is supported.
167             }
168         } else {
169             $this->metadataFactory->getMetadataFor($value)->accept($this, $value, $group, $propertyPath);
170         }
171     }
172
173     /**
174      * {@inheritdoc}
175      */
176     public function getViolations()
177     {
178         return $this->violations;
179     }
180
181     /**
182      * {@inheritdoc}
183      */
184     public function getRoot()
185     {
186         return $this->root;
187     }
188
189     /**
190      * {@inheritdoc}
191      */
192     public function getVisitor()
193     {
194         return $this;
195     }
196
197     /**
198      * {@inheritdoc}
199      */
200     public function getValidatorFactory()
201     {
202         return $this->validatorFactory;
203     }
204
205     /**
206      * {@inheritdoc}
207      */
208     public function getMetadataFactory()
209     {
210         return $this->metadataFactory;
211     }
212 }