44
55readonly 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 ,
0 commit comments