‪TYPO3CMS  10.4
CollectionValidator.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 
22 
27 {
31  protected ‪$supportedOptions = [
32  'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'],
33  'elementType' => [null, 'The type of the elements in the collection', 'string'],
34  'validationGroups' => [null, 'The validation groups to link to', 'string'],
35  ];
36 
40  protected ‪$validatorResolver;
41 
46  {
47  $this->validatorResolver = ‪$validatorResolver;
48  }
49 
57  public function ‪validate($value)
58  {
59  $this->result = new ‪Result();
60 
61  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
62  if ((is_object($value) && !‪TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
63  $this->‪addError('The given subject was not a collection.', 1317204797);
64  return ‪$this->result;
65  }
66  if ($value instanceof ‪LazyObjectStorage && !$value->‪isInitialized()) {
67  return ‪$this->result;
68  }
69  if (is_object($value)) {
70  if ($this->‪isValidatedAlready($value)) {
71  return ‪$this->result;
72  }
73  $this->‪markInstanceAsValidated($value);
74  }
75  $this->‪isValid($value);
76  }
77  return ‪$this->result;
78  }
79 
90  protected function ‪isValid($value)
91  {
92  foreach ($value as $index => $collectionElement) {
93  if (isset($this->options['elementValidator'])) {
94  $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
95  } elseif (isset($this->options['elementType'])) {
96  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
97  } else {
98  return;
99  }
100  if ($collectionElementValidator instanceof ‪ObjectValidatorInterface) {
101  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
102  }
103  $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
104  }
105  }
106 }
‪TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface
Definition: ObjectValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator
Definition: CollectionValidator.php:27
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:50
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:35
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:139
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\isValid
‪isValid($value)
Definition: CollectionValidator.php:88
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\$supportedOptions
‪array $supportedOptions
Definition: CollectionValidator.php:30
‪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\Utility\TypeHandlingUtility\isCollectionType
‪static bool isCollectionType(string $type)
Definition: TypeHandlingUtility.php:135
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility
Definition: TypeHandlingUtility.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:25
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\injectValidatorResolver
‪injectValidatorResolver(ValidatorResolver $validatorResolver)
Definition: CollectionValidator.php:43
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: CollectionValidator.php:55
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage\isInitialized
‪bool isInitialized()
Definition: LazyObjectStorage.php:83
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:16
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage
Definition: LazyObjectStorage.php:30
‪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\Validation\Validator\CollectionValidator\$validatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $validatorResolver
Definition: CollectionValidator.php:38