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