| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function is_int; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function is_string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * RulesDumper allows to get an array of rule names and corresponding settings from a set of rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * The array is usually passed to the client to use it in client-side validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * * @see SerializableRuleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * * @see RuleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class RulesDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Return all attribute rules as array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * For example: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      *    'amount' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      *        [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      *            'number', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      *            'integer' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      *            'max' => 100, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      *            'notANumberMessage' => 'Value must be an integer.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      *            'tooBigMessage' => 'Value must be no greater than 100.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *        ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *        ['callback'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      *    ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *    'name' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      *        [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      *            'hasLength', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      *            'max' => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      *            'message' => 'Value must contain at most 20 characters.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      *        ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      *    ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param iterable $rules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 8 |  |     public function asArray(iterable $rules): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 8 |  |         return $this->fetchOptions($rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 8 |  |     private function fetchOptions(iterable $rules): array | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 8 |  |         $result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         /** @var mixed $attribute */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         /** @var mixed $rule */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 8 |  |         foreach ($rules as $attribute => $rule) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 8 |  |             if (!is_int($attribute) && !is_string($attribute)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 $message = sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     'An attribute can only have an integer or a string type. %s given.', | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     get_debug_type($attribute), | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 throw new InvalidArgumentException($message); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 8 |  |             if (is_iterable($rule)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 3 |  |                 $options = $this->fetchOptions($rule); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 8 |  |             } elseif ($rule instanceof SerializableRuleInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 7 |  |                 $options = array_merge([$rule->getName()], $rule->getOptions()); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |             } elseif ($rule instanceof RuleInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 $options = [$rule->getName()]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |                 throw new InvalidArgumentException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                     'Each rule must implement "%s". Type "%s" given.', | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                     RuleInterface::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |                     get_debug_type($rule), | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 )); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 7 |  |             $result[$attribute] = $options; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 7 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |