@@ -87,19 +87,38 @@ public function getMatchesValidationPattern(): string {
87
87
* @param string|null $quantifier The quantifier to apply. Can be 'zeroOrMore', 'oneOrMore', or 'optional'.
88
88
* @return string The modified pattern with the quantifier applied.
89
89
*/
90
- private function applyQuantifier (string $ pattern , string |null $ quantifier ): string {
91
- switch ($ quantifier ) {
92
- case 'zeroOrMore ' || '0> ' || '0+ ' :
93
- $ p = $ pattern . '* ' ;
94
- return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
95
- case 'oneOrMore ' || '1> ' || '1+ ' :
96
- $ p = $ pattern . '+ ' ;
97
- return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
98
- case 'optional ' || '? ' || '| ' :
99
- return $ pattern . '? ' ;
100
- default :
101
- return $ pattern ;
90
+ private function applyQuantifier (string $ pattern , string |null $ q ): string {
91
+
92
+ if (!$ q ) {
93
+ return $ pattern ;
94
+ }
95
+
96
+ if ($ q == 'zeroOrMore ' || $ q == '0> ' || $ q == '0+ ' || $ q == '* ' ) {
97
+ $ p = "( " . $ pattern . ')* ' ;
98
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
99
+ } elseif ($ q == 'oneOrMore ' || $ q == '1> ' || $ q == '1+ ' || $ q == '+ ' ) {
100
+ $ p = "( " . $ pattern . ')+ ' ;
101
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
102
+ } elseif ($ q == 'optional ' || $ q == '? ' || $ q == '| ' ) {
103
+ $ p = "( " . $ pattern . ')? ' ;
104
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
102
105
}
106
+
107
+ if (is_int ($ q )) {
108
+ $ p = "( " . $ pattern . "){ " .$ q ."} " ;
109
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
110
+ } elseif (preg_match ("/^\d{1,10}$/ " , $ q )) {
111
+ $ p = "( " . $ pattern . '){ ' .$ q .'} ' ;
112
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
113
+ } elseif (preg_match ("/^\d{1,10},\d{1,10}$/ " , $ q )) {
114
+ $ range = explode (", " , $ q );
115
+ $ f = $ range [0 ];
116
+ $ s = $ range [1 ];
117
+ $ p = "( " . $ pattern . ") " . "{ " . $ f . ", " . $ s ."} " ;
118
+ return $ this ->lazy ? $ this ->addLazy ($ p ) : $ p ;
119
+ }
120
+
121
+ return $ pattern ;
103
122
}
104
123
105
124
/**
@@ -111,9 +130,11 @@ private function applyQuantifier(string $pattern, string|null $quantifier): stri
111
130
* @return string The generated regex quantifier string.
112
131
*/
113
132
private function getLengthOption (int |null $ length = null , int $ minLength = 0 , int $ maxLength = 0 ): string {
114
- if (is_int ($ length ) && $ length >= 0 ) {
133
+ if (is_int ($ length ) && $ length > 0 ) {
115
134
$ qntf = "{ " . $ length . "} " ;
116
135
return $ this ->lazy ? $ this ->addLazy ($ qntf ) : $ qntf ;
136
+ } elseif ($ length === 0 ) {
137
+ return "" ;
117
138
}
118
139
119
140
if ($ minLength > 0 && $ maxLength > 0 ) {
@@ -159,10 +180,11 @@ public function lazy(): self {
159
180
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
160
181
* @return self
161
182
*/
162
- public function charSet (callable $ callback ): self {
183
+ public function charSet (callable $ callback, ? string $ q = null ): self {
163
184
$ subPattern = new self ();
164
185
$ callback ($ subPattern );
165
- $ this ->pattern .= '[ ' . $ subPattern ->getPattern () . '] ' ;
186
+ $ p = '[ ' . $ subPattern ->getPattern () . '] ' ;
187
+ $ this ->pattern .= $ q ? $ this ->applyQuantifier ($ p , $ q ) : $ p ;
166
188
return $ this ;
167
189
}
168
190
@@ -172,10 +194,11 @@ public function charSet(callable $callback): self {
172
194
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
173
195
* @return self
174
196
*/
175
- public function negativeCharSet (callable $ callback ): self {
197
+ public function negativeCharSet (callable $ callback, ? string $ q = null ): self {
176
198
$ subPattern = new self ();
177
199
$ callback ($ subPattern );
178
- $ this ->pattern .= '[^ ' . $ subPattern ->getPattern () . '] ' ;
200
+ $ p = '[^ ' . $ subPattern ->getPattern () . '] ' ;
201
+ $ this ->pattern .= $ q ? $ this ->applyQuantifier ($ p , $ q ) : $ p ;
179
202
return $ this ;
180
203
}
181
204
@@ -185,10 +208,11 @@ public function negativeCharSet(callable $callback): self {
185
208
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
186
209
* @return self
187
210
*/
188
- public function group (callable $ callback ): self {
211
+ public function group (callable $ callback, ? string $ q = null ): self {
189
212
$ subPattern = new self ();
190
213
$ callback ($ subPattern );
191
- $ this ->pattern .= '( ' . $ subPattern ->getPattern () . ') ' ;
214
+ $ p = $ subPattern ->getPattern ();
215
+ $ this ->pattern .= $ q ? $ this ->applyQuantifier ($ p , $ q ) : '( ' . $ p . ') ' ;
192
216
return $ this ;
193
217
}
194
218
@@ -198,10 +222,11 @@ public function group(callable $callback): self {
198
222
* @param callable $callback A callback that receives a BuilderPattern instance to define the subpattern.
199
223
* @return self
200
224
*/
201
- public function nonCapturingGroup (callable $ callback ): self {
225
+ public function nonCapturingGroup (callable $ callback, ? string $ q = null ): self {
202
226
$ subPattern = new self ();
203
227
$ callback ($ subPattern );
204
- $ this ->pattern .= '(?: ' . $ subPattern ->getPattern () . ') ' ;
228
+ $ p = '(?: ' . $ subPattern ->getPattern () . ') ' ;
229
+ $ this ->pattern .= $ q ? $ this ->applyQuantifier ($ p , $ q ) : $ p ;
205
230
return $ this ;
206
231
}
207
232
@@ -211,10 +236,11 @@ public function nonCapturingGroup(callable $callback): self {
211
236
* @param callable $callback A callback that receives a BuilderPattern instance to define the alternation.
212
237
* @return self
213
238
*/
214
- public function orPattern (callable $ callback ): self {
239
+ public function orPattern (callable $ callback, ? string $ q = null ): self {
215
240
$ builder = new self ();
216
241
$ callback ($ builder );
217
- $ this ->pattern .= '| ' . $ builder ->getPattern ();
242
+ $ p = $ builder ->getPattern ();
243
+ $ this ->pattern .= $ q ? '| ' . $ this ->applyQuantifier ($ p , $ q ) : '| ' . $ p ;
218
244
return $ this ;
219
245
}
220
246
0 commit comments