@@ -28,10 +28,24 @@ class Builder implements BuilderContract {
28
28
29
29
use BuilderPatternMethods;
30
30
31
+ /**
32
+ * The string to be processed with regex.
33
+ */
31
34
protected string $ str ;
35
+
36
+ /**
37
+ * The current pattern being applied.
38
+ */
32
39
protected PatternContract $ pattern ;
40
+
41
+ /**
42
+ * Manages options for the current pattern.
43
+ */
33
44
protected OptionsManager $ manager ;
34
45
46
+ /**
47
+ * List of available patterns.
48
+ */
35
49
public array $ patterns =
36
50
[
37
51
TextOrNumbersPattern::class,
@@ -52,39 +66,69 @@ class Builder implements BuilderContract {
52
66
FilePathWinPattern::class,
53
67
];
54
68
69
+ /**
70
+ * Constructs a new Builder instance.
71
+ *
72
+ * @param string $str The initial string to process with regex.
73
+ */
55
74
public function __construct (string $ str = "" ) {
56
75
$ this ->str = $ str ;
57
76
$ this ->manager = new OptionsManager ();
58
77
// @todo add patterns via config
59
78
}
60
79
80
+ /**
81
+ * Processes an options array and applies it to the current pattern.
82
+ *
83
+ * @param array $options An associative array of options.
84
+ */
61
85
protected function processConfigArray (array $ options ) {
62
86
// Build options in options manager
63
87
$ this ->manager ->buildOptions ($ options );
64
88
// Set used options to pattern
65
89
$ this ->pattern ->setOptions ($ this ->manager ->getUsedOptions ());
66
90
}
67
91
92
+ /**
93
+ * Processes a callback function to configure options.
94
+ *
95
+ * @param callable $callback A function that configures options using an OptionsBuilder.
96
+ */
68
97
protected function processCallback (callable $ callback ) {
69
98
$ optionsBuilder = new OptionsBuilder ();
70
99
$ callback ($ optionsBuilder );
71
100
$ this ->processConfigArray ($ optionsBuilder ->getOptions ());
72
101
}
73
102
103
+ /**
104
+ * Validates the current string as an exact match against the current pattern.
105
+ *
106
+ * @return bool True if the string is an exact match, false otherwise.
107
+ */
74
108
protected function validateAsInput (): bool {
75
109
if (!$ this ->pattern ->validateInput ($ this ->str )) {
76
110
return false ;
77
111
}
78
112
return true ;
79
113
}
80
114
115
+ /**
116
+ * Validates if the current string contains any matches for the current pattern.
117
+ *
118
+ * @return bool True if the string contains matches, false otherwise.
119
+ */
81
120
protected function validateAsString (): bool {
82
121
if (!$ this ->pattern ->validateMatches ($ this ->str )) {
83
122
return false ;
84
123
}
85
124
return true ;
86
125
}
87
126
127
+ /**
128
+ * Retrieves all matches of the current pattern within the string.
129
+ *
130
+ * @return array|null An array of matches or null if no matches are found.
131
+ */
88
132
protected function getAllMatches (): ?array {
89
133
return $ this ->pattern ->getMatches ($ this ->str );
90
134
}
@@ -157,11 +201,23 @@ public function setOptions(array|callable $config): void {
157
201
}
158
202
}
159
203
204
+ /**
205
+ * Registers a single pattern in the Builder.
206
+ *
207
+ * @param PatternContract $pattern The pattern to register.
208
+ * @return self The Builder instance, for method chaining.
209
+ */
160
210
public function registerPattern (PatternContract $ pattern ): self {
161
211
$ this ->patterns [] = $ pattern ;
162
212
return $ this ;
163
213
}
164
214
215
+ /**
216
+ * Registers multiple patterns in the Builder.
217
+ *
218
+ * @param array $patterns An array of patterns to register.
219
+ * @return self The Builder instance, for method chaining.
220
+ */
165
221
public function registerPatterns (array $ patterns ): self {
166
222
foreach ($ patterns as $ pattern ) {
167
223
if (is_subclass_of ($ pattern , PatternContract::class)) {
@@ -173,10 +229,17 @@ public function registerPatterns(array $patterns): self {
173
229
return $ this ;
174
230
}
175
231
232
+ /**
233
+ * Retrieves the list of registered patterns.
234
+ *
235
+ * @return array An array of registered patterns.
236
+ */
176
237
public function getPatterns (): array {
177
238
return $ this ->patterns ;
178
239
}
179
240
241
+ // Expression flag methods:
242
+
180
243
public function asCaseInsensitive (): self {
181
244
$ this ->pattern ->addExpressionFlag ("i " );
182
245
return $ this ;
@@ -201,7 +264,17 @@ public function asSticky(): self {
201
264
$ this ->pattern ->addExpressionFlag ("y " );
202
265
return $ this ;
203
266
}
204
-
267
+
268
+ /**
269
+ * Dynamically handles calls to pattern methods.
270
+ *
271
+ * This method is invoked when a method is called on the Builder that matches the name of a registered pattern.
272
+ *
273
+ * @param string $name The name of the method being called.
274
+ * @param array $args The arguments passed to the method.
275
+ * @return self The Builder instance, for method chaining.
276
+ * @throws \LogicException If no patterns are registered or the pattern name is not found.
277
+ */
205
278
public function __call (
206
279
$ name ,
207
280
$ args
0 commit comments