Total Complexity | 11 |
Total Lines | 77 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
9 | final class CountConstraint |
||
10 | { |
||
11 | /** |
||
12 | * @var int Minimum count. |
||
13 | */ |
||
14 | private $min; |
||
15 | |||
16 | /** |
||
17 | * @var int|null Maximum count. |
||
18 | */ |
||
19 | private $max; |
||
20 | |||
21 | /** |
||
22 | * @var int Constraint weight. |
||
23 | */ |
||
24 | private $weight; |
||
25 | |||
26 | /** |
||
27 | * @param int $min Minimum count. |
||
28 | * @param int|null $max Maximum count. |
||
29 | * @param int $weight Constraint weight. |
||
30 | */ |
||
31 | 8 | public function __construct(int $min = 1, ?int $max = null, int $weight = 1) |
|
32 | { |
||
33 | 8 | if ($min < 0) { |
|
34 | 1 | throw new InvalidArgumentException('Min cannot be negative.'); |
|
35 | } |
||
36 | 7 | if ($max !== null && $max < $min) { |
|
37 | 1 | throw new InvalidArgumentException('Max cannot be smaller than min.'); |
|
38 | } |
||
39 | |||
40 | 6 | $this->min = $min; |
|
41 | 6 | $this->max = $max; |
|
42 | 6 | $this->weight = $weight; |
|
43 | 6 | } |
|
44 | |||
45 | /** |
||
46 | * @return int Minimum count. |
||
47 | */ |
||
48 | 1 | public function getMin(): int |
|
49 | { |
||
50 | 1 | return $this->min; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return int|null Maximum count. |
||
55 | */ |
||
56 | 1 | public function getMax(): ?int |
|
57 | { |
||
58 | 1 | return $this->max; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return int Constraint weight. |
||
63 | */ |
||
64 | 1 | public function getWeight(): int |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Check whether the count is in compliance with the constraint. |
||
71 | * |
||
72 | * @param int $count Count to check. |
||
73 | * @return bool Whether the count is in compliance with the constraint. |
||
74 | */ |
||
75 | 4 | public function test(int $count): bool |
|
86 | } |
||
87 | } |
||
88 |