1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Lang\Boolean |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/lang |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Lang; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class implements functionality to handle |
25
|
|
|
* a boolean value as object. |
26
|
|
|
* |
27
|
|
|
* @author Tim Wagner <[email protected]> |
28
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
29
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
30
|
|
|
* @link https://github.com/appserver-io/lang |
31
|
|
|
* @link http://www.appserver.io |
32
|
|
|
*/ |
33
|
|
|
class Boolean extends Objct |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The value of the Boolean. |
38
|
|
|
* |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
protected $value; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The accepted values for a Boolean object. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $booleans; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The boolean representation of |
52
|
|
|
* the requested value. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $values; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructs a newly allocated Boolean object that |
60
|
|
|
* represents the primitive boolean argument. |
61
|
|
|
* |
62
|
|
|
* @param boolean $value The value to be represented by the Boolean. |
63
|
|
|
* |
64
|
|
|
* @throws \AppserverIo\Lang\ClassCastException The passed value is not a valid boolean representation |
65
|
|
|
*/ |
66
|
13 |
|
public function __construct($value) |
67
|
|
|
{ |
68
|
|
|
// initialize property default values here, as declarative default values may break thread safety, |
69
|
|
|
// when utilizing static and non-static access on class methods within same thread context! |
70
|
13 |
|
$this->value = false; |
71
|
13 |
|
$this->booleans = array( |
72
|
|
|
true, |
73
|
|
|
false, |
74
|
|
|
1, |
75
|
|
|
0, |
76
|
|
|
"1", |
77
|
|
|
"0", |
78
|
|
|
"true", |
79
|
|
|
"false", |
80
|
|
|
"on", |
81
|
|
|
"off", |
82
|
|
|
"yes", |
83
|
|
|
"no", |
84
|
|
|
"y", |
85
|
|
|
"n" |
86
|
|
|
); |
87
|
13 |
|
$this->values = array( |
88
|
|
|
true => true, |
89
|
|
|
false => false, |
90
|
|
|
1 => true, |
91
|
|
|
0 => false, |
92
|
|
|
"1" => true, |
93
|
|
|
"0" => false, |
94
|
|
|
"true" => true, |
95
|
|
|
"false" => false, |
96
|
|
|
"on" => true, |
97
|
|
|
"off" => false, |
98
|
|
|
"yes" => true, |
99
|
|
|
"no" => false, |
100
|
|
|
"y" => true, |
101
|
|
|
"n" => false |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
|
105
|
13 |
|
if (in_array($value, $this->booleans, true)) { |
106
|
12 |
|
$this->value = $this->values[$value]; |
107
|
|
|
} else { |
108
|
1 |
|
throw new ClassCastException('The passed value ' . $value . ' is not a valid Boolean'); |
109
|
|
|
} |
110
|
12 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* This method returns the class name as |
114
|
|
|
* a string. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public static function __getClass() |
119
|
|
|
{ |
120
|
1 |
|
return __CLASS__; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the value of this Boolean object as a |
125
|
|
|
* boolean primitive. |
126
|
|
|
* |
127
|
|
|
* @return boolean Holds the value as a boolean primitive |
128
|
|
|
*/ |
129
|
|
|
public function booleanValue() |
130
|
|
|
{ |
131
|
|
|
return $this->value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a Boolean with a value represented by the specified String. |
136
|
|
|
* |
137
|
|
|
* If the passed string has the primitive value TRUE or 1 then the |
138
|
|
|
* returned object is initialized with the primitive value TRUE else |
139
|
|
|
* with FALSE. |
140
|
|
|
* |
141
|
|
|
* @param \AppserverIo\Lang\Strng $string Holds the String object to get the Boolean representation for |
142
|
|
|
* |
143
|
|
|
* @return \AppserverIo\Lang\Boolean The Boolean object representing the specified String. |
144
|
|
|
*/ |
145
|
|
|
public static function valueOf(Strng $string) |
146
|
|
|
{ |
147
|
|
|
// if the passed value is "true" or "1" then return a new Boolean |
148
|
|
|
// object initialized with true |
149
|
|
|
if ($string->equals(new Strng("1")) || |
150
|
|
|
$string->equals(new Strng("true")) || |
151
|
|
|
$string->equals(new Strng("yes")) || |
152
|
|
|
$string->equals(new Strng("on")) || |
153
|
|
|
$string->equals(new Strng("y"))) { |
154
|
|
|
return new Boolean(true); |
155
|
|
|
} |
156
|
|
|
// else return a new Boolean object initialized with false |
157
|
|
|
return new Boolean(false); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This method checks if the passed object is equal |
162
|
|
|
* to itself. |
163
|
|
|
* |
164
|
|
|
* @param \AppserverIo\Lang\Objct $obj The object to check |
165
|
|
|
* |
166
|
|
|
* @return boolean Returns TRUE if the passed object is equal |
167
|
|
|
* @see \AppserverIo\Lang\Objct::equals() |
168
|
|
|
*/ |
169
|
|
|
public function equals(Objct $obj) |
170
|
|
|
{ |
171
|
|
|
return $this->booleanValue() == $obj->booleanValue(); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* This object as String returned. |
176
|
|
|
* |
177
|
|
|
* @return \AppserverIo\Lang\Strng The value as String. |
178
|
|
|
*/ |
179
|
1 |
|
public function toString() |
180
|
|
|
{ |
181
|
1 |
|
return new Strng($this->__toString()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* This method returns the class as |
186
|
|
|
* a string representation. |
187
|
|
|
* |
188
|
|
|
* @return string The objects string representation |
189
|
|
|
* @see \AppserverIo\Lang\Objct::__toString() |
190
|
|
|
*/ |
191
|
11 |
|
public function __toString() |
192
|
|
|
{ |
193
|
|
|
// if TRUE, return string 'true' |
194
|
11 |
|
if ($this->value) { |
195
|
6 |
|
return 'true'; |
196
|
|
|
} |
197
|
|
|
// else return string 'false' |
198
|
5 |
|
return 'false'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* This method has to be called to serialize the Boolean. |
203
|
|
|
* |
204
|
|
|
* @return string Returns a serialized version of the Boolean |
205
|
|
|
* @see \Serializable::serialize() |
206
|
|
|
*/ |
207
|
1 |
|
public function serialize() |
208
|
|
|
{ |
209
|
1 |
|
return serialize($this->value); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* This method unserializes the passed string and initializes the Boolean |
214
|
|
|
* itself with the data. |
215
|
|
|
* |
216
|
|
|
* @param string $data Holds the data of the instance as serialized string |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
* @see \Serializable::unserialize($data) |
220
|
|
|
*/ |
221
|
1 |
|
public function unserialize($data) |
222
|
|
|
{ |
223
|
1 |
|
$this->value = unserialize($data); |
224
|
1 |
|
} |
225
|
|
|
} |
226
|
|
|
|