File indexing completed on 2024-05-12 06:02:32
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Filter 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Filter_Interface 0024 */ 0025 // require_once 'Zend/Filter/Interface.php'; 0026 0027 /** 0028 * @category Zend 0029 * @package Zend_Filter 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Filter_Boolean implements Zend_Filter_Interface 0034 { 0035 const BOOLEAN = 1; 0036 const INTEGER = 2; 0037 const FLOAT = 4; 0038 const STRING = 8; 0039 const ZERO = 16; 0040 const EMPTY_ARRAY = 32; 0041 const NULL = 64; 0042 const PHP = 127; 0043 const FALSE_STRING = 128; 0044 const YES = 256; 0045 const ALL = 511; 0046 0047 protected $_constants = array( 0048 self::BOOLEAN => 'boolean', 0049 self::INTEGER => 'integer', 0050 self::FLOAT => 'float', 0051 self::STRING => 'string', 0052 self::ZERO => 'zero', 0053 self::EMPTY_ARRAY => 'array', 0054 self::NULL => 'null', 0055 self::PHP => 'php', 0056 self::FALSE_STRING => 'false', 0057 self::YES => 'yes', 0058 self::ALL => 'all', 0059 ); 0060 0061 /** 0062 * Internal type to detect 0063 * 0064 * @var integer 0065 */ 0066 protected $_type = self::PHP; 0067 0068 /** 0069 * Internal locale 0070 * 0071 * @var array 0072 */ 0073 protected $_locale = array('auto'); 0074 0075 /** 0076 * Internal mode 0077 * 0078 * @var boolean 0079 */ 0080 protected $_casting = true; 0081 0082 /** 0083 * Constructor 0084 * 0085 * @param string|array|Zend_Config $options OPTIONAL 0086 */ 0087 public function __construct($options = null) 0088 { 0089 if ($options instanceof Zend_Config) { 0090 $options = $options->toArray(); 0091 } elseif (!is_array($options)) { 0092 $options = func_get_args(); 0093 $temp = array(); 0094 if (!empty($options)) { 0095 $temp['type'] = array_shift($options); 0096 } 0097 0098 if (!empty($options)) { 0099 $temp['casting'] = array_shift($options); 0100 } 0101 0102 if (!empty($options)) { 0103 $temp['locale'] = array_shift($options); 0104 } 0105 0106 $options = $temp; 0107 } 0108 0109 if (array_key_exists('type', $options)) { 0110 $this->setType($options['type']); 0111 } 0112 0113 if (array_key_exists('casting', $options)) { 0114 $this->setCasting($options['casting']); 0115 } 0116 0117 if (array_key_exists('locale', $options)) { 0118 $this->setLocale($options['locale']); 0119 } 0120 } 0121 0122 /** 0123 * Returns the set null types 0124 * 0125 * @return int 0126 */ 0127 public function getType() 0128 { 0129 return $this->_type; 0130 } 0131 0132 /** 0133 * Set the null types 0134 * 0135 * @param integer|array $type 0136 * @throws Zend_Filter_Exception 0137 * @return Zend_Filter_Boolean 0138 */ 0139 public function setType($type = null) 0140 { 0141 if (is_array($type)) { 0142 $detected = 0; 0143 foreach($type as $value) { 0144 if (is_int($value)) { 0145 $detected += $value; 0146 } elseif (in_array($value, $this->_constants)) { 0147 $detected += array_search($value, $this->_constants); 0148 } 0149 } 0150 0151 $type = $detected; 0152 } elseif (is_string($type) && in_array($type, $this->_constants)) { 0153 $type = array_search($type, $this->_constants); 0154 } 0155 0156 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { 0157 // require_once 'Zend/Filter/Exception.php'; 0158 throw new Zend_Filter_Exception('Unknown type'); 0159 } 0160 0161 $this->_type = $type; 0162 return $this; 0163 } 0164 0165 /** 0166 * Returns the set locale 0167 * 0168 * @return array 0169 */ 0170 public function getLocale() 0171 { 0172 return $this->_locale; 0173 } 0174 0175 /** 0176 * Set the locales which are accepted 0177 * 0178 * @param string|array|Zend_Locale $locale 0179 * @throws Zend_Filter_Exception 0180 * @return Zend_Filter_Boolean 0181 */ 0182 public function setLocale($locale = null) 0183 { 0184 if (is_string($locale)) { 0185 $locale = array($locale); 0186 } elseif ($locale instanceof Zend_Locale) { 0187 $locale = array($locale->toString()); 0188 } elseif (!is_array($locale)) { 0189 // require_once 'Zend/Filter/Exception.php'; 0190 throw new Zend_Filter_Exception('Locale has to be string, array or an instance of Zend_Locale'); 0191 } 0192 0193 // require_once 'Zend/Locale.php'; 0194 foreach ($locale as $single) { 0195 if (!Zend_Locale::isLocale($single)) { 0196 // require_once 'Zend/Filter/Exception.php'; 0197 throw new Zend_Filter_Exception("Unknown locale '$single'"); 0198 } 0199 } 0200 0201 $this->_locale = $locale; 0202 return $this; 0203 } 0204 0205 /** 0206 * Returns the casting option 0207 * 0208 * @return boolean 0209 */ 0210 public function getCasting() 0211 { 0212 return $this->_casting; 0213 } 0214 0215 /** 0216 * Set the working mode 0217 * 0218 * @param boolean $locale When true this filter works like cast 0219 * When false it recognises only true and false 0220 * and all other values are returned as is 0221 * @throws Zend_Filter_Exception 0222 * @return Zend_Filter_Boolean 0223 */ 0224 public function setCasting($casting = true) 0225 { 0226 $this->_casting = (boolean) $casting; 0227 return $this; 0228 } 0229 0230 /** 0231 * Defined by Zend_Filter_Interface 0232 * 0233 * Returns a boolean representation of $value 0234 * 0235 * @param string $value 0236 * @return string 0237 */ 0238 public function filter($value) 0239 { 0240 $type = $this->getType(); 0241 $casting = $this->getCasting(); 0242 0243 // STRING YES (Localized) 0244 if ($type >= self::YES) { 0245 $type -= self::YES; 0246 if (is_string($value)) { 0247 // require_once 'Zend/Locale.php'; 0248 $locales = $this->getLocale(); 0249 foreach ($locales as $locale) { 0250 if ($this->_getLocalizedQuestion($value, false, $locale) === false) { 0251 return false; 0252 } 0253 0254 if (!$casting && ($this->_getLocalizedQuestion($value, true, $locale) === true)) { 0255 return true; 0256 } 0257 } 0258 } 0259 } 0260 0261 // STRING FALSE ('false') 0262 if ($type >= self::FALSE_STRING) { 0263 $type -= self::FALSE_STRING; 0264 if (is_string($value) && (strtolower($value) == 'false')) { 0265 return false; 0266 } 0267 0268 if ((!$casting) && is_string($value) && (strtolower($value) == 'true')) { 0269 return true; 0270 } 0271 } 0272 0273 // NULL (null) 0274 if ($type >= self::NULL) { 0275 $type -= self::NULL; 0276 if ($value === null) { 0277 return false; 0278 } 0279 } 0280 0281 // EMPTY_ARRAY (array()) 0282 if ($type >= self::EMPTY_ARRAY) { 0283 $type -= self::EMPTY_ARRAY; 0284 if (is_array($value) && ($value == array())) { 0285 return false; 0286 } 0287 } 0288 0289 // ZERO ('0') 0290 if ($type >= self::ZERO) { 0291 $type -= self::ZERO; 0292 if (is_string($value) && ($value == '0')) { 0293 return false; 0294 } 0295 0296 if ((!$casting) && (is_string($value)) && ($value == '1')) { 0297 return true; 0298 } 0299 } 0300 0301 // STRING ('') 0302 if ($type >= self::STRING) { 0303 $type -= self::STRING; 0304 if (is_string($value) && ($value == '')) { 0305 return false; 0306 } 0307 } 0308 0309 // FLOAT (0.0) 0310 if ($type >= self::FLOAT) { 0311 $type -= self::FLOAT; 0312 if (is_float($value) && ($value == 0.0)) { 0313 return false; 0314 } 0315 0316 if ((!$casting) && is_float($value) && ($value == 1.0)) { 0317 return true; 0318 } 0319 } 0320 0321 // INTEGER (0) 0322 if ($type >= self::INTEGER) { 0323 $type -= self::INTEGER; 0324 if (is_int($value) && ($value == 0)) { 0325 return false; 0326 } 0327 0328 if ((!$casting) && is_int($value) && ($value == 1)) { 0329 return true; 0330 } 0331 } 0332 0333 // BOOLEAN (false) 0334 if ($type >= self::BOOLEAN) { 0335 $type -= self::BOOLEAN; 0336 if (is_bool($value)) { 0337 return $value; 0338 } 0339 } 0340 0341 if ($casting) { 0342 return true; 0343 } 0344 0345 return $value; 0346 } 0347 0348 /** 0349 * Determine the value of a localized string, and compare it to a given value 0350 * 0351 * @param string $value 0352 * @param boolean $yes 0353 * @param array $locale 0354 * @return boolean 0355 */ 0356 protected function _getLocalizedQuestion($value, $yes, $locale) 0357 { 0358 if ($yes == true) { 0359 $question = 'yes'; 0360 $return = true; 0361 } else { 0362 $question = 'no'; 0363 $return = false; 0364 } 0365 $str = Zend_Locale::getTranslation($question, 'question', $locale); 0366 $str = explode(':', $str); 0367 if (!empty($str)) { 0368 foreach($str as $no) { 0369 if (($no == $value) || (strtolower($no) == strtolower($value))) { 0370 return $return; 0371 } 0372 } 0373 } 0374 } 0375 }