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.
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
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
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
40 const EMPTY_ARRAY
= 32;
43 const FALSE_STRING
= 128;
47 protected $_constants = array(
48 self
::BOOLEAN
=> 'boolean',
49 self
::INTEGER => 'integer',
50 self
::FLOAT => 'float',
51 self
::STRING => 'string',
53 self
::EMPTY_ARRAY
=> 'array',
56 self
::FALSE_STRING
=> 'false',
62 * Internal type to detect
66 protected $_type = self
::PHP
;
73 protected $_locale = array('auto');
80 protected $_casting = true;
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();
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);
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
127 public function getType()
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)) {
143 foreach($type as $value) {
144 if (is_int($value)) {
146 } elseif (in_array($value, $this->_constants
)) {
147 $detected +
= array_search($value, $this->_constants
);
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;
166 * Returns the set locale
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;
206 * Returns the casting option
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;
231 * Defined by Zend_Filter_Interface
233 * Returns a boolean representation of $value
235 * @param string $value
238 public function filter($value)
240 $type = $this->getType();
241 $casting = $this->getCasting();
243 // STRING YES (Localized)
244 if ($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) {
254 if (!$casting && ($this->_getLocalizedQuestion($value, true, $locale) === true)) {
261 // STRING FALSE ('false')
262 if ($type >= self
::FALSE_STRING
) {
263 $type -= self
::FALSE_STRING
;
264 if (is_string($value) && (strtolower($value) == 'false')) {
268 if ((!$casting) && is_string($value) && (strtolower($value) == 'true')) {
274 if ($type >= self
::NULL) {
276 if (is_null($value)) {
281 // EMPTY_ARRAY (array())
282 if ($type >= self
::EMPTY_ARRAY
) {
283 $type -= self
::EMPTY_ARRAY
;
284 if (is_array($value) && ($value == array())) {
290 if ($type >= self
::ZERO
) {
292 if (is_string($value) && ($value == '0')) {
296 if ((!$casting) && (is_string($value)) && ($value == '1')) {
302 if ($type >= self
::STRING) {
303 $type -= self
::STRING;
304 if (is_string($value) && ($value == '')) {
310 if ($type >= self
::FLOAT) {
311 $type -= self
::FLOAT;
312 if (is_float($value) && ($value == 0.0)) {
316 if ((!$casting) && is_float($value) && ($value == 1.0)) {
322 if ($type >= self
::INTEGER) {
323 $type -= self
::INTEGER;
324 if (is_int($value) && ($value == 0)) {
328 if ((!$casting) && is_int($value) && ($value == 1)) {
334 if ($type >= self
::BOOLEAN
) {
335 $type -= self
::BOOLEAN
;
336 if (is_bool($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
356 protected function _getLocalizedQuestion($value, $yes, $locale)
365 $str = Zend_Locale
::getTranslation($question, 'question', $locale);
366 $str = explode(':', $str);
368 foreach($str as $no) {
369 if (($no == $value) ||
(strtolower($no) == strtolower($value))) {