[TASK] Streamline phpdoc annotations in EXT:extbase
[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 class CollectionValidator extends GenericObjectValidator
21 {
22 /**
23 * @var array
24 */
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
31 /**
32 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
33 */
34 protected $validatorResolver;
35
36 /**
37 * @param \TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver
38 */
39 public function injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
40 {
41 $this->validatorResolver = $validatorResolver;
42 }
43
44 /**
45 * Checks if the given value is valid according to the validator, and returns
46 * the Error Messages object which occurred.
47 *
48 * @param mixed $value The value that should be validated
49 * @return \TYPO3\CMS\Extbase\Error\Result
50 */
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) && $this->isValidatedAlready($value)) {
64 return $this->result;
65 }
66 $this->isValid($value);
67 }
68 return $this->result;
69 }
70
71 /**
72 * Checks for a collection and if needed validates the items in the collection.
73 * This is done with the specified element validator or a validator based on
74 * the given element type and validation group.
75 *
76 * Either elementValidator or elementType must be given, otherwise validation
77 * will be skipped.
78 *
79 * @param mixed $value A collection to be validated
80 */
81 protected function isValid($value)
82 {
83 foreach ($value as $index => $collectionElement) {
84 if (isset($this->options['elementValidator'])) {
85 $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
86 } elseif (isset($this->options['elementType'])) {
87 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
88 } else {
89 return;
90 }
91 if ($collectionElementValidator instanceof ObjectValidatorInterface) {
92 $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
93 }
94 $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
95 }
96 }
97 }