| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Traversable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\SerializableRuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\BeforeValidationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\Rule\Trait\RuleNameTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Validator\RuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Validator\RulesDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function is_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Can be used for validation of nested structures. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | final class Nested implements SerializableRuleInterface, BeforeValidationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     use BeforeValidationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     use RuleNameTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 4 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |          * @var RuleInterface[][] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         private iterable $rules = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         private bool $errorWhenPropertyPathIsNotFound = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         private string $propertyPathIsNotFoundMessage = 'Property path "{path}" is not found.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         private bool $skipOnEmpty = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         private bool $skipOnError = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |          * @var Closure(mixed, ValidationContext):bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         private ?Closure $when = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |         $rules = $rules instanceof Traversable ? iterator_to_array($rules) : $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |         if (empty($rules)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             throw new InvalidArgumentException('Rules must not be empty.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         if ($this->checkRules($rules)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |             $message = sprintf('Each rule should be an instance of %s.', RuleInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             throw new InvalidArgumentException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |         $this->rules = $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 16 |  |     public function getRules(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 16 |  |         return $this->rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 16 |  |     public function isErrorWhenPropertyPathIsNotFound(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 16 |  |         return $this->errorWhenPropertyPathIsNotFound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |     public function getPropertyPathIsNotFoundMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |         return $this->propertyPathIsNotFoundMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |     private function checkRules(array $rules): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         return array_reduce( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $rules, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |             function (bool $carry, $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |                 return $carry || (is_array($rule) ? $this->checkRules($rule) : !$rule instanceof RuleInterface); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |     public function getOptions(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  |         return (new RulesDumper())->asArray($this->rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |     public function getHandlerClassName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         return NestedHandler::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |