TYPO3 CMS  TYPO3_7-6
ValidationElementValidator.php
Go to the documentation of this file.
1 <?php
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 
18 
23 {
27  protected $propertyValidators = [];
28 
32  protected $sessionUtility;
33 
37  public function injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)
38  {
39  $this->sessionUtility = $sessionUtility;
40  }
41 
50  public function validate($value)
51  {
52  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
53  if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
54  if (!is_object($value)) {
55  $this->addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
56  } elseif ($this->isValidatedAlready($value) === false) {
57  $this->isValid($value);
58  }
59  }
60 
61  return $this->result;
62  }
63 
73  protected function getPropertyValue(\TYPO3\CMS\Form\Domain\Model\ValidationElement $validationElement, $propertyName)
74  {
82  if ($this->sessionUtility->getSessionData($propertyName)) {
83  $propertyValue = $this->sessionUtility->getSessionData($propertyName);
84  } else {
85  $propertyValue = $validationElement->getIncomingField($propertyName);
86  }
87  return $propertyValue;
88  }
89 
99  protected function checkProperty($value, $validators, $propertyName)
100  {
102  $result = null;
103  foreach ($validators as $validator) {
104  if ($validator instanceof ObjectValidatorInterface) {
105  $validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
106  }
107 
115  if (
116  isset($value[0]['name'])
117  && isset($value[0]['type'])
118  && isset($value[0]['tmp_name'])
119  && isset($value[0]['size'])
120  ) {
121  foreach ($value as $file) {
122  $currentResult = $validator->validate($file);
123  if ($currentResult->hasMessages()) {
124  if ($result == null) {
125  $result = $currentResult;
126  } else {
127  $result->merge($currentResult);
128  }
129  }
130  }
131  } else {
132  $currentResult = $validator->validate($value);
133  if ($currentResult->hasMessages()) {
134  if ($result == null) {
135  $result = $currentResult;
136  } else {
137  $result->merge($currentResult);
138  }
139  }
140  }
141  }
142  if ($result != null) {
143  $this->result->forProperty($propertyName)->merge($result);
144  }
145  }
146 
154  protected function isValid($object)
155  {
156  foreach ($this->propertyValidators as $propertyName => $validators) {
157  $propertyValue = $this->getPropertyValue($object, $propertyName);
158  $this->checkProperty($propertyValue, $validators, $propertyName);
159  }
160  }
161 
169  public function canValidate($object)
170  {
171  if (
172  is_object($object)
173  && $object instanceof \TYPO3\CMS\Form\Domain\Model\ValidationElement
174  ) {
175  return true;
176  }
177  return false;
178  }
179 
188  public function addPropertyValidator($propertyName, ValidatorInterface $validator)
189  {
190  if (!isset($this->propertyValidators[$propertyName])) {
191  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
192  }
193  $this->propertyValidators[$propertyName]->attach($validator);
194  }
195 
200  protected function isValidatedAlready($object)
201  {
202  if ($this->validatedInstancesContainer === null) {
203  $this->validatedInstancesContainer = new \SplObjectStorage();
204  }
205  if ($this->validatedInstancesContainer->contains($object)) {
206  return true;
207  } else {
208  $this->validatedInstancesContainer->attach($object);
209 
210  return false;
211  }
212  }
213 
220  public function getPropertyValidators($propertyName = null)
221  {
222  if ($propertyName !== null) {
223  return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : [];
224  } else {
226  }
227  }
228 
232  public function countPropertyValidators()
233  {
234  $count = 0;
235  foreach ($this->propertyValidators as $propertyValidators) {
236  $count += $propertyValidators->count();
237  }
238  return $count;
239  }
240 
245 
254  {
255  $this->validatedInstancesContainer = $validatedInstancesContainer;
256  }
257 }
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
addError($message, $code, array $arguments=[], $title='')
addPropertyValidator($propertyName, ValidatorInterface $validator)
getPropertyValue(\TYPO3\CMS\Form\Domain\Model\ValidationElement $validationElement, $propertyName)
injectSessionUtility(\TYPO3\CMS\Form\Utility\SessionUtility $sessionUtility)