[TASK] EXT:form - remove unused url parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Validator / ValidationElementValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\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 use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
18 use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
19
20 /**
21 * A generic object validator which allows for specifying property validators
22 */
23 class ValidationElementValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator implements \TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface {
24
25 /**
26 * @var \SplObjectStorage[]
27 */
28 protected $propertyValidators = array();
29
30 /**
31 * Checks if the given value is valid according to the validator, and returns
32 * the Error Messages object which occurred.
33 *
34 * @param mixed $value The value that should be validated
35 * @return \TYPO3\CMS\Extbase\Error\Result
36 * @api
37 */
38 public function validate($value) {
39 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
40 if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
41 if (!is_object($value)) {
42 $this->addError('Object expected, %1$s given.', 1241099149, array(gettype($value)));
43 } elseif ($this->isValidatedAlready($value) === FALSE) {
44 $this->isValid($value);
45 }
46 }
47
48 return $this->result;
49 }
50
51 /**
52 * Load the property value to be used for validation.
53 *
54 * In case the object is a doctrine proxy, we need to load the real instance first.
55 *
56 * @param \TYPO3\CMS\Form\Domain\Model\ValidationElement $validationElement
57 * @param string $propertyName
58 * @return mixed
59 */
60 protected function getPropertyValue(\TYPO3\CMS\Form\Domain\Model\ValidationElement $validationElement, $propertyName) {
61 return $validationElement->getIncomingField($propertyName);
62 }
63
64 /**
65 * Checks if the specified property of the given object is valid, and adds
66 * found errors to the $messages object.
67 *
68 * @param mixed $value The value to be validated
69 * @param \Traversable $validators The validators to be called on the value
70 * @param string $propertyName Name of ther property to check
71 * @return void
72 */
73 protected function checkProperty($value, $validators, $propertyName) {
74 /** @var \TYPO3\CMS\Extbase\Error\Result $result */
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 */
101 protected function isValid($object) {
102 foreach ($this->propertyValidators as $propertyName => $validators) {
103 $propertyValue = $this->getPropertyValue($object, $propertyName);
104 $this->checkProperty($propertyValue, $validators, $propertyName);
105 }
106 }
107
108 /**
109 * Checks the given object can be validated by the validator implementation
110 *
111 * @param mixed $object The object to be checked
112 * @return bool TRUE if the given value can be validated
113 * @api
114 */
115 public function canValidate($object) {
116 if (
117 is_object($object)
118 && $object instanceof \TYPO3\CMS\Form\Domain\Model\ValidationElement
119 ) {
120 return TRUE;
121 }
122 return FALSE;
123 }
124
125 /**
126 * Adds the given validator for validation of the specified property.
127 *
128 * @param string $propertyName Name of the property to validate
129 * @param ValidatorInterface $validator The property validator
130 * @return void
131 * @api
132 */
133 public function addPropertyValidator($propertyName, ValidatorInterface $validator) {
134 if (!isset($this->propertyValidators[$propertyName])) {
135 $this->propertyValidators[$propertyName] = new \SplObjectStorage();
136 }
137 $this->propertyValidators[$propertyName]->attach($validator);
138 }
139
140 /**
141 * @param object $object
142 * @return bool
143 */
144 protected function isValidatedAlready($object) {
145 if ($this->validatedInstancesContainer === NULL) {
146 $this->validatedInstancesContainer = new \SplObjectStorage();
147 }
148 if ($this->validatedInstancesContainer->contains($object)) {
149 return TRUE;
150 } else {
151 $this->validatedInstancesContainer->attach($object);
152
153 return FALSE;
154 }
155 }
156
157 /**
158 * Returns all property validators - or only validators of the specified property
159 *
160 * @param string $propertyName Name of the property to return validators for
161 * @return array An array of validators
162 */
163 public function getPropertyValidators($propertyName = NULL) {
164 if ($propertyName !== NULL) {
165 return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : array();
166 } else {
167 return $this->propertyValidators;
168 }
169 }
170
171 /**
172 * @return int
173 */
174 public function countPropertyValidators() {
175 $count = 0;
176 foreach ($this->propertyValidators as $propertyValidators) {
177 $count += $propertyValidators->count();
178 }
179 return $count;
180 }
181
182 /**
183 * @var \SplObjectStorage
184 */
185 protected $validatedInstancesContainer;
186
187 /**
188 * Allows to set a container to keep track of validated instances.
189 *
190 * @param \SplObjectStorage $validatedInstancesContainer A container to keep track of validated instances
191 * @return void
192 * @api
193 */
194 public function setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer) {
195 $this->validatedInstancesContainer = $validatedInstancesContainer;
196 }
197
198 }