File indexing completed on 2024-12-22 05:36:41
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_Null implements Zend_Filter_Interface 0034 { 0035 const BOOLEAN = 1; 0036 const INTEGER = 2; 0037 const EMPTY_ARRAY = 4; 0038 const STRING = 8; 0039 const ZERO = 16; 0040 const ALL = 31; 0041 0042 protected $_constants = array( 0043 self::BOOLEAN => 'boolean', 0044 self::INTEGER => 'integer', 0045 self::EMPTY_ARRAY => 'array', 0046 self::STRING => 'string', 0047 self::ZERO => 'zero', 0048 self::ALL => 'all' 0049 ); 0050 0051 /** 0052 * Internal type to detect 0053 * 0054 * @var integer 0055 */ 0056 protected $_type = self::ALL; 0057 0058 /** 0059 * Constructor 0060 * 0061 * @param string|array|Zend_Config $options OPTIONAL 0062 */ 0063 public function __construct($options = null) 0064 { 0065 if ($options instanceof Zend_Config) { 0066 $options = $options->toArray(); 0067 } else if (!is_array($options)) { 0068 $options = func_get_args(); 0069 $temp = array(); 0070 if (!empty($options)) { 0071 $temp = array_shift($options); 0072 } 0073 $options = $temp; 0074 } else if (is_array($options) && array_key_exists('type', $options)) { 0075 $options = $options['type']; 0076 } 0077 0078 if (!empty($options)) { 0079 $this->setType($options); 0080 } 0081 } 0082 0083 /** 0084 * Returns the set null types 0085 * 0086 * @return array 0087 */ 0088 public function getType() 0089 { 0090 return $this->_type; 0091 } 0092 0093 /** 0094 * Set the null types 0095 * 0096 * @param integer|array $type 0097 * @throws Zend_Filter_Exception 0098 * @return Zend_Filter_Null 0099 */ 0100 public function setType($type = null) 0101 { 0102 if (is_array($type)) { 0103 $detected = 0; 0104 foreach($type as $value) { 0105 if (is_int($value)) { 0106 $detected += $value; 0107 } else if (in_array($value, $this->_constants)) { 0108 $detected += array_search($value, $this->_constants); 0109 } 0110 } 0111 0112 $type = $detected; 0113 } else if (is_string($type)) { 0114 if (in_array($type, $this->_constants)) { 0115 $type = array_search($type, $this->_constants); 0116 } 0117 } 0118 0119 if (!is_int($type) || ($type < 0) || ($type > self::ALL)) { 0120 // require_once 'Zend/Filter/Exception.php'; 0121 throw new Zend_Filter_Exception('Unknown type'); 0122 } 0123 0124 $this->_type = $type; 0125 return $this; 0126 } 0127 0128 /** 0129 * Defined by Zend_Filter_Interface 0130 * 0131 * Returns null representation of $value, if value is empty and matches 0132 * types that should be considered null. 0133 * 0134 * @param string $value 0135 * @return string 0136 */ 0137 public function filter($value) 0138 { 0139 $type = $this->getType(); 0140 0141 // STRING ZERO ('0') 0142 if ($type >= self::ZERO) { 0143 $type -= self::ZERO; 0144 if (is_string($value) && ($value == '0')) { 0145 return null; 0146 } 0147 } 0148 0149 // STRING ('') 0150 if ($type >= self::STRING) { 0151 $type -= self::STRING; 0152 if (is_string($value) && ($value == '')) { 0153 return null; 0154 } 0155 } 0156 0157 // EMPTY_ARRAY (array()) 0158 if ($type >= self::EMPTY_ARRAY) { 0159 $type -= self::EMPTY_ARRAY; 0160 if (is_array($value) && ($value == array())) { 0161 return null; 0162 } 0163 } 0164 0165 // INTEGER (0) 0166 if ($type >= self::INTEGER) { 0167 $type -= self::INTEGER; 0168 if (is_int($value) && ($value == 0)) { 0169 return null; 0170 } 0171 } 0172 0173 // BOOLEAN (false) 0174 if ($type >= self::BOOLEAN) { 0175 $type -= self::BOOLEAN; 0176 if (is_bool($value) && ($value == false)) { 0177 return null; 0178 } 0179 } 0180 0181 return $value; 0182 } 0183 }