Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Between.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
38  const NOT_BETWEEN = 'notBetween';
39 
43  const NOT_BETWEEN_STRICT = 'notBetweenStrict';
44 
50  protected $_messageTemplates = array(
51  self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%', inclusively",
52  self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%'"
53  );
54 
60  protected $_messageVariables = array(
61  'min' => '_min',
62  'max' => '_max'
63  );
64 
70  protected $_min;
71 
77  protected $_max;
78 
87  protected $_inclusive;
88 
99  public function __construct($options)
100  {
101  if ($options instanceof Zend_Config) {
102  $options = $options->toArray();
103  } else if (!is_array($options)) {
104  $options = func_get_args();
105  $temp['min'] = array_shift($options);
106  if (!empty($options)) {
107  $temp['max'] = array_shift($options);
108  }
109 
110  if (!empty($options)) {
111  $temp['inclusive'] = array_shift($options);
112  }
113 
114  $options = $temp;
115  }
116 
117  if (!array_key_exists('min', $options) || !array_key_exists('max', $options)) {
118  #require_once 'Zend/Validate/Exception.php';
119  throw new Zend_Validate_Exception("Missing option. 'min' and 'max' has to be given");
120  }
121 
122  if (!array_key_exists('inclusive', $options)) {
123  $options['inclusive'] = true;
124  }
125 
126  $this->setMin($options['min'])
127  ->setMax($options['max'])
128  ->setInclusive($options['inclusive']);
129  }
130 
136  public function getMin()
137  {
138  return $this->_min;
139  }
140 
147  public function setMin($min)
148  {
149  $this->_min = $min;
150  return $this;
151  }
152 
158  public function getMax()
159  {
160  return $this->_max;
161  }
162 
169  public function setMax($max)
170  {
171  $this->_max = $max;
172  return $this;
173  }
174 
180  public function getInclusive()
181  {
182  return $this->_inclusive;
183  }
184 
191  public function setInclusive($inclusive)
192  {
193  $this->_inclusive = $inclusive;
194  return $this;
195  }
196 
206  public function isValid($value)
207  {
208  $this->_setValue($value);
209 
210  if ($this->_inclusive) {
211  if ($this->_min > $value || $value > $this->_max) {
212  $this->_error(self::NOT_BETWEEN);
213  return false;
214  }
215  } else {
216  if ($this->_min >= $value || $value >= $this->_max) {
217  $this->_error(self::NOT_BETWEEN_STRICT);
218  return false;
219  }
220  }
221  return true;
222  }
223 
224 }
_error($messageKey, $value=null)
Definition: Abstract.php:284
__construct($options)
Definition: Between.php:99
$value
Definition: gender.phtml:16
setInclusive($inclusive)
Definition: Between.php:191
const NOT_BETWEEN_STRICT
Definition: Between.php:43