TYPO3 CMS  TYPO3_7-6
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  } elseif ($value instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
63  return $this->result;
64  } elseif (is_object($value) && $this->isValidatedAlready($value)) {
65  return $this->result;
66  } else {
67  $this->isValid($value);
68  }
69  }
70  return $this->result;
71  }
72 
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  if (isset($this->options['validationGroups'])) {
91  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
92  } else {
93  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
94  }
95  } else {
96  return;
97  }
98  if ($collectionElementValidator instanceof ObjectValidatorInterface) {
99  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
100  }
101  $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
102  }
103  }
104 }
addError($message, $code, array $arguments=[], $title='')
injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)