PageRenderTime 100ms CodeModel.GetById 40ms RepoModel.GetById 4ms app.codeStats 0ms

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 183 lines | 101 code | 18 blank | 64 comment | 38 complexity | a5fb19b0b1fb54acea3384a25814502c 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: Null.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_Null implements Zend_Filter_Interface
  32. {
  33. const BOOLEAN = 1;
  34. const INTEGER = 2;
  35. const EMPTY_ARRAY = 4;
  36. const STRING = 8;
  37. const ZERO = 16;
  38. const ALL = 31;
  39. protected $_constants = array(
  40. self::BOOLEAN => 'boolean',
  41. self::INTEGER => 'integer',
  42. self::EMPTY_ARRAY => 'array',
  43. self::STRING => 'string',
  44. self::ZERO => 'zero',
  45. self::ALL => 'all'
  46. );
  47. /**
  48. * Internal type to detect
  49. *
  50. * @var integer
  51. */
  52. protected $_type = self::ALL;
  53. /**
  54. * Constructor
  55. *
  56. * @param string|array|Zend_Config $options OPTIONAL
  57. */
  58. public function __construct($options = null)
  59. {
  60. if ($options instanceof Zend_Config) {
  61. $options = $options->toArray();
  62. } else if (!is_array($options)) {
  63. $options = func_get_args();
  64. $temp = array();
  65. if (!empty($options)) {
  66. $temp = array_shift($options);
  67. }
  68. $options = $temp;
  69. } else if (is_array($options) && array_key_exists('type', $options)) {
  70. $options = $options['type'];
  71. }
  72. if (!empty($options)) {
  73. $this->setType($options);
  74. }
  75. }
  76. /**
  77. * Returns the set null types
  78. *
  79. * @return array
  80. */
  81. public function getType()
  82. {
  83. return $this->_type;
  84. }
  85. /**
  86. * Set the null types
  87. *
  88. * @param integer|array $type
  89. * @throws Zend_Filter_Exception
  90. * @return Zend_Filter_Null
  91. */
  92. public function setType($type = null)
  93. {
  94. if (is_array($type)) {
  95. $detected = 0;
  96. foreach($type as $value) {
  97. if (is_int($value)) {
  98. $detected += $value;
  99. } else if (in_array($value, $this->_constants)) {
  100. $detected += array_search($value, $this->_constants);
  101. }
  102. }
  103. $type = $detected;
  104. } else if (is_string($type)) {
  105. if (in_array($type, $this->_constants)) {
  106. $type = array_search($type, $this->_constants);
  107. }
  108. }
  109. if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
  110. require_once 'Zend/Filter/Exception.php';
  111. throw new Zend_Filter_Exception('Unknown type');
  112. }
  113. $this->_type = $type;
  114. return $this;
  115. }
  116. /**
  117. * Defined by Zend_Filter_Interface
  118. *
  119. * Returns null representation of $value, if value is empty and matches
  120. * types that should be considered null.
  121. *
  122. * @param string $value
  123. * @return string
  124. */
  125. public function filter($value)
  126. {
  127. $type = $this->getType();
  128. // STRING ZERO ('0')
  129. if ($type >= self::ZERO) {
  130. $type -= self::ZERO;
  131. if (is_string($value) && ($value == '0')) {
  132. return null;
  133. }
  134. }
  135. // STRING ('')
  136. if ($type >= self::STRING) {
  137. $type -= self::STRING;
  138. if (is_string($value) && ($value == '')) {
  139. return null;
  140. }
  141. }
  142. // EMPTY_ARRAY (array())
  143. if ($type >= self::EMPTY_ARRAY) {
  144. $type -= self::EMPTY_ARRAY;
  145. if (is_array($value) && ($value == array())) {
  146. return null;
  147. }
  148. }
  149. // INTEGER (0)
  150. if ($type >= self::INTEGER) {
  151. $type -= self::INTEGER;
  152. if (is_int($value) && ($value == 0)) {
  153. return null;
  154. }
  155. }
  156. // BOOLEAN (false)
  157. if ($type >= self::BOOLEAN) {
  158. $type -= self::BOOLEAN;
  159. if (is_bool($value) && ($value == false)) {
  160. return null;
  161. }
  162. }
  163. return $value;
  164. }
  165. }