1
|
|
|
<?php namespace Limoncello\Validation\Expressions; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\AutoNameRuleInterface; |
20
|
|
|
use Limoncello\Validation\Contracts\ErrorAggregatorInterface; |
21
|
|
|
use Limoncello\Validation\Contracts\RuleInterface; |
22
|
|
|
use Limoncello\Validation\Rules; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Limoncello\Validation |
26
|
|
|
*/ |
27
|
|
|
abstract class IterateRules extends BaseExpression implements AutoNameRuleInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RuleInterface[] |
31
|
|
|
*/ |
32
|
|
|
private $rules; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var null|bool |
36
|
|
|
*/ |
37
|
|
|
private $isStateless = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $isAutoParamNames = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $rules |
46
|
|
|
*/ |
47
|
13 |
|
public function __construct(array $rules) |
48
|
|
|
{ |
49
|
13 |
|
$this->rules = $rules; |
50
|
13 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
|
public function enableAutoParameterNames() |
56
|
|
|
{ |
57
|
1 |
|
$this->isAutoParamNames = true; |
58
|
|
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
1 |
|
public function disableAutoParameterNames() |
66
|
|
|
{ |
67
|
1 |
|
$this->isAutoParamNames = false; |
68
|
|
|
|
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
2 |
|
public function isStateless() |
76
|
|
|
{ |
77
|
2 |
|
if ($this->isStateless !== null) { |
78
|
1 |
|
return $this->isStateless; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
foreach ($this->getRules() as $rule) { |
82
|
2 |
|
if ($rule->isStateless() === false) { |
83
|
2 |
|
return $this->isStateless = false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->isStateless = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
13 |
|
public function onFinish(ErrorAggregatorInterface $aggregator) |
94
|
|
|
{ |
95
|
13 |
|
foreach ($this->getRules() as $key => $rule) { |
96
|
13 |
|
$this->setParameterName($key); |
97
|
13 |
|
$rule->setParentRule($this); |
98
|
13 |
|
$rule->onFinish($aggregator); |
99
|
|
|
} |
100
|
13 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
13 |
|
public function setParameterName($parameterName) |
106
|
|
|
{ |
107
|
13 |
|
if ($this->isAutoNames() === true) { |
108
|
13 |
|
parent::setParameterName($parameterName); |
109
|
|
|
} |
110
|
|
|
|
111
|
13 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return RuleInterface[] |
116
|
|
|
*/ |
117
|
13 |
|
protected function getRules() |
118
|
|
|
{ |
119
|
13 |
|
return $this->rules; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
13 |
|
protected function isAutoNames() |
126
|
|
|
{ |
127
|
13 |
|
return $this->isAutoParamNames; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|