1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Rules; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextInterface; |
22
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
23
|
|
|
use Limoncello\Validation\Execution\BlockReplies; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Validation |
27
|
|
|
* |
28
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
29
|
|
|
*/ |
30
|
|
|
abstract class BaseRule implements RuleInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* State key. |
34
|
|
|
*/ |
35
|
|
|
const STATE_ERROR_VALUE = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* State key. |
39
|
|
|
*/ |
40
|
|
|
const STATE_LAST = self::STATE_ERROR_VALUE; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Property key. |
44
|
|
|
*/ |
45
|
|
|
const PROPERTY_NAME = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Property key. |
49
|
|
|
*/ |
50
|
|
|
const PROPERTY_IS_CAPTURE_ENABLED = self::PROPERTY_NAME + 1; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Property key. |
54
|
|
|
*/ |
55
|
|
|
const PROPERTY_LAST = self::PROPERTY_IS_CAPTURE_ENABLED; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
private $name = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
private $isCaptureEnabled = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var RuleInterface|null |
69
|
|
|
*/ |
70
|
|
|
private $parent = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
33 |
|
public function getName(): string |
76
|
|
|
{ |
77
|
33 |
|
if ($this->name === null) { |
78
|
28 |
|
return $this->parent === null ? '' : $this->getParent()->getName(); |
79
|
|
|
} |
80
|
|
|
|
81
|
28 |
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
28 |
|
public function setName(string $name): RuleInterface |
88
|
|
|
{ |
89
|
28 |
|
$this->name = $name; |
90
|
|
|
|
91
|
28 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
1 |
|
public function unsetName(): RuleInterface |
98
|
|
|
{ |
99
|
1 |
|
$this->name = null; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
29 |
|
public function enableCapture(): RuleInterface |
108
|
|
|
{ |
109
|
29 |
|
$this->isCaptureEnabled = true; |
110
|
|
|
|
111
|
29 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
1 |
|
public function disableCapture(): RuleInterface |
118
|
|
|
{ |
119
|
1 |
|
$this->isCaptureEnabled = false; |
120
|
|
|
|
121
|
1 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
33 |
|
public function isCaptureEnabled(): bool |
128
|
|
|
{ |
129
|
33 |
|
return $this->isCaptureEnabled; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
18 |
|
public function getParent(): ?RuleInterface |
136
|
|
|
{ |
137
|
18 |
|
return $this->parent; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritdoc |
142
|
|
|
*/ |
143
|
18 |
|
public function setParent(RuleInterface $rule): RuleInterface |
144
|
|
|
{ |
145
|
18 |
|
$this->parent = $rule; |
146
|
|
|
|
147
|
18 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @inheritdoc |
152
|
|
|
*/ |
153
|
1 |
|
public function unsetParent(): RuleInterface |
154
|
|
|
{ |
155
|
1 |
|
$this->parent = null; |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
32 |
|
protected function getStandardProperties(): array |
164
|
|
|
{ |
165
|
32 |
|
return static::composeStandardProperties($this->getName(), $this->isCaptureEnabled()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $name |
170
|
|
|
* @param bool $isCaptureEnabled |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
32 |
|
protected function composeStandardProperties(string $name, bool $isCaptureEnabled): array |
175
|
|
|
{ |
176
|
|
|
return [ |
177
|
32 |
|
static::PROPERTY_NAME => $name, |
178
|
32 |
|
static::PROPERTY_IS_CAPTURE_ENABLED => $isCaptureEnabled, |
179
|
|
|
]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param iterable $messageParams |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
17 |
|
protected function checkEachValueConvertibleToString(iterable $messageParams): bool |
188
|
|
|
{ |
189
|
17 |
|
$result = true; |
190
|
|
|
|
191
|
17 |
|
foreach ($messageParams as $param) { |
192
|
8 |
|
$result = $result && ( |
193
|
8 |
|
is_scalar($param) === true || |
194
|
8 |
|
$param === null || |
195
|
8 |
|
(is_object($param) === true && method_exists($param, '__toString')) |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
17 |
|
return true; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param mixed $result |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
* |
207
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
208
|
|
|
*/ |
209
|
16 |
|
protected static function createSuccessReply($result): array |
210
|
|
|
{ |
211
|
16 |
|
return BlockReplies::createSuccessReply($result); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param ContextInterface $context |
216
|
|
|
* @param mixed $errorValue |
217
|
|
|
* @param int $errorCode |
218
|
|
|
* @param string $messageTemplate |
219
|
|
|
* @param array $messageParams |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
* |
223
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
224
|
|
|
*/ |
225
|
12 |
|
protected static function createErrorReply( |
226
|
|
|
ContextInterface $context, |
227
|
|
|
$errorValue, |
228
|
|
|
int $errorCode, |
229
|
|
|
string $messageTemplate, |
230
|
|
|
array $messageParams |
231
|
|
|
): array { |
232
|
12 |
|
return BlockReplies::createErrorReply($context, $errorValue, $errorCode, $messageTemplate, $messageParams); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|