Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Null.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Filter/Interface.php';
26 
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 
56  protected $_type = self::ALL;
57 
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 
88  public function getType()
89  {
90  return $this->_type;
91  }
92 
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 
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) {
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 }
filter($value)
Definition: Null.php:137
const ALL
Definition: Null.php:40
const STRING
Definition: Null.php:38
const BOOLEAN
Definition: Null.php:35
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
const ZERO
Definition: Null.php:39
const INTEGER
Definition: Null.php:36
const EMPTY_ARRAY
Definition: Null.php:37
__construct($options=null)
Definition: Null.php:63
setType($type=null)
Definition: Null.php:100