[BUGFIX] Skip validation of uninitialized collections
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / CollectionValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 /**
32 * A generic collection validator.
33 *
34 * @api
35 */
36 class CollectionValidator extends GenericObjectValidator {
37
38 /**
39 * @var array
40 */
41 protected $supportedOptions = array(
42 'elementValidator' => array(NULL, 'The validator type to use for the collection elements', 'string'),
43 'elementType' => array(NULL, 'The type of the elements in the collection', 'string'),
44 'validationGroups' => array(NULL, 'The validation groups to link to', 'string'),
45 );
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver
49 * @inject
50 */
51 protected $validatorResolver;
52
53 /**
54 * Checks if the given value is valid according to the validator, and returns
55 * the Error Messages object which occurred.
56 *
57 * @param mixed $value The value that should be validated
58 * @return \TYPO3\CMS\Extbase\Error\Result
59 * @api
60 */
61 public function validate($value) {
62 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
63
64 if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
65 if ((is_object($value) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
66 $this->addError('The given subject was not a collection.', 1317204797);
67 return $this->result;
68 } elseif ($value instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
69 return $this->result;
70 } elseif (is_object($value) && $this->isValidatedAlready($value)) {
71 return $this->result;
72 } else {
73 $this->isValid($value);
74 }
75 }
76 return $this->result;
77 }
78
79 /**
80 * Checks for a collection and if needed validates the items in the collection.
81 * This is done with the specified element validator or a validator based on
82 * the given element type and validation group.
83 *
84 * Either elementValidator or elementType must be given, otherwise validation
85 * will be skipped.
86 *
87 * @param mixed $value A collection to be validated
88 * @return void
89 * @todo: method must be protected once the old property mapper is removed
90 */
91 public function isValid($value) {
92 if (!$this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
93 // @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
94 if ($this->validatedInstancesContainer == NULL) {
95 $this->validatedInstancesContainer = new \SplObjectStorage();
96 }
97
98 if ($this->result == NULL) {
99 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
100 }
101 }
102
103 foreach ($value as $index => $collectionElement) {
104 if (isset($this->options['elementValidator'])) {
105 $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
106 } elseif (isset($this->options['elementType'])) {
107 if (isset($this->options['validationGroups'])) {
108 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
109 } else {
110 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
111 }
112 } else {
113 return;
114 }
115 if ($collectionElementValidator instanceof ObjectValidatorInterface) {
116 $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
117 }
118 $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
119 }
120 }
121 }