|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A collection of filters for integers. |
|
7
|
|
|
*/ |
|
8
|
|
|
final class Ints |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Filters $value to an integer strictly. |
|
12
|
|
|
* |
|
13
|
|
|
* $value must be an int or contain all digits, optionally prepended by a '+' or '-' and optionally surrounded by |
|
14
|
|
|
* whitespace to pass the filter. |
|
15
|
|
|
* |
|
16
|
|
|
* The return value is the int, as expected by the \TraderInteractive\Filterer class. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|int $value the value to make an integer |
|
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
|
|
|
* |
|
23
|
|
|
* @return int|null The filtered value |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \InvalidArgumentException if $allowNull is not a boolean |
|
26
|
|
|
* @throws \InvalidArgumentException if $minValue is not null and not an int |
|
27
|
|
|
* @throws \InvalidArgumentException if $maxValue is not an int |
|
28
|
|
|
* @throws Exception if $value string length is zero |
|
29
|
|
|
* @throws Exception if $value does not contain all digits, optionally prepended by a '+' or '-' and optionally |
|
30
|
|
|
* surrounded by whitespace |
|
31
|
|
|
* @throws Exception if $value was greater than a max int of PHP_INT_MAX |
|
32
|
|
|
* @throws Exception if $value was less than a min int of ~PHP_INT_MAX |
|
33
|
|
|
* @throws Exception if $value is not a string |
|
34
|
|
|
* @throws Exception if $value is less than $minValue |
|
35
|
|
|
* @throws Exception if $value is greater than $maxValue |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function filter($value, bool $allowNull = false, int $minValue = null, int $maxValue = PHP_INT_MAX) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($allowNull === true && $value === null) { |
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$valueInt = null; |
|
|
|
|
|
|
44
|
|
|
if (is_int($value)) { |
|
45
|
|
|
$valueInt = $value; |
|
46
|
|
|
} elseif (is_string($value)) { |
|
47
|
|
|
$value = trim($value); |
|
48
|
|
|
|
|
49
|
|
|
if (strlen($value) === 0) { |
|
50
|
|
|
throw new Exception('$value string length is zero'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$stringToCheckDigits = $value; |
|
54
|
|
|
|
|
55
|
|
|
if ($value[0] === '-' || $value[0] === '+') { |
|
56
|
|
|
$stringToCheckDigits = substr($value, 1); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!ctype_digit($stringToCheckDigits)) { |
|
60
|
|
|
throw new Exception( |
|
61
|
|
|
"{$value} does not contain all digits, optionally prepended by a '+' or '-' and optionally " |
|
62
|
|
|
. "surrounded by whitespace" |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$phpIntMin = ~PHP_INT_MAX; |
|
67
|
|
|
|
|
68
|
|
|
$casted = (int)$value; |
|
69
|
|
|
|
|
70
|
|
|
if ($casted === PHP_INT_MAX && $value !== (string)PHP_INT_MAX) { |
|
71
|
|
|
throw new Exception("{$value} was greater than a max int of " . PHP_INT_MAX); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($casted === $phpIntMin && $value !== (string)$phpIntMin) { |
|
75
|
|
|
throw new Exception("{$value} was less than a min int of {$phpIntMin}"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$valueInt = $casted; |
|
79
|
|
|
} else { |
|
80
|
|
|
throw new Exception('"' . var_export($value, true) . '" $value is not a string'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($minValue !== null && $valueInt < $minValue) { |
|
84
|
|
|
throw new Exception("{$valueInt} is less than {$minValue}"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($valueInt > $maxValue) { |
|
88
|
|
|
throw new Exception("{$valueInt} is greater than {$maxValue}"); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $valueInt; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
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.