Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NotEmpty.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const BOOLEAN = 1;
36  const INTEGER = 2;
37  const FLOAT = 4;
38  const STRING = 8;
39  const ZERO = 16;
40  const EMPTY_ARRAY = 32;
41  const NULL = 64;
42  const PHP = 127;
43  const SPACE = 128;
44  const OBJECT = 256;
45  const OBJECT_STRING = 512;
46  const OBJECT_COUNT = 1024;
47  const ALL = 2047;
48 
49  const INVALID = 'notEmptyInvalid';
50  const IS_EMPTY = 'isEmpty';
51 
52  protected $_constants = array(
53  self::BOOLEAN => 'boolean',
54  self::INTEGER => 'integer',
55  self::FLOAT => 'float',
56  self::STRING => 'string',
57  self::ZERO => 'zero',
58  self::EMPTY_ARRAY => 'array',
59  self::NULL => 'null',
60  self::PHP => 'php',
61  self::SPACE => 'space',
62  self::OBJECT => 'object',
63  self::OBJECT_STRING => 'objectstring',
64  self::OBJECT_COUNT => 'objectcount',
65  self::ALL => 'all',
66  );
67 
71  protected $_messageTemplates = array(
72  self::IS_EMPTY => "Value is required and can't be empty",
73  self::INVALID => "Invalid type given. String, integer, float, boolean or array expected",
74  );
75 
81  protected $_type = 493;
82 
88  public function __construct($options = null)
89  {
90  if ($options instanceof Zend_Config) {
91  $options = $options->toArray();
92  } else if (!is_array($options)) {
93  $options = func_get_args();
94  $temp = array();
95  if (!empty($options)) {
96  $temp['type'] = array_shift($options);
97  }
98 
99  $options = $temp;
100  }
101 
102  if (is_array($options) && array_key_exists('type', $options)) {
103  $this->setType($options['type']);
104  }
105  }
106 
112  public function getType()
113  {
114  return $this->_type;
115  }
116 
124  public function setType($type = null)
125  {
126  if (is_array($type)) {
127  $detected = 0;
128  foreach($type as $value) {
129  if (is_int($value)) {
130  $detected += $value;
131  } else if (in_array($value, $this->_constants)) {
132  $detected += array_search($value, $this->_constants);
133  }
134  }
135 
136  $type = $detected;
137  } else if (is_string($type) && in_array($type, $this->_constants)) {
138  $type = array_search($type, $this->_constants);
139  }
140 
141  if (!is_int($type) || ($type < 0) || ($type > self::ALL)) {
142  #require_once 'Zend/Validate/Exception.php';
143  throw new Zend_Validate_Exception('Unknown type');
144  }
145 
146  $this->_type = $type;
147  return $this;
148  }
149 
158  public function isValid($value)
159  {
160  if ($value !== null && !is_string($value) && !is_int($value) && !is_float($value) &&
161  !is_bool($value) && !is_array($value) && !is_object($value)) {
162  $this->_error(self::INVALID);
163  return false;
164  }
165 
166  $type = $this->getType();
167  $this->_setValue($value);
168  $object = false;
169 
170  // OBJECT_COUNT (countable object)
171  if ($type >= self::OBJECT_COUNT) {
173  $object = true;
174 
175  if (is_object($value) && ($value instanceof Countable) && (count($value) == 0)) {
176  $this->_error(self::IS_EMPTY);
177  return false;
178  }
179  }
180 
181  // OBJECT_STRING (object's toString)
182  if ($type >= self::OBJECT_STRING) {
184  $object = true;
185 
186  if ((is_object($value) && (!method_exists($value, '__toString'))) ||
187  (is_object($value) && (method_exists($value, '__toString')) && (((string) $value) == ""))) {
188  $this->_error(self::IS_EMPTY);
189  return false;
190  }
191  }
192 
193  // OBJECT (object)
194  if ($type >= self::OBJECT) {
195  $type -= self::OBJECT;
196  // fall trough, objects are always not empty
197  } else if ($object === false) {
198  // object not allowed but object given -> return false
199  if (is_object($value)) {
200  $this->_error(self::IS_EMPTY);
201  return false;
202  }
203  }
204 
205  // SPACE (' ')
206  if ($type >= self::SPACE) {
207  $type -= self::SPACE;
208  if (is_string($value) && (preg_match('/^\s+$/s', $value))) {
209  $this->_error(self::IS_EMPTY);
210  return false;
211  }
212  }
213 
214  // NULL (null)
215  if ($type >= self::NULL) {
216  $type -= self::NULL;
217  if ($value === null) {
218  $this->_error(self::IS_EMPTY);
219  return false;
220  }
221  }
222 
223  // EMPTY_ARRAY (array())
224  if ($type >= self::EMPTY_ARRAY) {
226  if (is_array($value) && ($value == array())) {
227  $this->_error(self::IS_EMPTY);
228  return false;
229  }
230  }
231 
232  // ZERO ('0')
233  if ($type >= self::ZERO) {
234  $type -= self::ZERO;
235  if (is_string($value) && ($value == '0')) {
236  $this->_error(self::IS_EMPTY);
237  return false;
238  }
239  }
240 
241  // STRING ('')
242  if ($type >= self::STRING) {
243  $type -= self::STRING;
244  if (is_string($value) && ($value == '')) {
245  $this->_error(self::IS_EMPTY);
246  return false;
247  }
248  }
249 
250  // FLOAT (0.0)
251  if ($type >= self::FLOAT) {
252  $type -= self::FLOAT;
253  if (is_float($value) && ($value == 0.0)) {
254  $this->_error(self::IS_EMPTY);
255  return false;
256  }
257  }
258 
259  // INTEGER (0)
260  if ($type >= self::INTEGER) {
261  $type -= self::INTEGER;
262  if (is_int($value) && ($value == 0)) {
263  $this->_error(self::IS_EMPTY);
264  return false;
265  }
266  }
267 
268  // BOOLEAN (false)
269  if ($type >= self::BOOLEAN) {
270  $type -= self::BOOLEAN;
271  if (is_bool($value) && ($value == false)) {
272  $this->_error(self::IS_EMPTY);
273  return false;
274  }
275  }
276 
277  return true;
278  }
279 }
setType($type=null)
Definition: NotEmpty.php:124
__construct($options=null)
Definition: NotEmpty.php:88
_error($messageKey, $value=null)
Definition: Abstract.php:284
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16