|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Extbase\Validation\Validator; |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
19
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
|
20
|
|
|
use TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage; |
|
21
|
|
|
use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility; |
|
22
|
|
|
use TYPO3\CMS\Extbase\Validation\ValidatorResolver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A generic collection validator. |
|
26
|
|
|
*/ |
|
27
|
|
|
class CollectionValidator extends GenericObjectValidator |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $supportedOptions = [ |
|
33
|
|
|
'elementValidator' => [null, 'The validator type to use for the collection elements', 'string'], |
|
34
|
|
|
'elementType' => [null, 'The type of the elements in the collection', 'string'], |
|
35
|
|
|
'validationGroups' => [null, 'The validation groups to link to', 'string'], |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ValidatorResolver |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $validatorResolver; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(array $options = []) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($options); |
|
46
|
|
|
|
|
47
|
|
|
$this->validatorResolver = GeneralUtility::makeInstance(ValidatorResolver::class); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Checks if the given value is valid according to the validator, and returns |
|
52
|
|
|
* the Error Messages object which occurred. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $value The value that should be validated |
|
55
|
|
|
* @return \TYPO3\CMS\Extbase\Error\Result |
|
56
|
|
|
*/ |
|
57
|
|
|
public function validate($value) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->result = new Result(); |
|
60
|
|
|
|
|
61
|
|
|
if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) { |
|
62
|
|
|
if ((is_object($value) && !TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) { |
|
63
|
|
|
$this->addError('The given subject was not a collection.', 1317204797); |
|
64
|
|
|
return $this->result; |
|
65
|
|
|
} |
|
66
|
|
|
if ($value instanceof LazyObjectStorage && !$value->isInitialized()) { |
|
67
|
|
|
return $this->result; |
|
68
|
|
|
} |
|
69
|
|
|
if (is_object($value)) { |
|
70
|
|
|
if ($this->isValidatedAlready($value)) { |
|
71
|
|
|
return $this->result; |
|
72
|
|
|
} |
|
73
|
|
|
$this->markInstanceAsValidated($value); |
|
74
|
|
|
} |
|
75
|
|
|
$this->isValid($value); |
|
76
|
|
|
} |
|
77
|
|
|
return $this->result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Checks for a collection and if needed validates the items in the collection. |
|
82
|
|
|
* This is done with the specified element validator or a validator based on |
|
83
|
|
|
* the given element type and validation group. |
|
84
|
|
|
* |
|
85
|
|
|
* Either elementValidator or elementType must be given, otherwise validation |
|
86
|
|
|
* will be skipped. |
|
87
|
|
|
* |
|
88
|
|
|
* @param mixed $value A collection to be validated |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function isValid($value) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($value as $index => $collectionElement) { |
|
93
|
|
|
if (isset($this->options['elementValidator'])) { |
|
94
|
|
|
$collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']); |
|
95
|
|
|
} elseif (isset($this->options['elementType'])) { |
|
96
|
|
|
$collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']); |
|
97
|
|
|
} else { |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
if ($collectionElementValidator instanceof ObjectValidatorInterface) { |
|
101
|
|
|
$collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer); |
|
102
|
|
|
} |
|
103
|
|
|
$this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|