-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
AbstractCompositeValidator.php
156 lines (141 loc) · 4.4 KB
/
AbstractCompositeValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Neos\Flow\Validation\Validator;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Validation\Exception\InvalidValidationOptionsException;
use Neos\Flow\Validation\Exception\NoSuchValidatorException;
/**
* An abstract composite validator consisting of other validators
*
* @api
*/
abstract class AbstractCompositeValidator implements ObjectValidatorInterface, \Countable
{
/**
* This contains the supported options, their default values and descriptions.
*
* @var array
*/
protected $supportedOptions = [];
/**
* @var array
*/
protected $options = [];
/**
* @var \SplObjectStorage
*/
protected $validators;
/**
* @var \SplObjectStorage
*/
protected $validatedInstancesContainer;
/**
* Constructs the composite validator and sets validation options
*
* @param array $options Options for the validator
* @api
* @throws InvalidValidationOptionsException
*/
public function __construct(array $options = [])
{
// check for options given but not supported
if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== []) {
throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1339079804);
}
// check for required options being set
array_walk(
$this->supportedOptions,
function ($supportedOptionData, $supportedOptionName, $options) {
if (isset($supportedOptionData[3]) && !array_key_exists($supportedOptionName, $options)) {
throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1339163922);
}
},
$options
);
// merge with default values
$this->options = array_merge(
array_map(
function ($value) {
return $value[0];
},
$this->supportedOptions
),
$options
);
$this->validators = new \SplObjectStorage();
}
/**
* Allows to set a container to keep track of validated instances.
*
* @param \SplObjectStorage $validatedInstancesContainer A container to keep track of validated instances
* @return void
* @api
*/
public function setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
{
$this->validatedInstancesContainer = $validatedInstancesContainer;
}
/**
* Adds a new validator to the conjunction.
*
* @param ValidatorInterface $validator The validator that should be added
* @return void
* @api
*/
public function addValidator(ValidatorInterface $validator)
{
if ($validator instanceof ObjectValidatorInterface && isset($this->validatedInstancesContainer)) {
$validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
}
$this->validators->attach($validator);
}
/**
* Removes the specified validator.
*
* @param ValidatorInterface $validator The validator to remove
* @throws NoSuchValidatorException
* @api
*/
public function removeValidator(ValidatorInterface $validator)
{
if (!$this->validators->contains($validator)) {
throw new NoSuchValidatorException('Cannot remove validator because its not in the conjunction.', 1207020177);
}
$this->validators->detach($validator);
}
/**
* Returns the number of validators contained in this conjunction.
*
* @return integer The number of validators
* @api
*/
public function count(): int
{
return count($this->validators);
}
/**
* Returns the child validators of this Composite Validator
*
* @return \SplObjectStorage
*/
public function getValidators()
{
return $this->validators;
}
/**
* Returns the options for this validator
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
}