Total Complexity | 4 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class MaxWords implements Rule |
||
8 | { |
||
9 | private int $max; |
||
10 | |||
11 | /** |
||
12 | * Create a new rule instance. |
||
13 | * |
||
14 | * @param integer $max |
||
15 | */ |
||
16 | public function __construct(int $max) |
||
17 | { |
||
18 | $this->max = $max; |
||
19 | } |
||
20 | |||
21 | /** |
||
22 | * Helper method to initialize the rule. |
||
23 | * |
||
24 | * @param integer $max |
||
25 | * @return self |
||
26 | */ |
||
27 | public static function make(int $max): self |
||
28 | { |
||
29 | return new static($max); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Determine if the validation rule passes. |
||
34 | * |
||
35 | * @param string $attribute |
||
36 | * @param mixed $value |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function passes($attribute, $value) |
||
40 | { |
||
41 | return count(array_filter(explode(' ', $value))) <= $this->max; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Get the validation error message. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function message() |
||
52 | } |
||
53 | } |
||
54 |