| Total Complexity | 7 |
| Total Lines | 37 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | #[Attribute(Attribute::TARGET_PROPERTY)] |
||
| 12 | class CountAtMost extends AbstractRule |
||
| 13 | { |
||
| 14 | const MESSAGE = 'Expected count is at most: %s'; |
||
| 15 | |||
| 16 | public function __construct( |
||
| 17 | private int $size, |
||
| 18 | ?string $message = null, |
||
| 19 | int $stopOnFailure = StopSign::DONT_STOP |
||
| 20 | ) { |
||
| 21 | parent::__construct($message, $stopOnFailure); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function isValid($input, array $context = []): bool |
||
| 25 | { |
||
| 26 | if (is_countable($input)) { |
||
| 27 | return count($input) <= $this->size; |
||
| 28 | } |
||
| 29 | |||
| 30 | if (is_iterable($input)) { |
||
| 31 | $count = 0; |
||
| 32 | foreach ($input as $v) { |
||
| 33 | $count++; |
||
| 34 | if ($count > $this->size) { |
||
| 35 | return false; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | return $count <= $this->size; |
||
| 40 | } |
||
| 41 | |||
| 42 | return false; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function translatedMessage(): ?string |
||
| 48 | } |
||
| 49 | } |
||
| 50 |