PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Filter/Boolean.php

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