‪TYPO3CMS  9.5
CollectionValidator.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 
21 {
25  protected ‪$supportedOptions = [
26  'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'],
27  'elementType' => [null, 'The type of the elements in the collection', 'string'],
28  'validationGroups' => [null, 'The validation groups to link to', 'string'],
29  ];
30 
34  protected ‪$validatorResolver;
35 
40  {
41  $this->validatorResolver = ‪$validatorResolver;
42  }
43 
51  public function ‪validate($value)
52  {
53  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
54 
55  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
56  if ((is_object($value) && !\‪TYPO3\CMS\‪Extbase\Utility\‪TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
57  $this->‪addError('The given subject was not a collection.', 1317204797);
58  return ‪$this->result;
59  }
60  if ($value instanceof \‪TYPO3\CMS\‪Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
61  return ‪$this->result;
62  }
63  if (is_object($value)) {
64  if ($this->‪isValidatedAlready($value)) {
65  return ‪$this->result;
66  }
67  $this->‪markInstanceAsValidated($value);
68  }
69  $this->‪isValid($value);
70  }
71  return ‪$this->result;
72  }
73 
84  protected function ‪isValid($value)
85  {
86  foreach ($value as $index => $collectionElement) {
87  if (isset($this->options['elementValidator'])) {
88  $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
89  } elseif (isset($this->options['elementType'])) {
90  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
91  } else {
92  return;
93  }
94  if ($collectionElementValidator instanceof ‪ObjectValidatorInterface) {
95  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
96  }
97  $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
98  }
99  }
100 }
‪TYPO3\CMS\Extbase\Validation\Validator\ObjectValidatorInterface
Definition: ObjectValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\injectValidatorResolver
‪injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
Definition: CollectionValidator.php:37
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator
Definition: CollectionValidator.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:46
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Validation\ValidatorResolver
Definition: ValidatorResolver.php:29
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:135
‪TYPO3
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\isValid
‪isValid($value)
Definition: CollectionValidator.php:82
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\$supportedOptions
‪array $supportedOptions
Definition: CollectionValidator.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator\isValidatedAlready
‪bool isValidatedAlready($object)
Definition: GenericObjectValidator.php:144
‪TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator
Definition: GenericObjectValidator.php:23
‪TYPO3\CMS\Extbase\Utility\TypeHandlingUtility\isCollectionType
‪static bool isCollectionType($type)
Definition: TypeHandlingUtility.php:125
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: CollectionValidator.php:49
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:2
‪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
‪TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator\$validatorResolver
‪TYPO3 CMS Extbase Validation ValidatorResolver $validatorResolver
Definition: CollectionValidator.php:32