1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils; |
6
|
|
|
|
7
|
|
|
class ConvertHelper_Bool |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array<mixed,bool> |
11
|
|
|
*/ |
12
|
|
|
protected static $booleanStrings = array( |
13
|
|
|
1 => true, |
14
|
|
|
0 => false, |
15
|
|
|
'true' => true, |
16
|
|
|
'false' => false, |
17
|
|
|
'yes' => true, |
18
|
|
|
'no' => false |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Converts a string, number or boolean value to a boolean value. |
23
|
|
|
* |
24
|
|
|
* @param mixed $string |
25
|
|
|
* @throws ConvertHelper_Exception |
26
|
|
|
* @return bool |
27
|
|
|
* |
28
|
|
|
* @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING |
29
|
|
|
*/ |
30
|
|
|
public static function fromString($string) : bool |
31
|
|
|
{ |
32
|
|
|
if($string === '' || $string === null || !is_scalar($string)) |
33
|
|
|
{ |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if(is_bool($string)) |
38
|
|
|
{ |
39
|
|
|
return $string; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if(array_key_exists($string, self::$booleanStrings)) |
43
|
|
|
{ |
44
|
|
|
return self::$booleanStrings[$string]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
throw new ConvertHelper_Exception( |
48
|
|
|
'Invalid string boolean representation', |
49
|
|
|
sprintf( |
50
|
|
|
'Cannot convert [%s] to a boolean value.', |
51
|
|
|
parseVariable($string)->enableType()->toString() |
52
|
|
|
), |
53
|
|
|
ConvertHelper::ERROR_INVALID_BOOLEAN_STRING |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts a boolean value to a string. Defaults to returning |
59
|
|
|
* 'true' or 'false', with the additional parameter it can also |
60
|
|
|
* return the 'yes' and 'no' variants. |
61
|
|
|
* |
62
|
|
|
* @param boolean|string $boolean |
63
|
|
|
* @param boolean $yesno |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @throws ConvertHelper_Exception |
67
|
|
|
* @see ConvertHelper::ERROR_INVALID_BOOLEAN_STRING |
68
|
|
|
*/ |
69
|
|
|
public static function toString($boolean, bool $yesno = false) : string |
70
|
|
|
{ |
71
|
|
|
// allow 'yes', 'true', 'no', 'false' string notations as well |
72
|
|
|
if(!is_bool($boolean)) { |
73
|
|
|
$boolean = self::fromString($boolean); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($boolean) { |
77
|
|
|
if ($yesno) { |
78
|
|
|
return 'yes'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return 'true'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($yesno) { |
85
|
|
|
return 'no'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return 'false'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Converts a strict boolean value to string. Compared to |
93
|
|
|
* {@see ConvertHelper_Bool::toString()}, this cannot |
94
|
|
|
* throw an exception. |
95
|
|
|
* |
96
|
|
|
* @param bool $boolean |
97
|
|
|
* @param bool $yesno |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public static function toStringStrict(bool $boolean, bool $yesno = false) : string |
101
|
|
|
{ |
102
|
|
|
if ($boolean) { |
103
|
|
|
if ($yesno) { |
104
|
|
|
return 'yes'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return 'true'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($yesno) { |
111
|
|
|
return 'no'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return 'false'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Checks if the specified string is a boolean value, which |
119
|
|
|
* includes string representations of boolean values, like |
120
|
|
|
* <code>yes</code> or <code>no</code>, and <code>true</code> |
121
|
|
|
* or <code>false</code>. |
122
|
|
|
* |
123
|
|
|
* @param mixed $value |
124
|
|
|
* @return boolean |
125
|
|
|
*/ |
126
|
|
|
public static function isBoolean($value) : bool |
127
|
|
|
{ |
128
|
|
|
if(is_bool($value)) { |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if(!is_scalar($value)) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return array_key_exists($value, self::$booleanStrings); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|