| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JetBrains\PhpStorm\Pure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Container\ContainerExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Psr\Container\NotFoundExceptionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ReflectionProperty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Traversable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yiisoft\Validator\DataSet\ArrayDataSet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Yiisoft\Validator\DataSet\MixedDataSet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Yiisoft\Validator\DataSet\ObjectDataSet; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Yiisoft\Validator\Rule\Callback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Yiisoft\Validator\Rule\Trait\PreValidateTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use function is_callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use function is_int; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Validator validates {@link DataSetInterface} against rules set for data set attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | final class Validator implements ValidatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     use PreValidateTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $defaultSkipOnEmptyCallback; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 643 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         private RuleHandlerResolverInterface $ruleHandlerResolver, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |          * @var bool|callable|null | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $defaultSkipOnEmpty = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |          * @var int What visibility levels to use when reading rules from the class specified in `$rules` argument in | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |          * {@see validate()} method. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         private int $rulesPropertyVisibility = ReflectionProperty::IS_PRIVATE | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         | ReflectionProperty::IS_PROTECTED | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         | ReflectionProperty::IS_PUBLIC, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 643 |  |         $this->defaultSkipOnEmptyCallback = SkipOnEmptyNormalizer::normalize($defaultSkipOnEmpty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param DataSetInterface|mixed|RulesProviderInterface $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null $rules | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @throws ContainerExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 114 |  |     public function validate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         mixed $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         iterable|RulesProviderInterface|null $rules = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ?ValidationContext $context = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     ): Result { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 114 |  |         $data = $this->normalizeDataSet($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 114 |  |         if ($rules === null && $data instanceof RulesProviderInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 25 |  |             $rules = $data->getRules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 98 |  |         } elseif ($rules instanceof RulesProviderInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             $rules = $rules->getRules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 96 |  |         } elseif (!$rules instanceof Traversable && !is_array($rules) && $rules !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $rules = (new AttributesRulesProvider($rules, $this->rulesPropertyVisibility))->getRules(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 114 |  |         $context = new ValidationContext( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 114 |  |             $context?->getValidator() ?? $this, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 114 |  |             $context?->getDataSet() ?? $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 114 |  |             $context?->getAttribute() ?? null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 114 |  |             $context?->getParameters() ?? [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 114 |  |         $compoundResult = new Result(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 114 |  |         foreach ($rules ?? [] as $attribute => $attributeRules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 104 |  |             $tempRule = is_iterable($attributeRules) ? $attributeRules : [$attributeRules]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 104 |  |             $attributeRules = $this->normalizeRules($tempRule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 104 |  |             if (is_int($attribute)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 41 |  |                 $validatedData = $data->getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 41 |  |                 $validatedContext = $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 67 |  |                 $validatedData = $data->getAttributeValue($attribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 67 |  |                 $validatedContext = $context->withAttribute($attribute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 104 |  |             $this->validateInternal( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $validatedData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $attributeRules, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $validatedContext, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $compoundResult, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 109 |  |         if ($data instanceof PostValidationHookInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $data->processValidationResult($compoundResult); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 109 |  |         return $compoundResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param iterable<Closure|Closure[]|RuleInterface|RuleInterface[]> $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @throws ContainerExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @throws NotFoundExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 104 |  |     private function validateInternal($value, iterable $rules, ValidationContext $context, Result $compoundResult): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 104 |  |         foreach ($rules as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 104 |  |             if ($rule instanceof BeforeValidationInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 101 |  |                 $preValidateResult = $this->preValidate($value, $context, $rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 101 |  |                 if ($preValidateResult) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 15 |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 99 |  |             $ruleHandler = $this->ruleHandlerResolver->resolve($rule->getHandlerClassName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 97 |  |             $ruleResult = $ruleHandler->validate($value, $rule, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 94 |  |             if ($ruleResult->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 37 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 77 |  |             $context->setParameter($this->parameterPreviousRulesErrored, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 77 |  |             foreach ($ruleResult->getErrors() as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 77 |  |                 $valuePath = $error->getValuePath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 77 |  |                 if ($context->getAttribute() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 53 |  |                     $valuePath = [$context->getAttribute(), ...$valuePath]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 77 |  |                 $compoundResult->addError($error->getMessage(), $valuePath, $error->getParameters()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param array $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return iterable<RuleInterface> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 104 |  |     private function normalizeRules(iterable $rules): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 104 |  |         foreach ($rules as $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 104 |  |             yield $this->normalizeRule($rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 104 |  |     private function normalizeRule($rule): RuleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 104 |  |         if (is_callable($rule)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 3 |  |             return new Callback($rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 104 |  |         if (!$rule instanceof RuleInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                     'Rule should be either an instance of %s or a callable, %s given.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                     RuleInterface::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                     get_debug_type($rule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 104 |  |         if ($rule instanceof SkipOnEmptyInterface && $rule->getSkipOnEmpty() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 99 |  |             $rule = $rule->skipOnEmpty($this->defaultSkipOnEmptyCallback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 104 |  |         return $rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 114 |  |     #[Pure] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     private function normalizeDataSet($data): DataSetInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 114 |  |         if ($data instanceof DataSetInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 47 |  |             return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 72 |  |         if (is_object($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 27 |  |             return new ObjectDataSet($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 49 |  |         if (is_array($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 16 |  |             return new ArrayDataSet($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 43 |  |         return new MixedDataSet($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 199 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |  |