9c0d47bddfc42ab7303856998876a618f08a806f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / CollectionValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
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
17 /**
18 * A generic collection validator.
19 *
20 * @api
21 */
22 class CollectionValidator extends GenericObjectValidator
23 {
24 /**
25 * @var array
26 */
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
33 /**
34 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
35 */
36 protected $validatorResolver;
37
38 /**
39 * @param \TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver
40 */
41 public function injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
42 {
43 $this->validatorResolver = $validatorResolver;
44 }
45
46 /**
47 * Checks if the given value is valid according to the validator, and returns
48 * the Error Messages object which occurred.
49 *
50 * @param mixed $value The value that should be validated
51 * @return \TYPO3\CMS\Extbase\Error\Result
52 * @api
53 */
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
73 /**
74 * Checks for a collection and if needed validates the items in the collection.
75 * This is done with the specified element validator or a validator based on
76 * the given element type and validation group.
77 *
78 * Either elementValidator or elementType must be given, otherwise validation
79 * will be skipped.
80 *
81 * @param mixed $value A collection to be validated
82 */
83 protected function isValid($value)
84 {
85 foreach ($value as $index => $collectionElement) {
86 if (isset($this->options['elementValidator'])) {
87 $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
88 } elseif (isset($this->options['elementType'])) {
89 if (isset($this->options['validationGroups'])) {
90 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
91 } else {
92 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
93 }
94 } else {
95 return;
96 }
97 if ($collectionElementValidator instanceof ObjectValidatorInterface) {
98 $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
99 }
100 $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
101 }
102 }
103 }