Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InArray.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const NOT_IN_ARRAY = 'notInArray';
36 
40  protected $_messageTemplates = array(
41  self::NOT_IN_ARRAY => "'%value%' was not found in the haystack",
42  );
43 
49  protected $_haystack;
50 
56  protected $_strict = false;
57 
63  protected $_recursive = false;
64 
71  public function __construct($options)
72  {
73  if ($options instanceof Zend_Config) {
74  $options = $options->toArray();
75  } else if (!is_array($options)) {
76  #require_once 'Zend/Validate/Exception.php';
77  throw new Zend_Validate_Exception('Array expected as parameter');
78  } else {
79  $count = func_num_args();
80  $temp = array();
81  if ($count > 1) {
82  $temp['haystack'] = func_get_arg(0);
83  $temp['strict'] = func_get_arg(1);
84  $options = $temp;
85  } else {
86  $temp = func_get_arg(0);
87  if (!array_key_exists('haystack', $options)) {
88  $options = array();
89  $options['haystack'] = $temp;
90  } else {
91  $options = $temp;
92  }
93  }
94  }
95 
96  $this->setHaystack($options['haystack']);
97  if (array_key_exists('strict', $options)) {
98  $this->setStrict($options['strict']);
99  }
100 
101  if (array_key_exists('recursive', $options)) {
102  $this->setRecursive($options['recursive']);
103  }
104  }
105 
111  public function getHaystack()
112  {
113  return $this->_haystack;
114  }
115 
122  public function setHaystack(array $haystack)
123  {
124  $this->_haystack = $haystack;
125  return $this;
126  }
127 
133  public function getStrict()
134  {
135  return $this->_strict;
136  }
137 
144  public function setStrict($strict)
145  {
146  $this->_strict = (boolean) $strict;
147  return $this;
148  }
149 
155  public function getRecursive()
156  {
157  return $this->_recursive;
158  }
159 
166  public function setRecursive($recursive)
167  {
168  $this->_recursive = (boolean) $recursive;
169  return $this;
170  }
171 
181  public function isValid($value)
182  {
183  $this->_setValue($value);
184  if ($this->getRecursive()) {
185  $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($this->_haystack));
186  foreach($iterator as $element) {
187  if ($this->_strict) {
188  if ($element === $value) {
189  return true;
190  }
191  } else if ($element == $value) {
192  return true;
193  }
194  }
195  } else {
196  if (in_array($value, $this->_haystack, $this->_strict)) {
197  return true;
198  }
199  }
200 
201  $this->_error(self::NOT_IN_ARRAY);
202  return false;
203  }
204 }
setHaystack(array $haystack)
Definition: InArray.php:122
__construct($options)
Definition: InArray.php:71
$count
Definition: recent.phtml:13
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
setRecursive($recursive)
Definition: InArray.php:166
$element
Definition: element.phtml:12