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