-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseDTO.php
More file actions
275 lines (243 loc) · 9.22 KB
/
BaseDTO.php
File metadata and controls
275 lines (243 loc) · 9.22 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace WebmanTech\DTO;
use BackedEnum;
use DateTime;
use DateTimeInterface;
use stdClass;
use WebmanTech\DTO\Attributes\FromDataConfig;
use WebmanTech\DTO\Attributes\ToArrayConfig;
use WebmanTech\DTO\Exceptions\DTONewInstanceException;
use WebmanTech\DTO\Exceptions\DTOValidateException;
use WebmanTech\DTO\Helper\ArrayHelper;
use WebmanTech\DTO\Helper\ConfigHelper;
use WebmanTech\DTO\Integrations\Validation;
use WebmanTech\DTO\Integrations\ValidatorStopOnFirstFailureInterface;
use WebmanTech\DTO\Reflection\ReflectionClassReader;
use WebmanTech\DTO\Reflection\ReflectionReaderFactory;
class BaseDTO
{
/**
* 根据 data 构建实例
* @throws DTONewInstanceException|DTOValidateException
*/
public static function fromData(array $data, bool $validate = true): static
{
$factory = ReflectionReaderFactory::fromClass(static::class);
$fromDataConfig = self::getFromDataConfig(__FUNCTION__, $factory);
if ($fromDataConfig) {
if ($fromDataConfig->trim) {
$data = array_map(function ($value) {
return is_string($value) ? trim($value) : $value;
}, $data);
}
if ($fromDataConfig->ignoreNull || $fromDataConfig->ignoreEmpty) {
$data = array_filter($data, function ($value) use ($fromDataConfig) {
if ($fromDataConfig->ignoreNull && $value === null) {
return false;
}
if ($fromDataConfig->ignoreEmpty && $value === '') {
return false;
}
return true;
});
}
}
if ($validate) {
$rules = static::getValidationRules();
try {
$data = static::validateData($data, $rules);
} finally {
self::resetFromDataConfig(__FUNCTION__);
}
}
try {
return $factory->newInstanceByData($data);
} catch (\Throwable $e) {
throw new DTONewInstanceException(static::class, $e);
} finally {
self::resetFromDataConfig(__FUNCTION__);
}
}
/**
* 获取全部的验证规则(包括嵌套 DTO 的所有规则)
* @return array<string, array>
*/
public static function getValidationRules(): array
{
// 必须的规则
$rules = ReflectionReaderFactory::fromClass(static::class)->getPropertiesValidationRules();
if ($extraRules = static::getExtraValidationRules()) {
// 合并自定义规则
foreach ($extraRules as $key => $keyRules) {
if (is_string($keyRules)) {
$keyRules = explode('|', $keyRules);
}
$keyRules = ArrayHelper::wrap($keyRules);
if (!isset($rules[$key])) {
$rules[$key] = $keyRules;
} else {
$allRules = array_merge($rules[$key], $keyRules);
$uniqueAllRules = [];
foreach ($allRules as $rule) {
if (is_string($rule) && in_array($rule, $uniqueAllRules, true)) {
continue;
}
$uniqueAllRules[] = $rule;
}
$rules[$key] = $uniqueAllRules;
}
}
}
$fromDataConfig = self::getFromDataConfig(__FUNCTION__);
// 应用 bail 规则
if ($fromDataConfig && $fromDataConfig->validatePropertiesAllWithBail) {
foreach ($rules as $key => $keyRules) {
// 如果第一位已经是 bail,跳过(ValidationRules 中已处理过)
if (isset($keyRules[0]) && $keyRules[0] === 'bail') {
continue;
}
// 如果没有 bail,添加到开头
array_unshift($rules[$key], 'bail');
}
}
self::resetFromDataConfig(__FUNCTION__);
return $rules;
}
/**
* 获取额外的验证规则
* @return array<string, array|string>
*/
protected static function getExtraValidationRules(): array
{
return [];
}
/**
* 验证规则的错误信息
* @return array<string, string>
*/
protected static function getValidationRuleMessages(): array
{
return [];
}
/**
* 验证规则的自定义属性
* @return array<string, string>
*/
protected static function getValidationRuleCustomAttributes(): array
{
return [];
}
/**
* 验证数据
* @param array<string, mixed> $data
* @return array<string, mixed> 验证过的数据
* @throws DTOValidateException
*/
protected static function validateData(array $data, array $rules): array
{
if (!$rules) {
return $data;
}
$validator = Validation::create();
$fromDataConfig = self::getFromDataConfig(__FUNCTION__);
if ($fromDataConfig && $fromDataConfig->validateStopOnFirstFailure && $validator instanceof ValidatorStopOnFirstFailureInterface) {
$validator = $validator->stopOnFirstFailure();
}
return $validator->validate($data, $rules, static::getValidationRuleMessages(), static::getValidationRuleCustomAttributes());
}
/**
* 日期格式
*/
protected function getDateTimeFormat(): string
{
return ConfigHelper::get('dto.to_array_default_datetime_format', DateTimeInterface::ATOM);
}
/**
* 转为数组
* @return array<string, mixed>|mixed
*/
public function toArray(?ToArrayConfig $toArrayConfig = null): mixed
{
$data = [];
$factory = ReflectionReaderFactory::fromClass($this);
$toArrayConfig ??= $factory->getPropertiesToArrayConfig() ?? new ToArrayConfig();
if ($toArrayConfig->singleKey) {
$data = $this->{$toArrayConfig->singleKey};
if ($data instanceof self) {
$data = $data->toArray();
}
return $data;
}
if ($toArrayConfig->only) {
$properties = $toArrayConfig->only;
} else {
$properties = $factory->getPropertyNameList();
if ($toArrayConfig->include) {
$properties = array_merge($properties, $toArrayConfig->include);
}
if ($toArrayConfig->exclude) {
$properties = array_diff($properties, $toArrayConfig->exclude);
}
}
foreach ($properties as $property) {
$value = $this->{$property};
if (is_array($value)) {
$value = array_map(function ($item) {
if ($item instanceof self) {
return $item->toArray();
}
return $item;
}, $value);
if ($toArrayConfig->ignoreNull) {
$value = array_filter($value, fn($v) => $v !== null);
}
if (
$toArrayConfig->emptyArrayAsObject === true
|| (
is_array($toArrayConfig->emptyArrayAsObject)
&& in_array($property, $toArrayConfig->emptyArrayAsObject, true)
)
) {
$value = $value ?: new stdClass();
}
} elseif ($value instanceof self) {
$value = $value->toArray();
if ($toArrayConfig->ignoreNull && is_array($value)) {
$value = array_filter($value, fn($v) => $v !== null);
}
} elseif ($value instanceof DateTime) {
$value = $value->format($this->getDateTimeFormat());
} elseif ($value instanceof BackedEnum) {
$value = $value->value;
}
if ($toArrayConfig->ignoreNull && $value === null) {
continue;
}
$data[$property] = $value;
}
return $data;
}
private static string|null $tmpFromDataConfigCallFrom = null;
private static false|null|FromDataConfig $tmpFromDataConfig = false;
private static function getFromDataConfig(string $callFrom, ?ReflectionClassReader $factory = null): FromDataConfig|null
{
if (self::$tmpFromDataConfig === false) {
self::$tmpFromDataConfigCallFrom = $callFrom;
$factory ??= ReflectionReaderFactory::fromClass(static::class);
self::$tmpFromDataConfig = $factory->getPropertiesFromDataConfig() ?? match (true) {
is_subclass_of(static::class, BaseRequestDTO::class) => FromDataConfig::createForRequestDTO(),
static::class === BaseDTO::class || is_subclass_of(static::class, BaseDTO::class) => FromDataConfig::createForBaseDTO(),
default => null,
};
}
return self::$tmpFromDataConfig;
}
private static function resetFromDataConfig(string $callFrom): void
{
if ($callFrom === self::$tmpFromDataConfigCallFrom) {
// 哪边注入的,哪边重置
self::$tmpFromDataConfigCallFrom = null;
self::$tmpFromDataConfig = false;
}
}
}