Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Count.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
36 {
40  const TOO_MANY = 'fileCountTooMany';
41  const TOO_FEW = 'fileCountTooFew';
47  protected $_messageTemplates = array(
48  self::TOO_MANY => "Too many files, maximum '%max%' are allowed but '%count%' are given",
49  self::TOO_FEW => "Too few files, minimum '%min%' are expected but '%count%' are given",
50  );
51 
55  protected $_messageVariables = array(
56  'min' => '_min',
57  'max' => '_max',
58  'count' => '_count'
59  );
60 
68  protected $_min;
69 
77  protected $_max;
78 
84  protected $_count;
85 
90  protected $_files;
91 
106  public function __construct($options)
107  {
108  if ($options instanceof Zend_Config) {
109  $options = $options->toArray();
110  } elseif (is_string($options) || is_numeric($options)) {
111  $options = array('max' => $options);
112  } elseif (!is_array($options)) {
113  #require_once 'Zend/Validate/Exception.php';
114  throw new Zend_Validate_Exception ('Invalid options to validator provided');
115  }
116 
117  if (1 < func_num_args()) {
118  $options['min'] = func_get_arg(0);
119  $options['max'] = func_get_arg(1);
120  }
121 
122  if (isset($options['min'])) {
123  $this->setMin($options);
124  }
125 
126  if (isset($options['max'])) {
127  $this->setMax($options);
128  }
129  }
130 
136  public function getMin()
137  {
138  return $this->_min;
139  }
140 
148  public function setMin($min)
149  {
150  if (is_array($min) and isset($min['min'])) {
151  $min = $min['min'];
152  }
153 
154  if (!is_string($min) and !is_numeric($min)) {
155  #require_once 'Zend/Validate/Exception.php';
156  throw new Zend_Validate_Exception ('Invalid options to validator provided');
157  }
158 
159  $min = (integer) $min;
160  if (($this->_max !== null) && ($min > $this->_max)) {
161  #require_once 'Zend/Validate/Exception.php';
162  throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum file count, but $min >"
163  . " {$this->_max}");
164  }
165 
166  $this->_min = $min;
167  return $this;
168  }
169 
175  public function getMax()
176  {
177  return $this->_max;
178  }
179 
187  public function setMax($max)
188  {
189  if (is_array($max) and isset($max['max'])) {
190  $max = $max['max'];
191  }
192 
193  if (!is_string($max) and !is_numeric($max)) {
194  #require_once 'Zend/Validate/Exception.php';
195  throw new Zend_Validate_Exception ('Invalid options to validator provided');
196  }
197 
198  $max = (integer) $max;
199  if (($this->_min !== null) && ($max < $this->_min)) {
200  #require_once 'Zend/Validate/Exception.php';
201  throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum file count, but "
202  . "$max < {$this->_min}");
203  }
204 
205  $this->_max = $max;
206  return $this;
207  }
208 
215  public function addFile($file)
216  {
217  if (is_string($file)) {
218  $file = array($file);
219  }
220 
221  if (is_array($file)) {
222  foreach ($file as $name) {
223  if (!isset($this->_files[$name]) && !empty($name)) {
224  $this->_files[$name] = $name;
225  }
226  }
227  }
228 
229  return $this;
230  }
231 
243  public function isValid($value, $file = null)
244  {
245  if (($file !== null) && !array_key_exists('destination', $file)) {
246  $file['destination'] = dirname($value);
247  }
248 
249  if (($file !== null) && array_key_exists('tmp_name', $file)) {
250  $value = $file['destination'] . DIRECTORY_SEPARATOR . $file['name'];
251  }
252 
253  if (($file === null) || !empty($file['tmp_name'])) {
254  $this->addFile($value);
255  }
256 
257  $this->_count = count($this->_files);
258  if (($this->_max !== null) && ($this->_count > $this->_max)) {
259  return $this->_throw($file, self::TOO_MANY);
260  }
261 
262  if (($this->_min !== null) && ($this->_count < $this->_min)) {
263  return $this->_throw($file, self::TOO_FEW);
264  }
265 
266  return true;
267  }
268 
276  protected function _throw($file, $errorType)
277  {
278  if ($file !== null) {
279  $this->_value = $file['name'];
280  }
281 
282  $this->_error($errorType);
283  return false;
284  }
285 }
__construct($options)
Definition: Count.php:106
_throw($file, $errorType)
Definition: Count.php:276
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
isValid($value, $file=null)
Definition: Count.php:243
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14