Repository navigation
Expand file tree
/
Copy pathToArrayPipeline.php
More file actions
113 lines (96 loc) · 3.14 KB
/
Copy pathToArrayPipeline.php
File metadata and controls
113 lines (96 loc) · 3.14 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
<?php
namespace WebmanTech\DTO\Processor;
use InvalidArgumentException;
use WebmanTech\DTO\Exceptions\DTOException;
use WebmanTech\DTO\Exceptions\DTOProcessorException;
use WebmanTech\DTO\Helper\ConfigHelper;
/**
* toArray 处理器
*/
final class ToArrayPipeline
{
/** @var list<class-string<ToArrayProcessorInterface>> */
private const DEFAULT_PROCESSORS = [
SelectProperties::class,
SerializeProperties::class,
FilterNullValues::class,
ConvertEmptyArraysToObjects::class,
EncryptFields::class,
ExtractSingleKey::class,
];
/** @var list<ToArrayProcessorInterface> */
private array $processors;
/**
* @param list<ToArrayProcessorInterface> $processors
*/
public function __construct(array $processors = [])
{
$this->processors = $processors;
}
/**
* 根据配置创建独立实例
*/
public static function create(): self
{
$processorClasses = ConfigHelper::get('dto.to_array_processors', self::DEFAULT_PROCESSORS);
if (!is_array($processorClasses)) {
throw new InvalidArgumentException('dto.to_array_processors must be an array');
}
$processors = [];
foreach ($processorClasses as $processorClass) {
if (!is_string($processorClass) || !is_a($processorClass, ToArrayProcessorInterface::class, true)) {
throw new InvalidArgumentException('dto.to_array_processors must contain ToArrayProcessorInterface class names');
}
$processors[] = new $processorClass();
}
return new self($processors);
}
public function process(ToArrayContext $context): void
{
foreach ($this->processors as $processor) {
try {
$processor($context);
} catch (DTOException $e) {
throw $e;
} catch (\Throwable $e) {
throw new DTOProcessorException($processor::class, previous: $e);
}
}
}
public function add(ToArrayProcessorInterface $processor): self
{
$this->processors[] = $processor;
return $this;
}
/**
* @param class-string<ToArrayProcessorInterface> $before
*/
public function insert(ToArrayProcessorInterface $processor, string $before): self
{
foreach ($this->processors as $index => $current) {
if ($current instanceof $before) {
array_splice($this->processors, $index, 0, [$processor]);
return $this;
}
}
throw new InvalidArgumentException("Processor {$before} not found");
}
/**
* @param class-string<ToArrayProcessorInterface> $processorClass
*/
public function remove(string $processorClass): self
{
$this->processors = array_values(array_filter(
$this->processors,
static fn(ToArrayProcessorInterface $processor): bool => !$processor instanceof $processorClass,
));
return $this;
}
/**
* @return list<ToArrayProcessorInterface>
*/
public function processors(): array
{
return $this->processors;
}
}