TYPO3 CMS  TYPO3_8-7
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 
23 {
27  protected $supportedOptions = [
28  'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'],
29  'elementType' => [null, 'The type of the elements in the collection', 'string'],
30  'validationGroups' => [null, 'The validation groups to link to', 'string'],
31  ];
32 
36  protected $validatorResolver;
37 
41  public function injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
42  {
43  $this->validatorResolver = $validatorResolver;
44  }
45 
54  public function validate($value)
55  {
56  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
57 
58  if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
59  if ((is_object($value) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
60  $this->addError('The given subject was not a collection.', 1317204797);
61  return $this->result;
62  }
63  if ($value instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
64  return $this->result;
65  }
66  if (is_object($value)) {
67  if ($this->isValidatedAlready($value)) {
68  return $this->result;
69  }
70  $this->markInstanceAsValidated($value);
71  }
72  $this->isValid($value);
73  }
74  return $this->result;
75  }
76 
87  protected function isValid($value)
88  {
89  foreach ($value as $index => $collectionElement) {
90  if (isset($this->options['elementValidator'])) {
91  $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
92  } elseif (isset($this->options['elementType'])) {
93  if (isset($this->options['validationGroups'])) {
94  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
95  } else {
96  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
97  }
98  } else {
99  return;
100  }
101  if ($collectionElementValidator instanceof ObjectValidatorInterface) {
102  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
103  }
104  $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
105  }
106  }
107 }
addError($message, $code, array $arguments=[], $title='')
injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)