Failed Conditions
Pull Request — master (#1)
by Chad
02:32 queued 25s
created

Floats   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
C filter() 0 48 14
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for floats.
9
 */
10
final class Floats
11
{
12
    /**
13
     * Filters $value to a float strictly.
14
     *
15
     * The return value is the float, as expected by the \TraderInteractive\Filterer class.
16
     *
17
     * @param string|float $value     the value to filter to a float
18
     * @param bool         $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is
19
     *                                NULL
20
     * @param float        $minValue  The minimum acceptable value
21
     * @param float        $maxValue  The maximum acceptable value
22
     * @param bool         $castInts  Flag to cast $value to float if it is an integer
23
     *
24
     * @return float|null The filtered value
25
     *
26
     * @throws FilterException if $value is greater than $maxValue
27
     * @see is_numeric
28
     */
29
    public static function filter(
30
        $value,
31
        bool $allowNull = false,
32
        float $minValue = null,
33
        float $maxValue = null,
34
        bool $castInts = false
35
    ) {
36
        if ($allowNull === true && $value === null) {
0 ignored issues
show
introduced by
The condition $allowNull === true && $value === null can never be true.
Loading history...
37
            return null;
38
        }
39
40
        $valueFloat = null;
41
        if (is_float($value)) {
42
            $valueFloat = $value;
43
        } elseif (is_int($value) && $castInts) {
0 ignored issues
show
introduced by
The condition is_int($value) && $castInts can never be true.
Loading history...
44
            $valueFloat = (float)$value;
45
        } elseif (is_string($value)) {
0 ignored issues
show
introduced by
The condition is_string($value) can never be false.
Loading history...
46
            $value = trim($value);
47
48
            if (!is_numeric($value)) {
49
                throw new FilterException("{$value} does not pass is_numeric");
50
            }
51
52
            $value = strtolower($value);
53
54
            //This is the only case (that we know of) where is_numeric does not return correctly cast-able float
55
            if (strpos($value, 'x') !== false) {
56
                throw new FilterException("{$value} is hex format");
57
            }
58
59
            $valueFloat = (float)$value;
60
        } else {
61
            throw new FilterException('"' . var_export($value, true) . '" $value is not a string');
62
        }
63
64
        if (is_infinite($valueFloat)) {
65
            throw new FilterException("{$value} overflow");
66
        }
67
68
        if ($minValue !== null && $valueFloat < $minValue) {
69
            throw new FilterException("{$valueFloat} is less than {$minValue}");
70
        }
71
72
        if ($maxValue !== null && $valueFloat > $maxValue) {
73
            throw new FilterException("{$valueFloat} is greater than {$maxValue}");
74
        }
75
76
        return $valueFloat;
77
    }
78
}
79