|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Filter |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Boolean.php 22662 2010-07-24 17:37:36Z mabe $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Filter_Interface |
|
24 */ |
|
25 require_once 'Zend/Filter/Interface.php'; |
|
26 |
|
27 /** |
|
28 * @category Zend |
|
29 * @package Zend_Filter |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 class Zend_Filter_Boolean implements Zend_Filter_Interface |
|
34 { |
|
35 const BOOLEAN = 1; |
|
36 const INTEGER = 2; |
|
37 const FLOAT = 4; |
|
38 const STRING = 8; |
|
39 const ZERO = 16; |
|
40 const EMPTY_ARRAY = 32; |
|
41 const NULL = 64; |
|
42 const PHP = 127; |
|
43 const FALSE_STRING = 128; |
|
44 const YES = 256; |
|
45 const ALL = 511; |
|
46 |
|
47 protected $_constants = array( |
|
48 self::BOOLEAN => 'boolean', |
|
49 self::INTEGER => 'integer', |
|
50 self::FLOAT => 'float', |
|
51 self::STRING => 'string', |
|
52 self::ZERO => 'zero', |
|
53 self::EMPTY_ARRAY => 'array', |
|
54 self::NULL => 'null', |
|
55 self::PHP => 'php', |
|
56 self::FALSE_STRING => 'false', |
|
57 self::YES => 'yes', |
|
58 self::ALL => 'all', |
|
59 ); |
|
60 |
|
61 /** |
|
62 * Internal type to detect |
|
63 * |
|
64 * @var integer |
|
65 */ |
|
66 protected $_type = self::PHP; |
|
67 |
|
68 /** |
|
69 * Internal locale |
|
70 * |
|
71 * @var array |
|
72 */ |
|
73 protected $_locale = array('auto'); |
|
74 |
|
75 /** |
|
76 * Internal mode |
|
77 * |
|
78 * @var boolean |
|
79 */ |
|
80 protected $_casting = true; |
|
81 |
|
82 /** |
|
83 * Constructor |
|
84 * |
|
85 * @param string|array|Zend_Config $options OPTIONAL |
|
86 */ |
|
87 public function __construct($options = null) |
|
88 { |
|
89 if ($options instanceof Zend_Config) { |
|
90 $options = $options->toArray(); |
|
91 } elseif (!is_array($options)) { |
|
92 $options = func_get_args(); |
|
93 $temp = array(); |
|
94 if (!empty($options)) { |
|
95 $temp['type'] = array_shift($options); |
|
96 } |
|
97 |
|
98 if (!empty($options)) { |
|
99 $temp['casting'] = array_shift($options); |
|
100 } |
|
101 |
|
102 if (!empty($options)) { |
|
103 $temp['locale'] = array_shift($options); |
|
104 } |
|
105 |
|
106 $options = $temp; |
|
107 } |
|
108 |
|
109 if (array_key_exists('type', $options)) { |
|
110 $this->setType($options['type']); |
|
111 } |
|
112 |
|
113 if (array_key_exists('casting', $options)) { |
|
114 $this->setCasting($options['casting']); |
|
115 } |
|
116 |
|
117 if (array_key_exists('locale', $options)) { |
|
118 $this->setLocale($options['locale']); |
|
119 } |
|
120 } |
|
121 |
|
122 /** |
|
123 * Returns the set null types |
|
124 * |
|
125 * @return int |
|
126 */ |
|
127 public function getType() |
|
128 { |
|
129 return $this->_type; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Set the null types |
|
134 * |
|
135 * @param integer|array $type |
|
136 * @throws Zend_Filter_Exception |
|
137 * @return Zend_Filter_Boolean |
|
138 */ |
|
139 public function setType($type = null) |
|
140 { |
|
141 if (is_array($type)) { |
|
142 $detected = 0; |
|
143 foreach($type as $value) { |
|
144 if (is_int($value)) { |
|
145 $detected += $value; |
|
146 } elseif (in_array($value, $this->_constants)) { |
|
147 $detected += array_search($value, $this->_constants); |
|
148 } |
|
149 } |
|
150 |
|
151 $type = $detected; |
|
152 } elseif (is_string($type) && in_array($type, $this->_constants)) { |
|
153 $type = array_search($type, $this->_constants); |
|
154 } |
|
155 |
|
156 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { |
|
157 require_once 'Zend/Filter/Exception.php'; |
|
158 throw new Zend_Filter_Exception('Unknown type'); |
|
159 } |
|
160 |
|
161 $this->_type = $type; |
|
162 return $this; |
|
163 } |
|
164 |
|
165 /** |
|
166 * Returns the set locale |
|
167 * |
|
168 * @return array |
|
169 */ |
|
170 public function getLocale() |
|
171 { |
|
172 return $this->_locale; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Set the locales which are accepted |
|
177 * |
|
178 * @param string|array|Zend_Locale $locale |
|
179 * @throws Zend_Filter_Exception |
|
180 * @return Zend_Filter_Boolean |
|
181 */ |
|
182 public function setLocale($locale = null) |
|
183 { |
|
184 if (is_string($locale)) { |
|
185 $locale = array($locale); |
|
186 } elseif ($locale instanceof Zend_Locale) { |
|
187 $locale = array($locale->toString()); |
|
188 } elseif (!is_array($locale)) { |
|
189 require_once 'Zend/Filter/Exception.php'; |
|
190 throw new Zend_Filter_Exception('Locale has to be string, array or an instance of Zend_Locale'); |
|
191 } |
|
192 |
|
193 require_once 'Zend/Locale.php'; |
|
194 foreach ($locale as $single) { |
|
195 if (!Zend_Locale::isLocale($single)) { |
|
196 require_once 'Zend/Filter/Exception.php'; |
|
197 throw new Zend_Filter_Exception("Unknown locale '$single'"); |
|
198 } |
|
199 } |
|
200 |
|
201 $this->_locale = $locale; |
|
202 return $this; |
|
203 } |
|
204 |
|
205 /** |
|
206 * Returns the casting option |
|
207 * |
|
208 * @return boolean |
|
209 */ |
|
210 public function getCasting() |
|
211 { |
|
212 return $this->_casting; |
|
213 } |
|
214 |
|
215 /** |
|
216 * Set the working mode |
|
217 * |
|
218 * @param boolean $locale When true this filter works like cast |
|
219 * When false it recognises only true and false |
|
220 * and all other values are returned as is |
|
221 * @throws Zend_Filter_Exception |
|
222 * @return Zend_Filter_Boolean |
|
223 */ |
|
224 public function setCasting($casting = true) |
|
225 { |
|
226 $this->_casting = (boolean) $casting; |
|
227 return $this; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Defined by Zend_Filter_Interface |
|
232 * |
|
233 * Returns a boolean representation of $value |
|
234 * |
|
235 * @param string $value |
|
236 * @return string |
|
237 */ |
|
238 public function filter($value) |
|
239 { |
|
240 $type = $this->getType(); |
|
241 $casting = $this->getCasting(); |
|
242 |
|
243 // STRING YES (Localized) |
|
244 if ($type >= self::YES) { |
|
245 $type -= self::YES; |
|
246 if (is_string($value)) { |
|
247 require_once 'Zend/Locale.php'; |
|
248 $locales = $this->getLocale(); |
|
249 foreach ($locales as $locale) { |
|
250 if ($this->_getLocalizedQuestion($value, false, $locale) === false) { |
|
251 return false; |
|
252 } |
|
253 |
|
254 if (!$casting && ($this->_getLocalizedQuestion($value, true, $locale) === true)) { |
|
255 return true; |
|
256 } |
|
257 } |
|
258 } |
|
259 } |
|
260 |
|
261 // STRING FALSE ('false') |
|
262 if ($type >= self::FALSE_STRING) { |
|
263 $type -= self::FALSE_STRING; |
|
264 if (is_string($value) && (strtolower($value) == 'false')) { |
|
265 return false; |
|
266 } |
|
267 |
|
268 if ((!$casting) && is_string($value) && (strtolower($value) == 'true')) { |
|
269 return true; |
|
270 } |
|
271 } |
|
272 |
|
273 // NULL (null) |
|
274 if ($type >= self::NULL) { |
|
275 $type -= self::NULL; |
|
276 if ($value === null) { |
|
277 return false; |
|
278 } |
|
279 } |
|
280 |
|
281 // EMPTY_ARRAY (array()) |
|
282 if ($type >= self::EMPTY_ARRAY) { |
|
283 $type -= self::EMPTY_ARRAY; |
|
284 if (is_array($value) && ($value == array())) { |
|
285 return false; |
|
286 } |
|
287 } |
|
288 |
|
289 // ZERO ('0') |
|
290 if ($type >= self::ZERO) { |
|
291 $type -= self::ZERO; |
|
292 if (is_string($value) && ($value == '0')) { |
|
293 return false; |
|
294 } |
|
295 |
|
296 if ((!$casting) && (is_string($value)) && ($value == '1')) { |
|
297 return true; |
|
298 } |
|
299 } |
|
300 |
|
301 // STRING ('') |
|
302 if ($type >= self::STRING) { |
|
303 $type -= self::STRING; |
|
304 if (is_string($value) && ($value == '')) { |
|
305 return false; |
|
306 } |
|
307 } |
|
308 |
|
309 // FLOAT (0.0) |
|
310 if ($type >= self::FLOAT) { |
|
311 $type -= self::FLOAT; |
|
312 if (is_float($value) && ($value == 0.0)) { |
|
313 return false; |
|
314 } |
|
315 |
|
316 if ((!$casting) && is_float($value) && ($value == 1.0)) { |
|
317 return true; |
|
318 } |
|
319 } |
|
320 |
|
321 // INTEGER (0) |
|
322 if ($type >= self::INTEGER) { |
|
323 $type -= self::INTEGER; |
|
324 if (is_int($value) && ($value == 0)) { |
|
325 return false; |
|
326 } |
|
327 |
|
328 if ((!$casting) && is_int($value) && ($value == 1)) { |
|
329 return true; |
|
330 } |
|
331 } |
|
332 |
|
333 // BOOLEAN (false) |
|
334 if ($type >= self::BOOLEAN) { |
|
335 $type -= self::BOOLEAN; |
|
336 if (is_bool($value)) { |
|
337 return $value; |
|
338 } |
|
339 } |
|
340 |
|
341 if ($casting) { |
|
342 return true; |
|
343 } |
|
344 |
|
345 return $value; |
|
346 } |
|
347 |
|
348 /** |
|
349 * Determine the value of a localized string, and compare it to a given value |
|
350 * |
|
351 * @param string $value |
|
352 * @param boolean $yes |
|
353 * @param array $locale |
|
354 * @return boolean |
|
355 */ |
|
356 protected function _getLocalizedQuestion($value, $yes, $locale) |
|
357 { |
|
358 if ($yes == true) { |
|
359 $question = 'yes'; |
|
360 $return = true; |
|
361 } else { |
|
362 $question = 'no'; |
|
363 $return = false; |
|
364 } |
|
365 $str = Zend_Locale::getTranslation($question, 'question', $locale); |
|
366 $str = explode(':', $str); |
|
367 if (!empty($str)) { |
|
368 foreach($str as $no) { |
|
369 if (($no == $value) || (strtolower($no) == strtolower($value))) { |
|
370 return $return; |
|
371 } |
|
372 } |
|
373 } |
|
374 } |
|
375 } |