|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Defines the DominionEnterprises\Filter\Strings class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace DominionEnterprises\Filter; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A collection of filters for strings. |
|
10
|
|
|
*/ |
|
11
|
|
|
final class Strings |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Filter a string. |
|
15
|
|
|
* |
|
16
|
|
|
* Verify that the passed in value is a string. By default, nulls are not allowed, and the length is restricted |
|
17
|
|
|
* between 1 and PHP_INT_MAX. These parameters can be overwritten for custom behavior. |
|
18
|
|
|
* |
|
19
|
|
|
* The return value is the string, as expected by the \DominionEnterprises\Filterer class. |
|
20
|
|
|
* |
|
21
|
|
|
* @param mixed $value The value to filter. |
|
22
|
|
|
* @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed. |
|
23
|
|
|
* @param int $minLength Minimum length to allow for $value. |
|
24
|
|
|
* @param int $maxLength Maximum length to allow for $value. |
|
25
|
|
|
* @return string|null The passed in $value. |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \Exception if the value did not pass validation. |
|
28
|
|
|
* @throws \InvalidArgumentException if one of the parameters was not correctly typed. |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function filter($value, $allowNull = false, $minLength = 1, $maxLength = PHP_INT_MAX) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($allowNull !== false && $allowNull !== true) { |
|
33
|
|
|
throw new \InvalidArgumentException('$allowNull was not a boolean value'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (!is_int($minLength) || $minLength < 0) { |
|
37
|
|
|
throw new \InvalidArgumentException('$minLength was not a positive integer value'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (!is_int($maxLength) || $maxLength < 0) { |
|
41
|
|
|
throw new \InvalidArgumentException('$maxLength was not a positive integer value'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ($allowNull === true && $value === null) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (is_scalar($value)) { |
|
49
|
|
|
$value = "{$value}"; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (is_object($value) && method_exists($value, '__toString')) { |
|
53
|
|
|
$value = (string)$value; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!is_string($value)) { |
|
57
|
|
|
throw new \Exception("Value '" . var_export($value, true) . "' is not a string"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$valueLength = strlen($value); |
|
61
|
|
|
|
|
62
|
|
|
if ($valueLength < $minLength || $valueLength > $maxLength) { |
|
63
|
|
|
throw new \Exception( |
|
64
|
|
|
sprintf( |
|
65
|
|
|
"Value '%s' with length '%d' is less than '%d' or greater than '%d'", |
|
66
|
|
|
$value, |
|
67
|
|
|
$valueLength, |
|
68
|
|
|
$minLength, |
|
69
|
|
|
$maxLength |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Explodes a string into an array using the given delimiter. |
|
79
|
|
|
* |
|
80
|
|
|
* For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz']. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $value The string to explode. |
|
83
|
|
|
* @param string $delimiter The non-empty delimiter to explode on. |
|
84
|
|
|
* @return array The exploded values. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws \Exception if the value is not a string. |
|
87
|
|
|
* @throws \InvalidArgumentException if the delimiter does not pass validation. |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function explode($value, $delimiter = ',') |
|
90
|
|
|
{ |
|
91
|
|
|
if (!is_string($value)) { |
|
92
|
|
|
throw new \Exception("Value '" . var_export($value, true) . "' is not a string"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!is_string($delimiter) || empty($delimiter)) { |
|
96
|
|
|
throw new \InvalidArgumentException( |
|
97
|
|
|
"Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string" |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return explode($delimiter, $value); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|