‪TYPO3CMS  ‪main
CollectionValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
29 {
33  protected ‪$supportedOptions = [
34  'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'],
35  'elementType' => [null, 'The type of the elements in the collection', 'string'],
36  ];
37 
38  public function ‪__construct(protected readonly ‪ValidatorResolver $validatorResolver) {}
39 
44  public function ‪validate(mixed $value): ‪Result
45  {
46  $this->result = new ‪Result();
47 
48  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
49  if ((is_object($value) && !‪TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
50  $this->‪addError('The given subject was not a collection.', 1317204797);
51  return ‪$this->result;
52  }
53  if ($value instanceof LazyObjectStorage && !$value->isInitialized()) {
54  return ‪$this->result;
55  }
56  if (is_object($value)) {
57  if ($this->‪isValidatedAlready($value)) {
58  return ‪$this->result;
59  }
60  $this->‪markInstanceAsValidated($value);
61  }
62  $this->‪isValid($value);
63  }
64  return ‪$this->result;
65  }
66 
75  protected function ‪isValid(mixed $value): void
76  {
77  foreach ($value as $index => $collectionElement) {
78  if (isset($this->options['elementValidator'])) {
79  $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
80  } elseif (isset($this->options['elementType'])) {
81  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
82  } else {
83  return;
84  }
85  if ($collectionElementValidator instanceof ObjectValidatorInterface) {
86  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
87  }
88  $this->result->forProperty((string)$index)->merge($collectionElementValidator->validate($collectionElement));
89  }
90  }
91 }
‪TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface
Definition: ObjectValidatorInterface.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractGenericObjectValidator\markInstanceAsValidated
‪markInstanceAsValidated(object $object)
Definition: AbstractGenericObjectValidator.php:147
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator
Definition: CollectionValidator.php:29
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪isInitialized()
Definition: LazyObjectStorage.php:66
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\isValid
‪isValid(mixed $value)
Definition: CollectionValidator.php:74
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:40
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractGenericObjectValidator\isValidatedAlready
‪isValidatedAlready(object $object)
Definition: AbstractGenericObjectValidator.php:135
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError(string $message, int $code, array $arguments=[], string $title='')
Definition: AbstractValidator.php:98
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\$supportedOptions
‪array $supportedOptions
Definition: CollectionValidator.php:32
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\validate
‪validate(mixed $value)
Definition: CollectionValidator.php:43
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪Result $result
Definition: AbstractValidator.php:53
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractGenericObjectValidator
Definition: AbstractGenericObjectValidator.php:27
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\__construct
‪__construct(protected readonly ValidatorResolver $validatorResolver)
Definition: CollectionValidator.php:37
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:18
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCollectionType
‪static isCollectionType(string $type)
Definition: TypeHandlingUtility.php:125
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:36
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪isEmpty(mixed $value)
Definition: AbstractValidator.php:138