[ZF-10089] Zend_Log
[zend.git] / library / Zend / Filter / Boolean.php
blob0d784a38b25f4c7f77962d2c55095505147ffb7f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /**
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
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
33 class Zend_Filter_Boolean implements Zend_Filter_Interface
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;
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',
61 /**
62 * Internal type to detect
64 * @var integer
66 protected $_type = self::PHP;
68 /**
69 * Internal locale
71 * @var array
73 protected $_locale = array('auto');
75 /**
76 * Internal mode
78 * @var boolean
80 protected $_casting = true;
82 /**
83 * Constructor
85 * @param string|array|Zend_Config $options OPTIONAL
87 public function __construct($options = null)
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);
98 if (!empty($options)) {
99 $temp['casting'] = array_shift($options);
102 if (!empty($options)) {
103 $temp['locale'] = array_shift($options);
106 $options = $temp;
109 if (array_key_exists('type', $options)) {
110 $this->setType($options['type']);
113 if (array_key_exists('casting', $options)) {
114 $this->setCasting($options['casting']);
117 if (array_key_exists('locale', $options)) {
118 $this->setLocale($options['locale']);
123 * Returns the set null types
125 * @return int
127 public function getType()
129 return $this->_type;
133 * Set the null types
135 * @param integer|array $type
136 * @throws Zend_Filter_Exception
137 * @return Zend_Filter_Boolean
139 public function setType($type = null)
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);
151 $type = $detected;
152 } elseif (is_string($type) && in_array($type, $this->_constants)) {
153 $type = array_search($type, $this->_constants);
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');
161 $this->_type = $type;
162 return $this;
166 * Returns the set locale
168 * @return array
170 public function getLocale()
172 return $this->_locale;
176 * Set the locales which are accepted
178 * @param string|array|Zend_Locale $locale
179 * @throws Zend_Filter_Exception
180 * @return Zend_Filter_Boolean
182 public function setLocale($locale = null)
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');
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'");
201 $this->_locale = $locale;
202 return $this;
206 * Returns the casting option
208 * @return boolean
210 public function getCasting()
212 return $this->_casting;
216 * Set the working mode
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
224 public function setCasting($casting = true)
226 $this->_casting = (boolean) $casting;
227 return $this;
231 * Defined by Zend_Filter_Interface
233 * Returns a boolean representation of $value
235 * @param string $value
236 * @return string
238 public function filter($value)
240 $type = $this->getType();
241 $casting = $this->getCasting();
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;
254 if (!$casting && ($this->_getLocalizedQuestion($value, true, $locale) === true)) {
255 return true;
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;
268 if ((!$casting) && is_string($value) && (strtolower($value) == 'true')) {
269 return true;
273 // NULL (null)
274 if ($type >= self::NULL) {
275 $type -= self::NULL;
276 if (is_null($value)) {
277 return false;
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;
289 // ZERO ('0')
290 if ($type >= self::ZERO) {
291 $type -= self::ZERO;
292 if (is_string($value) && ($value == '0')) {
293 return false;
296 if ((!$casting) && (is_string($value)) && ($value == '1')) {
297 return true;
301 // STRING ('')
302 if ($type >= self::STRING) {
303 $type -= self::STRING;
304 if (is_string($value) && ($value == '')) {
305 return false;
309 // FLOAT (0.0)
310 if ($type >= self::FLOAT) {
311 $type -= self::FLOAT;
312 if (is_float($value) && ($value == 0.0)) {
313 return false;
316 if ((!$casting) && is_float($value) && ($value == 1.0)) {
317 return true;
321 // INTEGER (0)
322 if ($type >= self::INTEGER) {
323 $type -= self::INTEGER;
324 if (is_int($value) && ($value == 0)) {
325 return false;
328 if ((!$casting) && is_int($value) && ($value == 1)) {
329 return true;
333 // BOOLEAN (false)
334 if ($type >= self::BOOLEAN) {
335 $type -= self::BOOLEAN;
336 if (is_bool($value)) {
337 return $value;
341 if ($casting) {
342 return true;
345 return $value;
349 * Determine the value of a localized string, and compare it to a given value
351 * @param string $value
352 * @param boolean $yes
353 * @param array $locale
354 * @return boolean
356 protected function _getLocalizedQuestion($value, $yes, $locale)
358 if ($yes == true) {
359 $question = 'yes';
360 $return = true;
361 } else {
362 $question = 'no';
363 $return = false;
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;