[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / AbstractCompositeValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * An abstract composite validator consisting of other validators
19 *
20 * @api
21 */
22 abstract class AbstractCompositeValidator implements ObjectValidatorInterface, \Countable {
23
24 /**
25 * This contains the supported options, their default values and descriptions.
26 *
27 * @var array
28 */
29 protected $supportedOptions = array();
30
31 /**
32 * @var array
33 */
34 protected $options = array();
35
36 /**
37 * @var \SplObjectStorage
38 */
39 protected $validators;
40
41 /**
42 * @var \SplObjectStorage
43 */
44 protected $validatedInstancesContainer;
45
46 /**
47 * @var array
48 */
49 protected $errors = array();
50
51 /**
52 * Constructs the composite validator and sets validation options
53 *
54 * @param array $options Options for the validator
55 * @api
56 */
57 public function __construct(array $options = array()) {
58 // check for options given but not supported
59 if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== array()) {
60 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1339079804);
61 }
62
63 // check for required options being set
64 array_walk(
65 $this->supportedOptions,
66 function($supportedOptionData, $supportedOptionName, $options) {
67 if (isset($supportedOptionData[3]) && !array_key_exists($supportedOptionName, $options)) {
68 throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1339163922);
69 }
70 },
71 $options
72 );
73
74 // merge with default values
75 $this->options = array_merge(
76 array_map(
77 function ($value) {
78 return $value[0];
79 },
80 $this->supportedOptions
81 ),
82 $options
83 );
84 $this->validators = new \SplObjectStorage();
85 }
86
87 /**
88 * Does nothing.
89 *
90 * @param array $options Not used
91 * @return void
92 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
93 */
94 public function setOptions(array $options) {
95 }
96
97 /**
98 * Returns an array of errors which occurred during the last isValid() call.
99 *
100 * @return array An array of \TYPO3\CMS\Extbase\Validation\Error objects or an empty array if no errors occurred.
101 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
102 */
103 public function getErrors() {
104 return $this->errors;
105 }
106
107 /**
108 * Adds a new validator to the conjunction.
109 *
110 * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The validator that should be added
111 * @return void
112 * @api
113 */
114 public function addValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
115 if ($validator instanceof ObjectValidatorInterface) {
116 // @todo: provide bugfix as soon as it is fixed in TYPO3.Flow (http://forge.typo3.org/issues/48093)
117 $validator->setValidatedInstancesContainer = $this->validatedInstancesContainer;
118 }
119 $this->validators->attach($validator);
120 }
121
122 /**
123 * Removes the specified validator.
124 *
125 * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The validator to remove
126 * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
127 * @return void
128 */
129 public function removeValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
130 if (!$this->validators->contains($validator)) {
131 throw new \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException('Cannot remove validator because its not in the conjunction.', 1207020177);
132 }
133 $this->validators->detach($validator);
134 }
135
136 /**
137 * Returns the number of validators contained in this conjunction.
138 *
139 * @return integer The number of validators
140 * @api
141 */
142 public function count() {
143 return count($this->validators);
144 }
145
146 /**
147 * Returns the child validators of this Composite Validator
148 *
149 * @return \SplObjectStorage
150 */
151 public function getValidators() {
152 return $this->validators;
153 }
154
155 /**
156 * Returns the options for this validator
157 *
158 * @return array
159 */
160 public function getOptions() {
161 return $this->options;
162 }
163
164 /**
165 * Allows to set a container to keep track of validated instances.
166 *
167 * @param \SplObjectStorage $validatedInstancesContainer A container to keep track of validated instances
168 * @return void
169 * @api
170 */
171 public function setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer) {
172 $this->validatedInstancesContainer = $validatedInstancesContainer;
173 }
174
175 /**
176 * Checks the given object can be validated by the validator implementation
177 *
178 * @param object $object The object to be checked
179 * @return boolean TRUE if this validator can validate instances of the given object or FALSE if it can't
180 *
181 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
182 */
183 public function canValidate($object) {
184 // deliberately empty
185 }
186
187 /**
188 * Checks if the specified property of the given object is valid.
189 *
190 * If at least one error occurred, the result is FALSE.
191 *
192 * @param object $object The object containing the property to validate
193 * @param string $propertyName Name of the property to validate
194 * @return boolean TRUE if the property value is valid, FALSE if an error occurred
195 *
196 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
197 */
198 public function isPropertyValid($object, $propertyName) {
199 // deliberately empty
200 }
201 }