Skip to content

Commit b3035b0

Browse files
committed
Add fluent configuration pipeline API
1 parent 709fab0 commit b3035b0

3 files changed

Lines changed: 177 additions & 65 deletions

File tree

src/Configuration.php

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,103 @@
44

55
readonly class Configuration
66
{
7+
/**
8+
* Create a new parser/refiner configuration.
9+
*
10+
* @param array<int, Parser> $parsers
11+
* @param array<int, Refiner> $refiners
12+
*/
13+
public static function make(array $parsers = [], array $refiners = []): self
14+
{
15+
return new self($parsers, $refiners);
16+
}
17+
718
/**
819
* Create a parser/refiner configuration.
920
*
1021
* @param array<int, Parser> $parsers
1122
* @param array<int, Refiner> $refiners
1223
*/
1324
public function __construct(
14-
public readonly array $parsers = [],
25+
protected readonly array $parsers = [],
1526

16-
public readonly array $refiners = [],
27+
protected readonly array $refiners = [],
1728
) {}
1829

30+
/**
31+
* Get the configured parsers.
32+
*
33+
* @return array<int, Parser>
34+
*/
35+
public function parsers(): array
36+
{
37+
return $this->parsers;
38+
}
39+
40+
/**
41+
* Get the configured refiners.
42+
*
43+
* @return array<int, Refiner>
44+
*/
45+
public function refiners(): array
46+
{
47+
return $this->refiners;
48+
}
49+
50+
/**
51+
* Determine whether the configuration has the given parser.
52+
*
53+
* @param class-string<Parser> $parser
54+
*/
55+
public function hasParser(string $parser): bool
56+
{
57+
foreach ($this->parsers as $configuredParser) {
58+
if ($configuredParser instanceof $parser) {
59+
return true;
60+
}
61+
}
62+
63+
return false;
64+
}
65+
66+
/**
67+
* Determine whether the configuration has the given refiner.
68+
*
69+
* @param class-string<Refiner> $refiner
70+
*/
71+
public function hasRefiner(string $refiner): bool
72+
{
73+
foreach ($this->refiners as $configuredRefiner) {
74+
if ($configuredRefiner instanceof $refiner) {
75+
return true;
76+
}
77+
}
78+
79+
return false;
80+
}
81+
1982
/**
2083
* Return a configuration with the given parser added.
2184
*/
22-
public function withParser(Parser $parser, bool $prepend = false): self
85+
public function addParser(Parser $parser): self
2386
{
24-
return new self(
25-
$prepend ? [$parser, ...$this->parsers] : [...$this->parsers, $parser],
26-
$this->refiners,
27-
);
87+
return new self([...$this->parsers, $parser], $this->refiners);
88+
}
89+
90+
/**
91+
* Return a configuration with the given parser added to the beginning.
92+
*/
93+
public function prependParser(Parser $parser): self
94+
{
95+
return new self([$parser, ...$this->parsers], $this->refiners);
2896
}
2997

3098
/**
3199
* Return a configuration without parsers matching the given class name.
32100
*
33101
* @param class-string<Parser> $parser
34102
*/
35-
public function withoutParser(string $parser): self
103+
public function removeParser(string $parser): self
36104
{
37105
return new self(
38106
array_values(array_filter(
@@ -46,20 +114,25 @@ public function withoutParser(string $parser): self
46114
/**
47115
* Return a configuration with the given refiner added.
48116
*/
49-
public function withRefiner(Refiner $refiner, bool $prepend = false): self
117+
public function addRefiner(Refiner $refiner): self
50118
{
51-
return new self(
52-
$this->parsers,
53-
$prepend ? [$refiner, ...$this->refiners] : [...$this->refiners, $refiner],
54-
);
119+
return new self($this->parsers, [...$this->refiners, $refiner]);
120+
}
121+
122+
/**
123+
* Return a configuration with the given refiner added to the beginning.
124+
*/
125+
public function prependRefiner(Refiner $refiner): self
126+
{
127+
return new self($this->parsers, [$refiner, ...$this->refiners]);
55128
}
56129

57130
/**
58131
* Return a configuration without refiners matching the given class name.
59132
*
60133
* @param class-string<Refiner> $refiner
61134
*/
62-
public function withoutRefiner(string $refiner): self
135+
public function removeRefiner(string $refiner): self
63136
{
64137
return new self(
65138
$this->parsers,

src/ConfiguredChronoEngine.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function parse(string $text, Reference $reference, Options $options): arr
2020
{
2121
$results = array_merge(...array_map(
2222
fn (Parser $parser) => $parser->parse($text, $reference, $options),
23-
$this->configuration->parsers,
23+
$this->configuration->parsers(),
2424
));
2525

2626
$results = $this->attachReference($results, $reference);
@@ -35,7 +35,7 @@ public function parse(string $text, Reference $reference, Options $options): arr
3535

3636
$results = array_map(fn (array $result): ParsedResult => $result[0], $results);
3737

38-
foreach ($this->configuration->refiners as $refiner) {
38+
foreach ($this->configuration->refiners() as $refiner) {
3939
$results = $this->attachReference(
4040
$refiner->refine($text, $results, $reference, $options),
4141
$reference,
@@ -51,8 +51,8 @@ public function parse(string $text, Reference $reference, Options $options): arr
5151
public function clone(): self
5252
{
5353
return $this->newInstance(new Configuration(
54-
parsers: [...$this->configuration->parsers],
55-
refiners: [...$this->configuration->refiners],
54+
parsers: [...$this->configuration->parsers()],
55+
refiners: [...$this->configuration->refiners()],
5656
));
5757
}
5858

@@ -61,7 +61,11 @@ public function clone(): self
6161
*/
6262
public function withParser(Parser $parser, bool $prepend = false): self
6363
{
64-
return $this->newInstance($this->configuration->withParser($parser, $prepend));
64+
return $this->newInstance(
65+
$prepend
66+
? $this->configuration->prependParser($parser)
67+
: $this->configuration->addParser($parser),
68+
);
6569
}
6670

6771
/**
@@ -71,15 +75,19 @@ public function withParser(Parser $parser, bool $prepend = false): self
7175
*/
7276
public function withoutParser(string $parser): self
7377
{
74-
return $this->newInstance($this->configuration->withoutParser($parser));
78+
return $this->newInstance($this->configuration->removeParser($parser));
7579
}
7680

7781
/**
7882
* Return an engine instance with the given refiner added.
7983
*/
8084
public function withRefiner(Refiner $refiner, bool $prepend = false): self
8185
{
82-
return $this->newInstance($this->configuration->withRefiner($refiner, $prepend));
86+
return $this->newInstance(
87+
$prepend
88+
? $this->configuration->prependRefiner($refiner)
89+
: $this->configuration->addRefiner($refiner),
90+
);
8391
}
8492

8593
/**
@@ -89,7 +97,7 @@ public function withRefiner(Refiner $refiner, bool $prepend = false): self
8997
*/
9098
public function withoutRefiner(string $refiner): self
9199
{
92-
return $this->newInstance($this->configuration->withoutRefiner($refiner));
100+
return $this->newInstance($this->configuration->removeRefiner($refiner));
93101
}
94102

95103
/**

0 commit comments

Comments
 (0)