Skip to content

Commit db0ee89

Browse files
committed
feat: defer conditional evaluation to verify() for more intuitive rule composition
1 parent f88d70b commit db0ee89

14 files changed

Lines changed: 1633 additions & 240 deletions

benchmarks/DataVerifyBench.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,148 @@ public function benchLoadFromDirectory(): void
472472
$dv->field('test')->string;
473473
$dv->verify();
474474
}
475+
476+
/**
477+
* @Revs(1000)
478+
* @Iterations(5)
479+
*/
480+
public function benchDeferredConditionalDefaultPlusConditional(): void
481+
{
482+
$data = new stdClass();
483+
$data->user = new stdClass();
484+
$data->user->id = 123;
485+
$data->user->password = 'StrongP@ss123!';
486+
487+
$dv = new DataVerify($data);
488+
$dv->field('user')->object
489+
->subfield('password')
490+
->minLength(8) // Default rule
491+
->containsUpper // Default rule
492+
->when('user.id', '!=', null)
493+
->then->required; // Conditional rule
494+
495+
$dv->verify();
496+
}
497+
498+
/**
499+
* @Revs(1000)
500+
* @Iterations(5)
501+
*/
502+
public function benchDeferredConditionalMultipleBlocks(): void
503+
{
504+
$data = new stdClass();
505+
$data->user = new stdClass();
506+
$data->user->id = 123;
507+
$data->user->role = 'admin';
508+
$data->user->password = 'VeryStrongP@ss123!';
509+
510+
$dv = new DataVerify($data);
511+
$dv->field('user')->object
512+
->subfield('password')
513+
->minLength(8) // Default
514+
->when('user.id', '!=', null)
515+
->then->required // Block 1
516+
->when('user.role', '=', 'admin')
517+
->then->minLength(16) // Block 2
518+
->containsSpecialCharacter; // Block 2
519+
520+
$dv->verify();
521+
}
522+
523+
/**
524+
* @Revs(1000)
525+
* @Iterations(5)
526+
*/
527+
public function benchDeferredConditionalNotTriggered(): void
528+
{
529+
$data = new stdClass();
530+
$data->user = new stdClass();
531+
$data->user->id = null;
532+
$data->user->password = 'Short1!';
533+
534+
$dv = new DataVerify($data);
535+
$dv->field('user')->object
536+
->subfield('password')
537+
->minLength(8) // Default - will fail
538+
->containsUpper // Default - passes
539+
->when('user.id', '!=', null)
540+
->then->required // NOT triggered
541+
->minLength(16); // NOT triggered
542+
543+
$dv->verify();
544+
}
545+
546+
/**
547+
* @Revs(500)
548+
* @Iterations(5)
549+
*/
550+
public function benchDeferredConditionalAPIPostPatch(): void
551+
{
552+
// Simulates PATCH scenario
553+
$data = new stdClass();
554+
$data->user = new stdClass();
555+
$data->user->id = 456;
556+
$data->user->email = 'update@example.com';
557+
// password optional in PATCH
558+
559+
$dv = new DataVerify($data);
560+
$dv->field('user')->object
561+
->subfield('email')
562+
->email // Always check format
563+
->when('user.id', '!=', null)
564+
->then->required // Required if updating
565+
->subfield('password')
566+
->minLength(12) // Always check if present
567+
->containsUpper // Always check if present
568+
->when('user.id', '!=', null)
569+
->then->required; // Required if updating
570+
571+
$dv->verify();
572+
}
573+
574+
/**
575+
* @Revs(1000)
576+
* @Iterations(5)
577+
*/
578+
public function benchDeferredConditionalAcrossFields(): void
579+
{
580+
$data = new stdClass();
581+
$data->x = 1;
582+
$data->y = 2;
583+
$data->field1 = 'value1';
584+
$data->field2 = 'value2';
585+
586+
$dv = new DataVerify($data);
587+
$dv->field('field1')
588+
->string
589+
->when('x', '=', 1)
590+
->then->required
591+
->field('field2') // Terminates previous block
592+
->string
593+
->when('y', '=', 2)
594+
->then->required;
595+
596+
$dv->verify();
597+
}
598+
599+
/**
600+
* @Revs(500)
601+
* @Iterations(5)
602+
*/
603+
public function benchDeferredConditionalComplex(): void
604+
{
605+
$data = new stdClass();
606+
$data->tier = 'premium';
607+
$data->status = 'active';
608+
$data->features = ['api', 'support'];
609+
610+
$dv = new DataVerify($data);
611+
$dv->field('features')
612+
->array // Default
613+
->when('tier', '=', 'premium')
614+
->and('status', '=', 'active')
615+
->then->required; // Conditional with AND
616+
617+
$dv->verify();
618+
}
475619
}

0 commit comments

Comments
 (0)