| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JetBrains\PhpStorm\ArrayShape; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\PropagateOptionsInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\Rule\Trait\WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\RuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Validator\SerializableRuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Validator\SkipOnEmptyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Yiisoft\Validator\SkipOnErrorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Yiisoft\Validator\WhenInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Validates an array by checking each of its elements against a set of rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | final class Each implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     SerializableRuleInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     SkipOnErrorInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     WhenInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     SkipOnEmptyInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     PropagateOptionsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use SkipOnEmptyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     use SkipOnErrorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     use WhenTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 4 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |          * @var iterable<RuleInterface> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         private iterable $rules = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         private string $incorrectInputMessage = 'Value must be array or iterable.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          * @var bool|callable|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         private $skipOnEmpty = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         private bool $skipOnError = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |          * @var Closure(mixed, ValidationContext):bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         private ?Closure $when = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |     public function getName(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |         return 'each'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     public function propagateOptions(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $rules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         foreach ($this->rules as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             if ($rule instanceof SkipOnEmptyInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 $rule = $rule->skipOnEmpty($this->skipOnEmpty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |             if ($rule instanceof SkipOnErrorInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |                 $rule = $rule->skipOnError($this->skipOnError); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             if ($rule instanceof WhenInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                 $rule = $rule->when($this->when); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             $rules[] = $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             if ($rule instanceof PropagateOptionsInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |                 $rule->propagateOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $this->rules = $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |     public function getRules(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 9 |  |         return $this->rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |     public function getIncorrectInputMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |         return $this->incorrectInputMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |     #[ArrayShape([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         'incorrectInputMessage' => 'array', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         'skipOnEmpty' => 'bool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         'skipOnError' => 'bool', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         'rules' => 'array', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     ])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |         $arrayOfRules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         foreach ($this->rules as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |             if ($rule instanceof SerializableRuleInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |                 $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $arrayOfRules[] = [$rule->getName()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'incorrectInputMessage' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |                 'message' => $this->getIncorrectInputMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |             'skipOnEmpty' => $this->getSkipOnEmptyOption(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |             'skipOnError' => $this->skipOnError, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             'rules' => $arrayOfRules, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 9 |  |     public function getHandlerClassName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |         return EachHandler::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |