1 | <?php |
||
33 | class Boolean extends Object |
||
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() |
|
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() |
||
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) |
||
159 | |||
160 | /** |
||
161 | * This method checks if the passed object is equal |
||
162 | * to itself. |
||
163 | * |
||
164 | * @param \AppserverIo\Lang\Object $obj The object to check |
||
165 | * |
||
166 | * @return boolean Returns TRUE if the passed object is equal |
||
167 | * @see \AppserverIo\Lang\Object::equals() |
||
168 | */ |
||
169 | public function equals(Object $obj) |
||
173 | |||
174 | /** |
||
175 | * This object as String returned. |
||
176 | * |
||
177 | * @return \AppserverIo\Lang\Strng The value as String. |
||
178 | */ |
||
179 | 1 | public function toString() |
|
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\Object::__toString() |
||
190 | */ |
||
191 | 11 | public function __toString() |
|
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() |
|
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) |
|
225 | } |
||
226 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: