tfettig01 /
InputGuard
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace InputGuard\Guards\Bases; |
||
| 5 | |||
| 6 | trait FloatBase |
||
| 7 | { |
||
| 8 | use Strict; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var float |
||
| 12 | */ |
||
| 13 | private $min = PHP_FLOAT_MIN; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var float |
||
| 17 | */ |
||
| 18 | private $max = PHP_FLOAT_MAX; |
||
| 19 | |||
| 20 | 5 | public function between(float $min, float $max): self |
|
| 21 | { |
||
| 22 | 5 | $this->min = $min; |
|
| 23 | 5 | $this->max = $max; |
|
| 24 | |||
| 25 | 5 | return $this; |
|
| 26 | } |
||
| 27 | |||
| 28 | 12 | public function min(float $min): self |
|
| 29 | { |
||
| 30 | 12 | $this->min = $min; |
|
| 31 | |||
| 32 | 12 | return $this; |
|
| 33 | } |
||
| 34 | |||
| 35 | 11 | public function max(float $max): self |
|
| 36 | { |
||
| 37 | 11 | $this->max = $max; |
|
| 38 | |||
| 39 | 11 | return $this; |
|
| 40 | } |
||
| 41 | |||
| 42 | 28 | protected function validation($input, &$value): bool |
|
| 43 | { |
||
| 44 | 28 | if (\is_bool($input)) { |
|
| 45 | 2 | return false; |
|
| 46 | } |
||
| 47 | |||
| 48 | 26 | if ($this->strict) { |
|
| 49 | 2 | $return = \is_float($input) ? $input : false; |
|
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 50 | } else { |
||
| 51 | 24 | $return = filter_var($input, FILTER_VALIDATE_FLOAT); |
|
| 52 | } |
||
| 53 | |||
| 54 | 26 | if ($return === false) { |
|
| 55 | 6 | return false; |
|
| 56 | } |
||
| 57 | |||
| 58 | 20 | if ($return < $this->min || $return > $this->max) { |
|
| 59 | 4 | return false; |
|
| 60 | } |
||
| 61 | |||
| 62 | 17 | $value = $return; |
|
| 63 | |||
| 64 | 17 | return true; |
|
| 65 | } |
||
| 66 | } |
||
| 67 |