[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / GenericObjectValidator.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 * A generic object validator which allows for specifying property validators
18 */
19 class GenericObjectValidator extends AbstractValidator implements ObjectValidatorInterface {
20
21 /**
22 * @var array
23 */
24 protected $propertyValidators = array();
25
26 /**
27 * Checks if the given value is valid according to the validator, and returns
28 * the Error Messages object which occurred.
29 *
30 * @param mixed $value The value that should be validated
31 * @return \TYPO3\CMS\Extbase\Error\Result
32 * @api
33 */
34 public function validate($value) {
35 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
36 if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
37 if (!is_object($value)) {
38 $this->addError('Object expected, %1$s given.', 1241099149, array(gettype($value)));
39 } elseif ($this->isValidatedAlready($value) === FALSE) {
40 $this->isValid($value);
41 }
42 }
43
44 return $this->result;
45 }
46
47 /**
48 * Load the property value to be used for validation.
49 *
50 * In case the object is a doctrine proxy, we need to load the real instance first.
51 *
52 * @param object $object
53 * @param string $propertyName
54 * @return mixed
55 */
56 protected function getPropertyValue($object, $propertyName) {
57 // TODO: add support for lazy loading proxies, if needed
58 if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($object, $propertyName)) {
59 return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName);
60 } else {
61 return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName, TRUE);
62 }
63 }
64
65 /**
66 * Checks if the specified property of the given object is valid, and adds
67 * found errors to the $messages object.
68 *
69 * @param mixed $value The value to be validated
70 * @param array $validators The validators to be called on the value
71 * @param string $propertyName Name of ther property to check
72 * @return void
73 */
74 protected function checkProperty($value, $validators, $propertyName) {
75 $result = NULL;
76 foreach ($validators as $validator) {
77 if ($validator instanceof ObjectValidatorInterface) {
78 $validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
79 }
80 $currentResult = $validator->validate($value);
81 if ($currentResult->hasMessages()) {
82 if ($result == NULL) {
83 $result = $currentResult;
84 } else {
85 $result->merge($currentResult);
86 }
87 }
88 }
89 if ($result != NULL) {
90 $this->result->forProperty($propertyName)->merge($result);
91 }
92 }
93
94 /**
95 * Checks if the given value is valid according to the property validators.
96 *
97 * @param mixed $object The value that should be validated
98 * @return void
99 * @api
100 * @todo: method must be protected once the old property mapper is removed
101 */
102 public function isValid($object) {
103 if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
104 foreach ($this->propertyValidators as $propertyName => $validators) {
105 $propertyValue = $this->getPropertyValue($object, $propertyName);
106 $this->checkProperty($propertyValue, $validators, $propertyName);
107 }
108
109 return;
110 } else {
111 /* @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1 */
112 if (!is_object($object)) {
113 $this->addError(
114 $this->translateErrorMessage(
115 'validator.genericobject.noobject',
116 'extbase'
117 ), 1241099148);
118
119 return FALSE;
120 }
121 $result = TRUE;
122 foreach (array_keys($this->propertyValidators) as $propertyName) {
123 if ($this->isPropertyValid($object, $propertyName) === FALSE) {
124 $result = FALSE;
125 }
126 }
127
128 return $result;
129 }
130 }
131
132 /**
133 * Checks the given object can be validated by the validator implementation
134 *
135 * @param object $object The object to be checked
136 * @return boolean TRUE if the given value is an object
137 * @api
138 */
139 public function canValidate($object) {
140 return is_object($object);
141 }
142
143 /**
144 * Checks if the specified property of the given object is valid.
145 *
146 * If at least one error occurred, the result is FALSE.
147 *
148 * @param object $object The object containing the property to validate
149 * @param string $propertyName Name of the property to validate
150 * @throws \InvalidArgumentException
151 * @return boolean TRUE if the property value is valid, FALSE if an error occurred
152 * @api
153 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
154 */
155 public function isPropertyValid($object, $propertyName) {
156 if (!is_object($object)) {
157 throw new \InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1241099149);
158 }
159 if (!isset($this->propertyValidators[$propertyName])) {
160 return TRUE;
161 }
162 $result = TRUE;
163 foreach ($this->propertyValidators[$propertyName] as $validator) {
164 $validator->isValid(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName));
165 if (count($validator->getErrors()) > 0) {
166 $this->addErrorsForProperty($validator->getErrors(), $propertyName);
167 $result = FALSE;
168 }
169 }
170 return $result;
171 }
172
173 /**
174 * @param array $errors Array of \TYPO3\CMS\Extbase\Validation\Error
175 * @param string $propertyName Name of the property to add errors
176 * @return void
177 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
178 */
179 protected function addErrorsForProperty($errors, $propertyName) {
180 if (!isset($this->errors[$propertyName])) {
181 $this->errors[$propertyName] = new \TYPO3\CMS\Extbase\Validation\PropertyError($propertyName);
182 }
183 $this->errors[$propertyName]->addErrors($errors);
184 }
185
186 /**
187 * Adds the given validator for validation of the specified property.
188 *
189 * @param string $propertyName Name of the property to validate
190 * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The property validator
191 * @return void
192 * @api
193 */
194 public function addPropertyValidator($propertyName, \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
195 if (!isset($this->propertyValidators[$propertyName])) {
196 $this->propertyValidators[$propertyName] = new \SplObjectStorage();
197 }
198 $this->propertyValidators[$propertyName]->attach($validator);
199 }
200
201 /**
202 * @param object $object
203 * @return boolean
204 */
205 protected function isValidatedAlready($object) {
206 if ($this->validatedInstancesContainer === NULL) {
207 $this->validatedInstancesContainer = new \SplObjectStorage();
208 }
209 if ($this->validatedInstancesContainer->contains($object)) {
210 return TRUE;
211 } else {
212 $this->validatedInstancesContainer->attach($object);
213
214 return FALSE;
215 }
216 }
217
218 /**
219 * Returns all property validators - or only validators of the specified property
220 *
221 * @param string $propertyName Name of the property to return validators for
222 * @return array An array of validators
223 */
224 public function getPropertyValidators($propertyName = NULL) {
225 if ($propertyName !== NULL) {
226 return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : array();
227 } else {
228 return $this->propertyValidators;
229 }
230 }
231
232 /**
233 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
234 */
235 protected $configurationManager;
236
237 /**
238 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
239 * @return void
240 */
241 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
242 // @todo: remove configuration manager once the old property mapper is removed
243 $this->configurationManager = $configurationManager;
244 }
245
246 /**
247 * @var \SplObjectStorage
248 */
249 protected $validatedInstancesContainer;
250
251 /**
252 * Allows to set a container to keep track of validated instances.
253 *
254 * @param \SplObjectStorage $validatedInstancesContainer A container to keep track of validated instances
255 * @return void
256 * @api
257 */
258 public function setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer) {
259 $this->validatedInstancesContainer = $validatedInstancesContainer;
260 }
261 }