Total Complexity | 17 |
Total Lines | 150 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class Prime extends Iterators |
||
12 | { |
||
13 | /** |
||
14 | * The maximum limit. |
||
15 | * |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $max; |
||
19 | |||
20 | /** |
||
21 | * The minimum limit. |
||
22 | * |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $min; |
||
26 | |||
27 | /** |
||
28 | * Prime constructor. |
||
29 | */ |
||
30 | 2 | public function __construct() |
|
31 | { |
||
32 | 2 | $this->setMaxLimit(PHP_INT_MAX); |
|
33 | 2 | $this->setMinLimit(0); |
|
34 | 2 | parent::__construct(); |
|
35 | 2 | } |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function current(): mixed |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get the maximum limit. |
||
55 | * |
||
56 | * @return int |
||
57 | * The limit |
||
58 | */ |
||
59 | 2 | public function getMaxLimit(): int |
|
60 | { |
||
61 | 2 | return (int) $this->max; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the minimum limit. |
||
66 | * |
||
67 | * @return int |
||
68 | * The limit |
||
69 | */ |
||
70 | 2 | public function getMinLimit(): int |
|
71 | { |
||
72 | 2 | return 2 >= $this->min ? 2 : (int) $this->min; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | 2 | public function next(): void |
|
81 | { |
||
82 | 2 | ++$this->key; |
|
83 | 2 | } |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 2 | public function rewind(): void |
|
89 | { |
||
90 | 2 | $this->key = $this->getMinLimit(); |
|
91 | 2 | } |
|
92 | |||
93 | /** |
||
94 | * Set the maximum limit. |
||
95 | * |
||
96 | * @param int $max |
||
97 | * The limit |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | 2 | public function setMaxLimit($max): void |
|
102 | { |
||
103 | 2 | $this->max = $max; |
|
104 | 2 | } |
|
105 | |||
106 | /** |
||
107 | * Set the minimum limit. |
||
108 | * |
||
109 | * @param int $min |
||
110 | * The limit |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | 2 | public function setMinLimit($min): void |
|
115 | { |
||
116 | 2 | $this->min = $min; |
|
117 | 2 | } |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | 2 | public function valid(): bool |
|
125 | { |
||
126 | 2 | return $this->current() < $this->getMaxLimit(); |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Test if a number is prime or not. |
||
131 | * |
||
132 | * @param int $number |
||
133 | * The number to test |
||
134 | * |
||
135 | * @return bool |
||
136 | * The true if the number is prime, false otherwise |
||
137 | */ |
||
138 | 2 | protected function isPrimeNumber($number): bool |
|
161 | } |
||
162 | } |
||
163 |