3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Filter
;
14 class Null extends AbstractFilter
16 const TYPE_BOOLEAN
= 1;
17 const TYPE_INTEGER
= 2;
18 const TYPE_EMPTY_ARRAY
= 4;
19 const TYPE_STRING
= 8;
20 const TYPE_ZERO_STRING
= 16;
21 const TYPE_FLOAT
= 32;
27 protected $constants = array(
28 self
::TYPE_BOOLEAN
=> 'boolean',
29 self
::TYPE_INTEGER
=> 'integer',
30 self
::TYPE_EMPTY_ARRAY
=> 'array',
31 self
::TYPE_STRING
=> 'string',
32 self
::TYPE_ZERO_STRING
=> 'zero',
33 self
::TYPE_FLOAT
=> 'float',
34 self
::TYPE_ALL
=> 'all',
40 protected $options = array(
41 'type' => self
::TYPE_ALL
,
47 * @param string|array|Traversable $typeOrOptions OPTIONAL
49 public function __construct($typeOrOptions = null)
51 if ($typeOrOptions !== null) {
52 if ($typeOrOptions instanceof Traversable
) {
53 $typeOrOptions = iterator_to_array($typeOrOptions);
56 if (is_array($typeOrOptions)) {
57 if (isset($typeOrOptions['type'])) {
58 $this->setOptions($typeOrOptions);
60 $this->setType($typeOrOptions);
63 $this->setType($typeOrOptions);
71 * @param int|array $type
72 * @throws Exception\InvalidArgumentException
75 public function setType($type = null)
77 if (is_array($type)) {
79 foreach ($type as $value) {
82 } elseif (in_array($value, $this->constants
)) {
83 $detected +
= array_search($value, $this->constants
);
88 } elseif (is_string($type) && in_array($type, $this->constants
)) {
89 $type = array_search($type, $this->constants
);
92 if (!is_int($type) ||
($type < 0) ||
($type > self
::TYPE_ALL
)) {
93 throw new Exception\
InvalidArgumentException(sprintf(
94 'Unknown type value "%s" (%s)',
100 $this->options
['type'] = $type;
105 * Returns defined boolean types
109 public function getType()
111 return $this->options
['type'];
115 * Defined by Zend\Filter\FilterInterface
117 * Returns null representation of $value, if value is empty and matches
118 * types that should be considered null.
120 * @param string $value
123 public function filter($value)
125 $type = $this->getType();
128 if ($type >= self
::TYPE_FLOAT
) {
129 $type -= self
::TYPE_FLOAT
;
130 if (is_float($value) && ($value == 0.0)) {
136 if ($type >= self
::TYPE_ZERO_STRING
) {
137 $type -= self
::TYPE_ZERO_STRING
;
138 if (is_string($value) && ($value == '0')) {
144 if ($type >= self
::TYPE_STRING
) {
145 $type -= self
::TYPE_STRING
;
146 if (is_string($value) && ($value == '')) {
151 // EMPTY_ARRAY (array())
152 if ($type >= self
::TYPE_EMPTY_ARRAY
) {
153 $type -= self
::TYPE_EMPTY_ARRAY
;
154 if (is_array($value) && ($value == array())) {
160 if ($type >= self
::TYPE_INTEGER
) {
161 $type -= self
::TYPE_INTEGER
;
162 if (is_int($value) && ($value == 0)) {
168 if ($type >= self
::TYPE_BOOLEAN
) {
169 $type -= self
::TYPE_BOOLEAN
;
170 if (is_bool($value) && ($value == false)) {