‪TYPO3CMS  10.4
GenericObjectValidator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
29  protected ‪$propertyValidators = [];
30 
38  public function ‪validate($value)
39  {
40  if (is_object($value) && $this->‪isValidatedAlready($value)) {
41  return ‪$this->result;
42  }
43 
44  $this->result = new ‪Result();
45  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
46  if (!is_object($value)) {
47  $this->‪addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
48  } elseif ($this->‪isValidatedAlready($value) === false) {
49  $this->‪markInstanceAsValidated($value);
50  $this->‪isValid($value);
51  }
52  }
53 
54  return ‪$this->result;
55  }
56 
66  protected function ‪getPropertyValue($object, $propertyName)
67  {
68  // @todo add support for lazy loading proxies, if needed
69  if (‪ObjectAccess::isPropertyGettable($object, $propertyName)) {
70  return ‪ObjectAccess::getProperty($object, $propertyName);
71  }
72  throw new \RuntimeException(
73  sprintf(
74  'Could not get value of property "%s::%s", make sure the property is either public or has a getter get%3$s(), a hasser has%3$s() or an isser is%3$s().',
75  get_class($object),
76  $propertyName,
77  ucfirst($propertyName)
78  ),
79  1546632293
80  );
81  }
82 
91  protected function ‪checkProperty($value, $validators, $propertyName)
92  {
94  ‪$result = null;
95  foreach ($validators as ‪$validator) {
97  ‪$validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
98  }
99  $currentResult = ‪$validator->validate($value);
100  if ($currentResult->hasMessages()) {
101  if (‪$result == null) {
102  ‪$result = $currentResult;
103  } else {
104  ‪$result->merge($currentResult);
105  }
106  }
107  }
108  if (‪$result != null) {
109  $this->result->forProperty($propertyName)->merge(‪$result);
110  }
111  }
112 
118  protected function ‪isValid($object)
119  {
120  foreach ($this->propertyValidators as $propertyName => $validators) {
121  $propertyValue = $this->‪getPropertyValue($object, $propertyName);
122  $this->‪checkProperty($propertyValue, $validators, $propertyName);
123  }
124  }
125 
132  public function ‪canValidate($object)
133  {
134  return is_object($object);
135  }
136 
143  public function ‪addPropertyValidator($propertyName, ‪ValidatorInterface ‪$validator)
144  {
145  if (!isset($this->propertyValidators[$propertyName])) {
146  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
147  }
148  $this->propertyValidators[$propertyName]->attach(‪$validator);
149  }
150 
155  protected function ‪isValidatedAlready($object)
156  {
157  if ($this->validatedInstancesContainer === null) {
158  $this->validatedInstancesContainer = new \SplObjectStorage();
159  }
160  if ($this->validatedInstancesContainer->contains($object)) {
161  return true;
162  }
163 
164  return false;
165  }
166 
170  protected function ‪markInstanceAsValidated($object): void
171  {
172  $this->validatedInstancesContainer->attach($object);
173  }
174 
181  public function ‪getPropertyValidators($propertyName = null)
182  {
183  if ($propertyName !== null) {
184  return $this->propertyValidators[$propertyName] ?? [];
185  }
187  }
188 
193 
199  public function ‪setValidatedInstancesContainer(\SplObjectStorage ‪$validatedInstancesContainer)
200  {
201  $this->validatedInstancesContainer = ‪$validatedInstancesContainer;
202  }
203 }
‪TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface
Definition: ObjectValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\getPropertyValidators
‪array getPropertyValidators($propertyName=null)
Definition: GenericObjectValidator.php:180
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\setValidatedInstancesContainer
‪setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
Definition: GenericObjectValidator.php:197
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:27
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:50
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\addPropertyValidator
‪addPropertyValidator($propertyName, ValidatorInterface $validator)
Definition: GenericObjectValidator.php:142
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\$validatedInstancesContainer
‪SplObjectStorage $validatedInstancesContainer
Definition: GenericObjectValidator.php:190
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:139
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\getPropertyValue
‪mixed getPropertyValue($object, $propertyName)
Definition: GenericObjectValidator.php:65
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\isValid
‪isValid($object)
Definition: GenericObjectValidator.php:117
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\isValidatedAlready
‪bool isValidatedAlready($object)
Definition: GenericObjectValidator.php:154
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\checkProperty
‪checkProperty($value, $validators, $propertyName)
Definition: GenericObjectValidator.php:90
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\isPropertyGettable
‪static bool isPropertyGettable($object, $propertyName)
Definition: ObjectAccess.php:333
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:25
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\$propertyValidators
‪SplObjectStorage[] $propertyValidators
Definition: GenericObjectValidator.php:28
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\canValidate
‪bool canValidate($object)
Definition: GenericObjectValidator.php:131
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:16
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: GenericObjectValidator.php:37
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\markInstanceAsValidated
‪markInstanceAsValidated($object)
Definition: GenericObjectValidator.php:169
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:120
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty($subject, string $propertyName, bool $forceDirectAccess=false)
Definition: ObjectAccess.php:62