1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines the DominionEnterprises\Filter\Booleans class. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace DominionEnterprises\Filter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A collection of filters for booleans. |
10
|
|
|
*/ |
11
|
|
|
final class Booleans |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Filters $value to a boolean strictly. |
15
|
|
|
* |
16
|
|
|
* $value must be a bool or 'true' or 'false' disregarding case and whitespace. |
17
|
|
|
* |
18
|
|
|
* The return value is the bool, as expected by the \DominionEnterprises\Filterer class. |
19
|
|
|
* |
20
|
|
|
* @param string|bool $value the value to filter to a boolean |
21
|
|
|
* @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is NULL |
22
|
|
|
* @param array $trueValues Array of values which represent the boolean true value. Values should be lowercased |
23
|
|
|
* @param array $falseValues Array of values which represent the boolean false value. Values should be lowercased |
24
|
|
|
* |
25
|
|
|
* @return bool|null the filtered $value |
26
|
|
|
* |
27
|
|
|
* @throws \InvalidArgumentException if $allowNull is not a boolean |
28
|
|
|
* @throws \Exception if $value is not a string |
29
|
|
|
* @throws \Exception if $value is not 'true' or 'false' disregarding case and whitespace |
30
|
|
|
*/ |
31
|
|
|
public static function filter( |
32
|
|
|
$value, |
33
|
|
|
$allowNull = false, |
34
|
|
|
array $trueValues = ['true'], |
35
|
|
|
array $falseValues = ['false'] |
36
|
|
|
) { |
37
|
|
|
if ($allowNull !== false && $allowNull !== true) { |
38
|
|
|
throw new \InvalidArgumentException('$allowNull was not a bool'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($allowNull === true && $value === null) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (is_bool($value)) { |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!is_string($value)) { |
50
|
|
|
throw new \Exception('"' . var_export($value, true) . '" $value is not a string'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$value = trim($value); |
54
|
|
|
|
55
|
|
|
$value = strtolower($value); |
56
|
|
|
|
57
|
|
|
if (in_array($value, $trueValues, true)) { |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (in_array($value, $falseValues, true)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
throw new \Exception( |
66
|
|
|
sprintf( |
67
|
|
|
"%s is not '%s' disregarding case and whitespace", |
68
|
|
|
$value, |
69
|
|
|
implode("' or '", array_merge($trueValues, $falseValues)) |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Filters the boolean $value to the given $true and $false cases |
76
|
|
|
* |
77
|
|
|
* @param boolean $value The boolean value to convert. |
78
|
|
|
* @param mixed $true The value to return on the true case. |
79
|
|
|
* @param mixed $false The value to return on the false case. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
* |
83
|
|
|
* @throws \Exception Thrown if $value is not a boolean |
84
|
|
|
*/ |
85
|
|
|
public static function convert($value, $true = 'true', $false = 'false') |
86
|
|
|
{ |
87
|
|
|
if ($value !== false && $value !== true) { |
88
|
|
|
throw new \Exception('$value was not a bool'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $value ? $true : $false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|