1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines the DominionEnterprises\Filter\Floats class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace DominionEnterprises\Filter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A collection of filters for floats. |
10
|
|
|
*/ |
11
|
|
|
final class Floats |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Filters $value to a float strictly. |
15
|
|
|
* |
16
|
|
|
* The return value is the float, as expected by the \DominionEnterprises\Filterer class. |
17
|
|
|
* |
18
|
|
|
* @param string|float $value the value to filter to a float |
19
|
|
|
* @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is NULL |
20
|
|
|
* @param int $minValue The minimum acceptable value |
21
|
|
|
* @param int $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
|
|
|
* @see is_numeric |
27
|
|
|
* @throws \InvalidArgumentException if $allowNull is not a boolean |
28
|
|
|
* @throws \InvalidArgumentException if $minValue is not null and not a float |
29
|
|
|
* @throws \InvalidArgumentException if $maxValue is not null and not a float |
30
|
|
|
* @throws \InvalidArgumentException if $castInts is not a boolean |
31
|
|
|
* @throws \Exception if $value does not pass is_numeric |
32
|
|
|
* @throws \Exception if $value is hex format |
33
|
|
|
* @throws \Exception if $value is not a string or float |
34
|
|
|
* @throws \Exception if $value overflow or underflows |
35
|
|
|
* @throws \Exception if $value is less than $minValue |
36
|
|
|
* @throws \Exception if $value is greater than $maxValue |
37
|
|
|
*/ |
38
|
|
|
public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false) |
39
|
|
|
{ |
40
|
|
View Code Duplication |
if ($allowNull !== false && $allowNull !== true) { |
|
|
|
|
41
|
|
|
throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
if ($minValue !== null && !is_float($minValue)) { |
|
|
|
|
45
|
|
|
throw new \InvalidArgumentException('"' . var_export($minValue, true) . '" $minValue was not a float'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if ($maxValue !== null && !is_float($maxValue)) { |
|
|
|
|
49
|
|
|
throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not a float'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
if ($castInts !== false && $castInts !== true) { |
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException('"' . var_export($castInts, true) . '" $castInts was not a bool'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($allowNull === true && $value === null) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$valueFloat = null; |
|
|
|
|
61
|
|
|
if (is_float($value)) { |
62
|
|
|
$valueFloat = $value; |
63
|
|
|
} elseif (is_int($value) && $castInts) { |
64
|
|
|
$valueFloat = (float)$value; |
65
|
|
|
} elseif (is_string($value)) { |
66
|
|
|
$value = trim($value); |
67
|
|
|
|
68
|
|
|
if (!is_numeric($value)) { |
69
|
|
|
throw new \Exception("{$value} does not pass is_numeric"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$value = strtolower($value); |
73
|
|
|
|
74
|
|
|
//This is the only case (that we know of) where is_numeric does not return correctly castable float |
75
|
|
|
if (strpos($value, 'x') !== false) { |
76
|
|
|
throw new \Exception("{$value} is hex format"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$valueFloat = (float)$value; |
80
|
|
|
} else { |
81
|
|
|
throw new \Exception('"' . var_export($value, true) . '" $value is not a string'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_infinite($valueFloat)) { |
85
|
|
|
throw new \Exception("{$value} overflow"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($minValue !== null && $valueFloat < $minValue) { |
89
|
|
|
throw new \Exception("{$valueFloat} is less than {$minValue}"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($maxValue !== null && $valueFloat > $maxValue) { |
93
|
|
|
throw new \Exception("{$valueFloat} is greater than {$maxValue}"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $valueFloat; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.