|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A collection of filters for floats. |
|
7
|
|
|
*/ |
|
8
|
|
|
final class Floats |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Filters $value to a float strictly. |
|
12
|
|
|
* |
|
13
|
|
|
* The return value is the float, as expected by the \TraderInteractive\Filterer class. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string|float $value the value to filter to a float |
|
16
|
|
|
* @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is |
|
17
|
|
|
* NULL |
|
18
|
|
|
* @param float $minValue The minimum acceptable value |
|
19
|
|
|
* @param float $maxValue The maximum acceptable value |
|
20
|
|
|
* @param bool $castInts Flag to cast $value to float if it is an integer |
|
21
|
|
|
* |
|
22
|
|
|
* @return float|null The filtered value |
|
23
|
|
|
* |
|
24
|
|
|
* @throws Exception if $value is greater than $maxValue |
|
25
|
|
|
* @see is_numeric |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function filter( |
|
28
|
|
|
$value, |
|
29
|
|
|
bool $allowNull = false, |
|
30
|
|
|
float $minValue = null, |
|
31
|
|
|
float $maxValue = null, |
|
32
|
|
|
bool $castInts = false |
|
33
|
|
|
) { |
|
34
|
|
|
if ($allowNull === true && $value === null) { |
|
35
|
|
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$valueFloat = null; |
|
|
|
|
|
|
39
|
|
|
if (is_float($value)) { |
|
40
|
|
|
$valueFloat = $value; |
|
41
|
|
|
} elseif (is_int($value) && $castInts) { |
|
42
|
|
|
$valueFloat = (float)$value; |
|
43
|
|
|
} elseif (is_string($value)) { |
|
44
|
|
|
$value = trim($value); |
|
45
|
|
|
|
|
46
|
|
|
if (!is_numeric($value)) { |
|
47
|
|
|
throw new Exception("{$value} does not pass is_numeric"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$value = strtolower($value); |
|
51
|
|
|
|
|
52
|
|
|
//This is the only case (that we know of) where is_numeric does not return correctly cast-able float |
|
53
|
|
|
if (strpos($value, 'x') !== false) { |
|
54
|
|
|
throw new Exception("{$value} is hex format"); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$valueFloat = (float)$value; |
|
58
|
|
|
} else { |
|
59
|
|
|
throw new Exception('"' . var_export($value, true) . '" $value is not a string'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (is_infinite($valueFloat)) { |
|
63
|
|
|
throw new Exception("{$value} overflow"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($minValue !== null && $valueFloat < $minValue) { |
|
67
|
|
|
throw new Exception("{$valueFloat} is less than {$minValue}"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($maxValue !== null && $valueFloat > $maxValue) { |
|
71
|
|
|
throw new Exception("{$valueFloat} is greater than {$maxValue}"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $valueFloat; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.