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