‪TYPO3CMS  9.5
GenericObjectValidator.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 
36  public function ‪validate($value)
37  {
38  if (is_object($value) && $this->‪isValidatedAlready($value)) {
39  return ‪$this->result;
40  }
41 
42  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
43  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
44  if (!is_object($value)) {
45  $this->‪addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
46  } elseif ($this->‪isValidatedAlready($value) === false) {
47  $this->‪markInstanceAsValidated($value);
48  $this->‪isValid($value);
49  }
50  }
51 
52  return ‪$this->result;
53  }
54 
64  protected function ‪getPropertyValue($object, $propertyName)
65  {
66  // @todo add support for lazy loading proxies, if needed
67  if (‪ObjectAccess::isPropertyGettable($object, $propertyName)) {
68  return ‪ObjectAccess::getProperty($object, $propertyName);
69  }
70  return ‪ObjectAccess::getProperty($object, $propertyName, true);
71  }
72 
81  protected function ‪checkProperty($value, $validators, $propertyName)
82  {
84  ‪$result = null;
85  foreach ($validators as ‪$validator) {
87  ‪$validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
88  }
89  $currentResult = ‪$validator->validate($value);
90  if ($currentResult->hasMessages()) {
91  if (‪$result == null) {
92  ‪$result = $currentResult;
93  } else {
94  ‪$result->merge($currentResult);
95  }
96  }
97  }
98  if (‪$result != null) {
99  $this->result->forProperty($propertyName)->merge(‪$result);
100  }
101  }
102 
108  protected function ‪isValid($object)
109  {
110  foreach ($this->propertyValidators as $propertyName => $validators) {
111  $propertyValue = $this->‪getPropertyValue($object, $propertyName);
112  $this->‪checkProperty($propertyValue, $validators, $propertyName);
113  }
114  }
115 
122  public function ‪canValidate($object)
123  {
124  return is_object($object);
125  }
126 
133  public function ‪addPropertyValidator($propertyName, ‪ValidatorInterface ‪$validator)
134  {
135  if (!isset($this->propertyValidators[$propertyName])) {
136  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
137  }
138  $this->propertyValidators[$propertyName]->attach(‪$validator);
139  }
140 
145  protected function ‪isValidatedAlready($object)
146  {
147  if ($this->validatedInstancesContainer === null) {
148  $this->validatedInstancesContainer = new \SplObjectStorage();
149  }
150  if ($this->validatedInstancesContainer->contains($object)) {
151  return true;
152  }
153 
154  return false;
155  }
156 
160  protected function ‪markInstanceAsValidated($object): void
161  {
162  $this->validatedInstancesContainer->attach($object);
163  }
164 
171  public function ‪getPropertyValidators($propertyName = null)
172  {
173  if ($propertyName !== null) {
174  return $this->propertyValidators[$propertyName] ?? [];
175  }
177  }
178 
183 
189  public function ‪setValidatedInstancesContainer(\SplObjectStorage ‪$validatedInstancesContainer)
190  {
191  $this->validatedInstancesContainer = ‪$validatedInstancesContainer;
192  }
193 }
‪TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface
Definition: ObjectValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\getPropertyValidators
‪array getPropertyValidators($propertyName=null)
Definition: GenericObjectValidator.php:170
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\setValidatedInstancesContainer
‪setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
Definition: GenericObjectValidator.php:187
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:23
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:46
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\addPropertyValidator
‪addPropertyValidator($propertyName, ValidatorInterface $validator)
Definition: GenericObjectValidator.php:132
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\$validatedInstancesContainer
‪SplObjectStorage $validatedInstancesContainer
Definition: GenericObjectValidator.php:180
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:135
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\getPropertyValue
‪mixed getPropertyValue($object, $propertyName)
Definition: GenericObjectValidator.php:63
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\isValid
‪isValid($object)
Definition: GenericObjectValidator.php:107
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\isValidatedAlready
‪bool isValidatedAlready($object)
Definition: GenericObjectValidator.php:144
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\checkProperty
‪checkProperty($value, $validators, $propertyName)
Definition: GenericObjectValidator.php:80
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\isPropertyGettable
‪static bool isPropertyGettable($object, $propertyName)
Definition: ObjectAccess.php:335
‪$validator
‪if(isset($args['d'])) $validator
Definition: validateRstFiles.php:218
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:23
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty
‪static mixed getProperty($subject, $propertyName, $forceDirectAccess=false)
Definition: ObjectAccess.php:54
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\$propertyValidators
‪SplObjectStorage[] $propertyValidators
Definition: GenericObjectValidator.php:26
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\canValidate
‪bool canValidate($object)
Definition: GenericObjectValidator.php:121
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:2
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: GenericObjectValidator.php:35
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\markInstanceAsValidated
‪markInstanceAsValidated($object)
Definition: GenericObjectValidator.php:159
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:116