@@ -170,16 +170,9 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
170
170
if (strpos ($ field , '* ' ) !== false ) {
171
171
$ flattenedArray = array_flatten_with_dots ($ data );
172
172
173
- $ pattern = '/\A '
174
- . str_replace (
175
- ['\.\* ' , '\*\. ' ],
176
- ['\.[^.]+ ' , '[^.]+\. ' ],
177
- preg_quote ($ field , '/ ' )
178
- )
179
- . '\z/ ' ;
180
173
$ values = array_filter (
181
174
$ flattenedArray ,
182
- static fn ($ key ) => preg_match ($ pattern , $ key ),
175
+ static fn ($ key ) => preg_match ($ this -> getRegex ( $ field ) , $ key ),
183
176
ARRAY_FILTER_USE_KEY
184
177
);
185
178
@@ -220,6 +213,20 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
220
213
return false ;
221
214
}
222
215
216
+ /**
217
+ * Returns regex pattern for key with dot array syntax.
218
+ */
219
+ private function getRegex (string $ field ): string
220
+ {
221
+ return '/\A '
222
+ . str_replace (
223
+ ['\.\* ' , '\*\. ' ],
224
+ ['\.[^.]+ ' , '[^.]+\. ' ],
225
+ preg_quote ($ field , '/ ' )
226
+ )
227
+ . '\z/ ' ;
228
+ }
229
+
223
230
/**
224
231
* Runs the validation process, returning true or false determining whether
225
232
* validation was successful or not.
@@ -823,15 +830,7 @@ private function retrievePlaceholders(string $rule, array $data): array
823
830
*/
824
831
public function hasError (string $ field ): bool
825
832
{
826
- $ pattern = '/\A '
827
- . str_replace (
828
- ['\.\* ' , '\*\. ' ],
829
- ['\.[^.]+ ' , '[^.]+\. ' ],
830
- preg_quote ($ field , '/ ' )
831
- )
832
- . '\z/ ' ;
833
-
834
- return (bool ) preg_grep ($ pattern , array_keys ($ this ->getErrors ()));
833
+ return (bool ) preg_grep ($ this ->getRegex ($ field ), array_keys ($ this ->getErrors ()));
835
834
}
836
835
837
836
/**
@@ -844,16 +843,9 @@ public function getError(?string $field = null): string
844
843
$ field = array_key_first ($ this ->rules );
845
844
}
846
845
847
- $ pattern = '/\A '
848
- . str_replace (
849
- ['\.\* ' , '\*\. ' ],
850
- ['\.[^.]+ ' , '[^.]+\. ' ],
851
- preg_quote ($ field , '/ ' )
852
- )
853
- . '\z/ ' ;
854
846
$ errors = array_filter (
855
847
$ this ->getErrors (),
856
- static fn ($ key ) => preg_match ($ pattern , $ key ),
848
+ static fn ($ key ) => preg_match ($ this -> getRegex ( $ field ) , $ key ),
857
849
ARRAY_FILTER_USE_KEY
858
850
);
859
851
0 commit comments