PageRenderTime 108ms CodeModel.GetById 24ms RepoModel.GetById 7ms app.codeStats 0ms

/src/application/libraries/Zend/Filter/Boolean.php

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