Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| MinMaxTester | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMin | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getMax | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMax | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| setMinMax | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| testValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | declare(strict_types=1); |
| 7 | |
| 8 | namespace pvc\validator\val_tester\min_max; |
| 9 | |
| 10 | use pvc\interfaces\validator\ValTesterInterface; |
| 11 | use pvc\validator\err\SetMaxException; |
| 12 | use pvc\validator\err\SetMinException; |
| 13 | |
| 14 | /** |
| 15 | * Class MinMaxTester |
| 16 | * @template DataType |
| 17 | * @implements ValTesterInterface<DataType> |
| 18 | */ |
| 19 | abstract class MinMaxTester implements ValTesterInterface |
| 20 | { |
| 21 | /** |
| 22 | * @var DataType |
| 23 | */ |
| 24 | protected $min; |
| 25 | |
| 26 | /** |
| 27 | * @var DataType |
| 28 | */ |
| 29 | protected $max; |
| 30 | |
| 31 | public function __construct(mixed $min, mixed $max) |
| 32 | { |
| 33 | $this->setMinMax($min, $max); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return DataType|null |
| 38 | */ |
| 39 | public function getMin(): mixed |
| 40 | { |
| 41 | return $this->min; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param DataType $min |
| 46 | * @throws SetMinException |
| 47 | */ |
| 48 | public function setMin(mixed $min): void |
| 49 | { |
| 50 | /** |
| 51 | * $min cannot be null |
| 52 | */ |
| 53 | if (is_null($min)) { |
| 54 | throw new SetMinException(); |
| 55 | } |
| 56 | /** |
| 57 | * if max is set, min must be less than max |
| 58 | */ |
| 59 | if (isset($this->max) && $min > $this->max) { |
| 60 | throw new SetMinException(); |
| 61 | } |
| 62 | $this->min = $min; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return DataType|null |
| 67 | */ |
| 68 | public function getMax(): mixed |
| 69 | { |
| 70 | return $this->max; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param DataType $max |
| 75 | * @throws SetMaxException |
| 76 | */ |
| 77 | public function setMax($max): void |
| 78 | { |
| 79 | if (is_null($max)) { |
| 80 | throw new SetMaxException(); |
| 81 | } |
| 82 | if (isset($this->min) && ($max < $this->min)) { |
| 83 | throw new SetMaxException(); |
| 84 | } |
| 85 | $this->max = $max; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * setMinMax |
| 90 | * @param DataType $min |
| 91 | * @param DataType $max |
| 92 | * @throws SetMaxException |
| 93 | * @throws SetMinException |
| 94 | */ |
| 95 | public function setMinMax($min, $max): void |
| 96 | { |
| 97 | $this->setMin($min); |
| 98 | $this->setMax($max); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * validate |
| 103 | * @param DataType $value |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function testValue($value): bool |
| 107 | { |
| 108 | return ($value >= $this->getMin()) && ($value <= $this->getMax()); |
| 109 | } |
| 110 | } |