|
11 | 11 |
|
12 | 12 | class ProcessorBuilder
|
13 | 13 | {
|
14 |
| - public function __construct(private ProcessorRegistry $registry) |
| 14 | + public function __construct(private readonly ProcessorRegistry $registry) |
15 | 15 | {
|
16 | 16 | }
|
17 | 17 |
|
18 |
| - public function build(string $context, string $name, array $config = []): Processor |
| 18 | + public function build(string $context, string $name, array $processorConfig = []): Processor |
19 | 19 | {
|
20 | 20 | $processor = $this->registry->get($context, $name);
|
21 |
| - if ($processor instanceof ConfigurableProcessor && !empty($config)) { |
22 |
| - $processor->configure($config); |
| 21 | + if ($processor instanceof ConfigurableProcessor && !empty($processorConfig)) { |
| 22 | + $processor->configure($processorConfig); |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | return $processor;
|
26 | 26 | }
|
27 | 27 |
|
| 28 | + /** |
| 29 | + * @param array<int|string, string|array<string, mixed>> $processorSpecs |
| 30 | + */ |
28 | 31 | public function buildPipeline(string $context, array $processorSpecs): Pipeline
|
29 | 32 | {
|
30 | 33 | $pipeline = new ProcessorPipeline();
|
31 |
| - foreach ($processorSpecs as $name => $config) { |
32 |
| - if (is_int($name)) { |
33 |
| - $name = $config; |
34 |
| - $config = []; |
35 |
| - } |
36 |
| - $processor = $this->build($context, $name, $config); |
| 34 | + foreach ($processorSpecs as $key => $spec) { |
| 35 | + $processorName = $this->resolveProcessorName($key, $spec); |
| 36 | + $processorConfig = $this->resolveProcessorConfig($key, $spec); |
| 37 | + $processor = $this->build($context, $processorName, $processorConfig); |
37 | 38 | $pipeline->addProcessor($processor);
|
38 | 39 | }
|
39 | 40 |
|
40 | 41 | return $pipeline;
|
41 | 42 | }
|
| 43 | + |
| 44 | + private function isUnnamedProcessor(int|string $key): bool |
| 45 | + { |
| 46 | + return is_int($key); |
| 47 | + } |
| 48 | + |
| 49 | + private function resolveProcessorName(int|string $key, string|array $spec): string |
| 50 | + { |
| 51 | + return $this->isUnnamedProcessor($key) ? (string) $spec : (string) $key; |
| 52 | + } |
| 53 | + |
| 54 | + private function resolveProcessorConfig(int|string $key, string|array $spec): array |
| 55 | + { |
| 56 | + return $this->isUnnamedProcessor($key) ? [] : (array) $spec; |
| 57 | + } |
42 | 58 | }
|
0 commit comments