Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Filter / Null.php
bloba47e86c238059103b0856f00f229f6e205ff96fb
1 <?php
2 /**
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
8 */
10 namespace Zend\Filter;
12 use Traversable;
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;
22 const TYPE_ALL = 63;
24 /**
25 * @var array
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',
37 /**
38 * @var array
40 protected $options = array(
41 'type' => self::TYPE_ALL,
44 /**
45 * Constructor
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);
59 } else {
60 $this->setType($typeOrOptions);
62 } else {
63 $this->setType($typeOrOptions);
68 /**
69 * Set boolean types
71 * @param int|array $type
72 * @throws Exception\InvalidArgumentException
73 * @return self
75 public function setType($type = null)
77 if (is_array($type)) {
78 $detected = 0;
79 foreach ($type as $value) {
80 if (is_int($value)) {
81 $detected += $value;
82 } elseif (in_array($value, $this->constants)) {
83 $detected += array_search($value, $this->constants);
87 $type = $detected;
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)',
95 $type,
96 gettype($type)
97 ));
100 $this->options['type'] = $type;
101 return $this;
105 * Returns defined boolean types
107 * @return int
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
121 * @return string
123 public function filter($value)
125 $type = $this->getType();
127 // FLOAT (0.0)
128 if ($type >= self::TYPE_FLOAT) {
129 $type -= self::TYPE_FLOAT;
130 if (is_float($value) && ($value == 0.0)) {
131 return null;
135 // STRING ZERO ('0')
136 if ($type >= self::TYPE_ZERO_STRING) {
137 $type -= self::TYPE_ZERO_STRING;
138 if (is_string($value) && ($value == '0')) {
139 return null;
143 // STRING ('')
144 if ($type >= self::TYPE_STRING) {
145 $type -= self::TYPE_STRING;
146 if (is_string($value) && ($value == '')) {
147 return null;
151 // EMPTY_ARRAY (array())
152 if ($type >= self::TYPE_EMPTY_ARRAY) {
153 $type -= self::TYPE_EMPTY_ARRAY;
154 if (is_array($value) && ($value == array())) {
155 return null;
159 // INTEGER (0)
160 if ($type >= self::TYPE_INTEGER) {
161 $type -= self::TYPE_INTEGER;
162 if (is_int($value) && ($value == 0)) {
163 return null;
167 // BOOLEAN (false)
168 if ($type >= self::TYPE_BOOLEAN) {
169 $type -= self::TYPE_BOOLEAN;
170 if (is_bool($value) && ($value == false)) {
171 return null;
175 return $value;