@@ -218,87 +218,30 @@ public function check($value, $rules, array $errors = [], $dbGroup = null): bool
218218 * so that we can collect all of the first errors.
219219 *
220220 * @param array|string $value
221- * @param array|null $rules
222- * @param array|null $data The array of data to validate, with `DBGroup`.
221+ * @param array $rules
222+ * @param array $data The array of data to validate, with `DBGroup`.
223223 * @param string|null $originalField The original asterisk field name like "foo.*.bar".
224224 */
225225 protected function processRules (
226226 string $ field ,
227227 ?string $ label ,
228228 $ value ,
229- $ rules = null ,
230- ?array $ data = null ,
229+ $ rules = null , // @TODO remove `= null`
230+ ?array $ data = null , // @TODO remove `= null`
231231 ?string $ originalField = null
232232 ): bool {
233233 if ($ data === null ) {
234234 throw new InvalidArgumentException ('You must supply the parameter: data. ' );
235235 }
236236
237- if (in_array ('if_exist ' , $ rules , true )) {
238- $ flattenedData = array_flatten_with_dots ($ data );
239- $ ifExistField = $ field ;
240-
241- if (strpos ($ field , '.* ' ) !== false ) {
242- // We'll change the dot notation into a PCRE pattern that can be used later
243- $ ifExistField = str_replace ('\.\* ' , '\.(?:[^\.]+) ' , preg_quote ($ field , '/ ' ));
244- $ dataIsExisting = false ;
245- $ pattern = sprintf ('/%s/u ' , $ ifExistField );
246-
247- foreach (array_keys ($ flattenedData ) as $ item ) {
248- if (preg_match ($ pattern , $ item ) === 1 ) {
249- $ dataIsExisting = true ;
250- break ;
251- }
252- }
253- } else {
254- $ dataIsExisting = array_key_exists ($ ifExistField , $ flattenedData );
255- }
256-
257- unset($ ifExistField , $ flattenedData );
258-
259- if (! $ dataIsExisting ) {
260- // we return early if `if_exist` is not satisfied. we have nothing to do here.
261- return true ;
262- }
263-
264- // Otherwise remove the if_exist rule and continue the process
265- $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'if_exist ' );
237+ $ rules = $ this ->processIfExist ($ field , $ rules , $ data );
238+ if ($ rules === true ) {
239+ return true ;
266240 }
267241
268- if (in_array ('permit_empty ' , $ rules , true )) {
269- if (
270- ! in_array ('required ' , $ rules , true )
271- && (is_array ($ value ) ? $ value === [] : trim ((string ) $ value ) === '' )
272- ) {
273- $ passed = true ;
274-
275- foreach ($ rules as $ rule ) {
276- if (! $ this ->isClosure ($ rule ) && preg_match ('/(.*?)\[(.*)\]/ ' , $ rule , $ match )) {
277- $ rule = $ match [1 ];
278- $ param = $ match [2 ];
279-
280- if (! in_array ($ rule , ['required_with ' , 'required_without ' ], true )) {
281- continue ;
282- }
283-
284- // Check in our rulesets
285- foreach ($ this ->ruleSetInstances as $ set ) {
286- if (! method_exists ($ set , $ rule )) {
287- continue ;
288- }
289-
290- $ passed = $ passed && $ set ->{$ rule }($ value , $ param , $ data );
291- break ;
292- }
293- }
294- }
295-
296- if ($ passed === true ) {
297- return true ;
298- }
299- }
300-
301- $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'permit_empty ' );
242+ $ rules = $ this ->processPermitEmpty ($ value , $ rules , $ data );
243+ if ($ rules === true ) {
244+ return true ;
302245 }
303246
304247 foreach ($ rules as $ i => $ rule ) {
@@ -374,6 +317,92 @@ protected function processRules(
374317 return true ;
375318 }
376319
320+ /**
321+ * @param array $data The array of data to validate, with `DBGroup`.
322+ *
323+ * @return array|true The modified rules or true if we return early
324+ */
325+ private function processIfExist (string $ field , array $ rules , array $ data )
326+ {
327+ if (in_array ('if_exist ' , $ rules , true )) {
328+ $ flattenedData = array_flatten_with_dots ($ data );
329+ $ ifExistField = $ field ;
330+
331+ if (strpos ($ field , '.* ' ) !== false ) {
332+ // We'll change the dot notation into a PCRE pattern that can be used later
333+ $ ifExistField = str_replace ('\.\* ' , '\.(?:[^\.]+) ' , preg_quote ($ field , '/ ' ));
334+ $ dataIsExisting = false ;
335+ $ pattern = sprintf ('/%s/u ' , $ ifExistField );
336+
337+ foreach (array_keys ($ flattenedData ) as $ item ) {
338+ if (preg_match ($ pattern , $ item ) === 1 ) {
339+ $ dataIsExisting = true ;
340+ break ;
341+ }
342+ }
343+ } else {
344+ $ dataIsExisting = array_key_exists ($ ifExistField , $ flattenedData );
345+ }
346+
347+ if (! $ dataIsExisting ) {
348+ // we return early if `if_exist` is not satisfied. we have nothing to do here.
349+ return true ;
350+ }
351+
352+ // Otherwise remove the if_exist rule and continue the process
353+ $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'if_exist ' );
354+ }
355+
356+ return $ rules ;
357+ }
358+
359+ /**
360+ * @param array|string $value
361+ * @param array $data The array of data to validate, with `DBGroup`.
362+ *
363+ * @return array|true The modified rules or true if we return early
364+ */
365+ private function processPermitEmpty ($ value , array $ rules , array $ data )
366+ {
367+ if (in_array ('permit_empty ' , $ rules , true )) {
368+ if (
369+ ! in_array ('required ' , $ rules , true )
370+ && (is_array ($ value ) ? $ value === [] : trim ((string ) $ value ) === '' )
371+ ) {
372+ $ passed = true ;
373+
374+ foreach ($ rules as $ rule ) {
375+ if (! $ this ->isClosure ($ rule ) && preg_match ('/(.*?)\[(.*)\]/ ' , $ rule , $ match )) {
376+ $ rule = $ match [1 ];
377+ $ param = $ match [2 ];
378+
379+ if (! in_array ($ rule , ['required_with ' , 'required_without ' ], true )) {
380+ continue ;
381+ }
382+
383+ // Check in our rulesets
384+ foreach ($ this ->ruleSetInstances as $ set ) {
385+ if (! method_exists ($ set , $ rule )) {
386+ continue ;
387+ }
388+
389+ $ passed = $ passed && $ set ->{$ rule }($ value , $ param , $ data );
390+ break ;
391+ }
392+ }
393+ }
394+
395+ if ($ passed === true ) {
396+ return true ;
397+ }
398+ }
399+
400+ $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'permit_empty ' );
401+ }
402+
403+ return $ rules ;
404+ }
405+
377406 /**
378407 * @param Closure|string $rule
379408 */
0 commit comments