Skip to content

Commit 1b9e3e7

Browse files
authored
Merge pull request #4 from KaririCode-Framework/develop
refactor(processor-builder): apply readonly property to enhance immut…
2 parents 79bd873 + e71eec2 commit 1b9e3e7

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/ProcessorBuilder.php

+26-10
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,48 @@
1111

1212
class ProcessorBuilder
1313
{
14-
public function __construct(private ProcessorRegistry $registry)
14+
public function __construct(private readonly ProcessorRegistry $registry)
1515
{
1616
}
1717

18-
public function build(string $context, string $name, array $config = []): Processor
18+
public function build(string $context, string $name, array $processorConfig = []): Processor
1919
{
2020
$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);
2323
}
2424

2525
return $processor;
2626
}
2727

28+
/**
29+
* @param array<int|string, string|array<string, mixed>> $processorSpecs
30+
*/
2831
public function buildPipeline(string $context, array $processorSpecs): Pipeline
2932
{
3033
$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);
3738
$pipeline->addProcessor($processor);
3839
}
3940

4041
return $pipeline;
4142
}
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+
}
4258
}

0 commit comments

Comments
 (0)