Total Complexity | 9 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | class InputValidator |
||
9 | { |
||
10 | 24 | public function __construct(private StyleInterface $output) |
|
11 | { |
||
12 | 24 | } |
|
13 | |||
14 | 24 | public function validate(InputInterface $input): bool |
|
15 | { |
||
16 | 24 | $hasValidInput = true; |
|
17 | |||
18 | 24 | $sizes = (array) $input->getOption(BenchmarkCommand::SIZE_OPTION); |
|
19 | 24 | $iterations = $input->getOption(BenchmarkCommand::ITERATIONS_OPTION); |
|
20 | 24 | $seed = $input->getOption(BenchmarkCommand::SEED_OPTION); |
|
21 | |||
22 | 24 | foreach ($sizes as $size) { |
|
23 | 24 | if (!$this->isPositiveInteger($size)) { |
|
24 | 7 | $this->output->error('The size must be an integer greater than 0'); |
|
25 | 7 | $hasValidInput = false; |
|
26 | } |
||
27 | } |
||
28 | |||
29 | 24 | if (!$this->isPositiveInteger($iterations)) { |
|
30 | 8 | $this->output->error('The number of iterations must be an integer greater than 0'); |
|
31 | 8 | $hasValidInput = false; |
|
32 | } |
||
33 | |||
34 | 24 | if (!$this->isOptionalInteger($seed)) { |
|
35 | 8 | $this->output->error('The seed must be an integer'); |
|
36 | 8 | $hasValidInput = false; |
|
37 | } |
||
38 | |||
39 | 24 | return $hasValidInput; |
|
40 | } |
||
41 | |||
42 | 24 | private function isPositiveInteger(mixed $value): bool |
|
43 | { |
||
44 | 24 | $positiveInteger = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); |
|
45 | |||
46 | 24 | return $positiveInteger !== false; |
|
47 | } |
||
48 | |||
49 | 24 | private function isOptionalInteger(mixed $value): bool |
|
54 | } |
||
55 | } |
||
56 |