b2c73c0cf3c6a891722b03fac982a18832194dd4
[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 (is_object($value) && $this->isValidatedAlready($value)) {
69 return $this->result;
70 } else {
71 $this->isValid($value);
72 }
73 }
74 return $this->result;
75 }
76
77 /**
78 * Checks for a collection and if needed validates the items in the collection.
79 * This is done with the specified element validator or a validator based on
80 * the given element type and validation group.
81 *
82 * Either elementValidator or elementType must be given, otherwise validation
83 * will be skipped.
84 *
85 * @param mixed $value A collection to be validated
86 * @return void
87 * @todo: method must be protected once the old property mapper is removed
88 */
89 public function isValid($value) {
90 if (!$this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
91 // @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
92 if ($this->validatedInstancesContainer == NULL) {
93 $this->validatedInstancesContainer = new \SplObjectStorage();
94 }
95
96 if ($this->result == NULL) {
97 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
98 }
99 }
100
101 foreach ($value as $index => $collectionElement) {
102 if (isset($this->options['elementValidator'])) {
103 $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
104 } elseif (isset($this->options['elementType'])) {
105 if (isset($this->options['validationGroups'])) {
106 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
107 } else {
108 $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
109 }
110 } else {
111 return;
112 }
113 if ($collectionElementValidator instanceof ObjectValidatorInterface) {
114 $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
115 }
116 $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
117 }
118 }
119 }