|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Execution; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [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 function assert; |
|
23
|
|
|
use function count; |
|
24
|
|
|
use function is_array; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @package Limoncello\Validation |
|
28
|
|
|
*/ |
|
29
|
|
|
final class BlockReplies |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Rule reply key. |
|
33
|
|
|
*/ |
|
34
|
|
|
private const REPLY_SUCCESS_VALUE = 0; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Rule reply key. |
|
38
|
|
|
*/ |
|
39
|
|
|
private const REPLY_ERRORS_INFO = self::REPLY_SUCCESS_VALUE + 1; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Error info key. |
|
43
|
|
|
*/ |
|
44
|
|
|
public const ERROR_INFO_BLOCK_INDEX = 0; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Error info key. |
|
48
|
|
|
*/ |
|
49
|
|
|
public const ERROR_INFO_VALUE = self::ERROR_INFO_BLOCK_INDEX + 1; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Error info key. |
|
53
|
|
|
*/ |
|
54
|
|
|
public const ERROR_INFO_CODE = self::ERROR_INFO_VALUE + 1; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Error info key. |
|
58
|
|
|
*/ |
|
59
|
|
|
public const ERROR_INFO_MESSAGE_TEMPLATE = self::ERROR_INFO_CODE + 1; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Error info key. |
|
63
|
|
|
*/ |
|
64
|
|
|
public const ERROR_INFO_MESSAGE_PARAMETERS = self::ERROR_INFO_MESSAGE_TEMPLATE + 1; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param mixed $result |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
19 |
|
public static function createSuccessReply($result): array |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
19 |
|
static::REPLY_SUCCESS_VALUE => $result, |
|
75
|
19 |
|
static::REPLY_ERRORS_INFO => null, |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public static function createStartSuccessReply(): array |
|
83
|
|
|
{ |
|
84
|
3 |
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public static function createEndSuccessReply(): array |
|
91
|
|
|
{ |
|
92
|
4 |
|
return []; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param ContextInterface $context |
|
97
|
|
|
* @param mixed $errorValue |
|
98
|
|
|
* @param int $errorCode |
|
99
|
|
|
* @param string $messageTemplate |
|
100
|
|
|
* @param array $messageParams |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
14 |
|
public static function createErrorReply( |
|
105
|
|
|
ContextInterface $context, |
|
106
|
|
|
$errorValue, |
|
107
|
|
|
int $errorCode, |
|
108
|
|
|
string $messageTemplate, |
|
109
|
|
|
array $messageParams |
|
110
|
|
|
): array { |
|
111
|
|
|
return [ |
|
112
|
14 |
|
static::REPLY_SUCCESS_VALUE => null, |
|
113
|
14 |
|
static::REPLY_ERRORS_INFO => [ |
|
114
|
14 |
|
static::createErrorInfoEntry( |
|
115
|
14 |
|
$context->getCurrentBlockId(), |
|
116
|
14 |
|
$errorValue, |
|
117
|
14 |
|
$errorCode, |
|
118
|
14 |
|
$messageTemplate, |
|
119
|
14 |
|
$messageParams |
|
120
|
|
|
), |
|
121
|
|
|
], |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param ContextInterface $context |
|
127
|
|
|
* @param int $errorCode |
|
128
|
|
|
* @param string $messageTemplate |
|
129
|
|
|
* @param array $messageParams |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public static function createStartErrorReply( |
|
134
|
|
|
ContextInterface $context, |
|
135
|
|
|
int $errorCode, |
|
136
|
|
|
string $messageTemplate, |
|
137
|
|
|
array $messageParams |
|
138
|
|
|
): array { |
|
139
|
2 |
|
$value = null; |
|
140
|
|
|
|
|
141
|
|
|
return [ |
|
142
|
2 |
|
static::createErrorInfoEntry( |
|
143
|
2 |
|
$context->getCurrentBlockId(), |
|
144
|
2 |
|
$value, |
|
145
|
2 |
|
$errorCode, |
|
146
|
2 |
|
$messageTemplate, |
|
147
|
2 |
|
$messageParams |
|
148
|
|
|
), |
|
149
|
|
|
]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param ContextInterface $context |
|
154
|
|
|
* @param int $errorCode |
|
155
|
|
|
* @param string $messageTemplate |
|
156
|
|
|
* @param array $messageParams |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
3 |
|
public static function createEndErrorReply( |
|
161
|
|
|
ContextInterface $context, |
|
162
|
|
|
int $errorCode, |
|
163
|
|
|
string $messageTemplate, |
|
164
|
|
|
array $messageParams |
|
165
|
|
|
): array { |
|
166
|
3 |
|
$value = null; |
|
167
|
|
|
|
|
168
|
|
|
return [ |
|
169
|
3 |
|
static::createErrorInfoEntry( |
|
170
|
3 |
|
$context->getCurrentBlockId(), |
|
171
|
3 |
|
$value, |
|
172
|
3 |
|
$errorCode, |
|
173
|
3 |
|
$messageTemplate, |
|
174
|
3 |
|
$messageParams |
|
175
|
|
|
), |
|
176
|
|
|
]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param int $blockId |
|
181
|
|
|
* @param mixed $value |
|
182
|
|
|
* @param int $code |
|
183
|
|
|
* @param string $messageTemplate |
|
184
|
|
|
* @param array $messageParams |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
17 |
|
protected static function createErrorInfoEntry( |
|
189
|
|
|
int $blockId, |
|
190
|
|
|
$value, |
|
191
|
|
|
int $code, |
|
192
|
|
|
string $messageTemplate, |
|
193
|
|
|
array $messageParams |
|
194
|
|
|
): array { |
|
195
|
|
|
return [ |
|
196
|
17 |
|
static::ERROR_INFO_BLOCK_INDEX => $blockId, |
|
197
|
17 |
|
static::ERROR_INFO_VALUE => $value, |
|
198
|
17 |
|
static::ERROR_INFO_CODE => $code, |
|
199
|
17 |
|
static::ERROR_INFO_MESSAGE_TEMPLATE => $messageTemplate, |
|
200
|
17 |
|
static::ERROR_INFO_MESSAGE_PARAMETERS => $messageParams, |
|
201
|
|
|
]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param array $result |
|
206
|
|
|
* |
|
207
|
|
|
* @return bool |
|
208
|
|
|
*/ |
|
209
|
21 |
|
public static function isResultSuccessful(array $result): bool |
|
210
|
|
|
{ |
|
211
|
21 |
|
assert( |
|
212
|
21 |
|
count($result) === 2 && |
|
213
|
21 |
|
($result[static::REPLY_ERRORS_INFO] === null || is_array($result[static::REPLY_ERRORS_INFO]) === true) |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
// if error code is `null` |
|
217
|
21 |
|
$isOk = $result[static::REPLY_ERRORS_INFO] === null; |
|
218
|
|
|
|
|
219
|
21 |
|
return $isOk; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param array $result |
|
224
|
|
|
* |
|
225
|
|
|
* @return mixed |
|
226
|
|
|
*/ |
|
227
|
19 |
|
public static function extractResultOutput(array $result) |
|
228
|
|
|
{ |
|
229
|
|
|
// extracting result only make sense when error is `null`. |
|
230
|
19 |
|
assert(static::isResultSuccessful($result) === true && $result[static::REPLY_ERRORS_INFO] === null); |
|
231
|
|
|
|
|
232
|
19 |
|
$value = $result[static::REPLY_SUCCESS_VALUE]; |
|
233
|
|
|
|
|
234
|
19 |
|
return $value; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param array $result |
|
239
|
|
|
* |
|
240
|
|
|
* @return array |
|
241
|
|
|
*/ |
|
242
|
14 |
|
public static function extractResultErrorsInfo(array $result): array |
|
243
|
|
|
{ |
|
244
|
14 |
|
assert( |
|
245
|
14 |
|
count($result) === 2 && |
|
246
|
14 |
|
$result[static::REPLY_SUCCESS_VALUE] === null && |
|
247
|
14 |
|
is_array($result[static::REPLY_ERRORS_INFO]) === true && |
|
248
|
14 |
|
empty($result[static::REPLY_ERRORS_INFO]) === false |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
14 |
|
$value = $result[static::REPLY_ERRORS_INFO]; |
|
252
|
|
|
|
|
253
|
14 |
|
return $value; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|