@@ -234,71 +234,14 @@ protected function processRules(
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,96 @@ protected function processRules(
374317 return true ;
375318 }
376319
320+ /**
321+ * @param array|null $rules
322+ * @param array|null $data The array of data to validate, with `DBGroup`.
323+ *
324+ * @return array|true The modified rules or true if we return early
325+ */
326+ private function processIfExist (string $ field , $ rules , ?array $ data )
327+ {
328+ if (in_array ('if_exist ' , $ rules , true )) {
329+ $ flattenedData = array_flatten_with_dots ($ data );
330+ $ ifExistField = $ field ;
331+
332+ if (strpos ($ field , '.* ' ) !== false ) {
333+ // We'll change the dot notation into a PCRE pattern that can be used later
334+ $ ifExistField = str_replace ('\.\* ' , '\.(?:[^\.]+) ' , preg_quote ($ field , '/ ' ));
335+ $ dataIsExisting = false ;
336+ $ pattern = sprintf ('/%s/u ' , $ ifExistField );
337+
338+ foreach (array_keys ($ flattenedData ) as $ item ) {
339+ if (preg_match ($ pattern , $ item ) === 1 ) {
340+ $ dataIsExisting = true ;
341+ break ;
342+ }
343+ }
344+ } else {
345+ $ dataIsExisting = array_key_exists ($ ifExistField , $ flattenedData );
346+ }
347+
348+ unset($ ifExistField , $ flattenedData );
349+
350+ if (! $ dataIsExisting ) {
351+ // we return early if `if_exist` is not satisfied. we have nothing to do here.
352+ return true ;
353+ }
354+
355+ // Otherwise remove the if_exist rule and continue the process
356+ $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'if_exist ' );
357+ }
358+
359+ return $ rules ;
360+ }
361+
362+ /**
363+ * @param array|string $value
364+ * @param array|null $rules
365+ * @param array|null $data The array of data to validate, with `DBGroup`.
366+ *
367+ * @return array|true The modified rules or true if we return early
368+ */
369+ private function processPermitEmpty ($ value , $ rules = null , ?array $ data = null )
370+ {
371+ if (in_array ('permit_empty ' , $ rules , true )) {
372+ if (
373+ ! in_array ('required ' , $ rules , true )
374+ && (is_array ($ value ) ? $ value === [] : trim ((string ) $ value ) === '' )
375+ ) {
376+ $ passed = true ;
377+
378+ foreach ($ rules as $ rule ) {
379+ if (! $ this ->isClosure ($ rule ) && preg_match ('/(.*?)\[(.*)\]/ ' , $ rule , $ match )) {
380+ $ rule = $ match [1 ];
381+ $ param = $ match [2 ];
382+
383+ if (! in_array ($ rule , ['required_with ' , 'required_without ' ], true )) {
384+ continue ;
385+ }
386+
387+ // Check in our rulesets
388+ foreach ($ this ->ruleSetInstances as $ set ) {
389+ if (! method_exists ($ set , $ rule )) {
390+ continue ;
391+ }
392+
393+ $ passed = $ passed && $ set ->{$ rule }($ value , $ param , $ data );
394+ break ;
395+ }
396+ }
397+ }
398+
399+ if ($ passed === true ) {
400+ return true ;
401+ }
402+ }
403+
404+ $ rules = array_filter ($ rules , static fn ($ rule ) => $ rule instanceof Closure || $ rule !== 'permit_empty ' );
405+ }
406+
407+ return $ rules ;
408+ }
409+
377410 /**
378411 * @param Closure|string $rule
379412 */
0 commit comments