| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Yiisoft\Validator\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Countable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Yiisoft\Validator\Exception\UnexpectedRuleException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Yiisoft\Validator\Formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Validator\FormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\Validator\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Yiisoft\Validator\ValidationContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Validates that the value contains certain number of items. Can be applied to arrays or classes implementing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * {@see Countable} interface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | final class CountHandler implements RuleHandlerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private FormatterInterface $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 20 |  |     public function __construct(?FormatterInterface $formatter = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 20 |  |         $this->formatter = $formatter ?? new Formatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 20 |  |     public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 20 |  |         if (!$rule instanceof Count) { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 1 |  |             throw new UnexpectedRuleException(Count::class, $rule); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 19 |  |         $result = new Result(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 19 |  |         if (!is_countable($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             $formattedMessage = $this->formatter->format( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 $rule->getMessage(), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 ['attribute' => $context?->getAttribute(), 'value' => $value] | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $result->addError($formattedMessage); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 19 |  |         $count = count($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 19 |  |         if ($rule->getExactly() !== null && $count !== $rule->getExactly()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             $formattedMessage = $this->formatter->format( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |                 $rule->getNotExactlyMessage(), | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |                 ['exactly' => $rule->getExactly(), 'attribute' => $context?->getAttribute(), 'value' => $value] | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |             $result->addError($formattedMessage); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |             return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 18 |  |         if ($rule->getMin() !== null && $count < $rule->getMin()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 9 |  |             $formattedMessage = $this->formatter->format( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 9 |  |                 $rule->getTooFewItemsMessage(), | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 9 |  |                 ['min' => $rule->getMin(), 'attribute' => $context?->getAttribute(), 'value' => $value] | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 9 |  |             $result->addError($formattedMessage); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 18 |  |         if ($rule->getMax() !== null && $count > $rule->getMax()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 2 |  |             $formattedMessage = $this->formatter->format( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 2 |  |                 $rule->getTooManyItemsMessage(), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 2 |  |                 ['max' => $rule->getMax(), 'attribute' => $context?->getAttribute(), 'value' => $value] | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 2 |  |             $result->addError($formattedMessage); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 18 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |