Completed
Push — master ( 813469...d9bcbf )
by Travis
02:04
created

FloatBase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A max() 0 5 1
A min() 0 5 1
A validation() 0 18 5
A between() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards\Bases;
5
6
trait FloatBase
7
{
8
    /**
9
     * @var float
10
     */
11
    private $min = PHP_FLOAT_MIN;
12
13
    /**
14
     * @var float
15
     */
16
    private $max = PHP_FLOAT_MAX;
17
18
    public function between(float $min, float $max): self
19
    {
20
        $this->min = $min;
21
        $this->max = $max;
22
23
        return $this;
24
    }
25
26
    public function min(float $min): self
27
    {
28
        $this->min = $min;
29
30
        return $this;
31
    }
32
33
    public function max(float $max): self
34
    {
35
        $this->max = $max;
36
37
        return $this;
38
    }
39
40
    protected function validation($input, &$value): bool
41
    {
42
        if (\is_bool($input)) {
43
            return false;
44
        }
45
46
        $return = filter_var($input, FILTER_VALIDATE_FLOAT);
47
        if ($return === false) {
48
            return false;
49
        }
50
51
        if ($return < $this->min || $return > $this->max) {
52
            return false;
53
        }
54
55
        $value = $return;
56
57
        return true;
58
    }
59
}
60