Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Validate_File_Size Class Reference
Inheritance diagram for Zend_Validate_File_Size:
Zend_Validate_Abstract Zend_Validate_Interface Size Zend_Validate_File_FilesSize

Public Member Functions

 __construct ($options)
 
 setUseByteString ($byteString=true)
 
 useByteString ()
 
 getMin ($raw=false)
 
 setMin ($min)
 
 getMax ($raw=false)
 
 setMax ($max)
 
 isValid ($value, $file=null)
 
- Public Member Functions inherited from Zend_Validate_Abstract
 getMessages ()
 
 getMessageVariables ()
 
 getMessageTemplates ()
 
 setMessage ($messageString, $messageKey=null)
 
 setMessages (array $messages)
 
 __get ($property)
 
 getErrors ()
 
 setObscureValue ($flag)
 
 getObscureValue ()
 
 setTranslator ($translator=null)
 
 getTranslator ()
 
 hasTranslator ()
 
 setDisableTranslator ($flag)
 
 translatorIsDisabled ()
 
- Public Member Functions inherited from Zend_Validate_Interface
 isValid ($value)
 

Data Fields

const TOO_BIG = 'fileSizeTooBig'
 
const TOO_SMALL = 'fileSizeTooSmall'
 
const NOT_FOUND = 'fileSizeNotFound'
 

Protected Member Functions

 _getSize ()
 
 _setSize ($size)
 
 _toByteString ($size)
 
 _fromByteString ($size)
 
 _throw ($file, $errorType)
 
- Protected Member Functions inherited from Zend_Validate_Abstract
 _createMessage ($messageKey, $value)
 
 _implodeRecursive (array $pieces)
 
 _error ($messageKey, $value=null)
 
 _setValue ($value)
 

Protected Attributes

 $_messageTemplates
 
 $_messageVariables
 
 $_min
 
 $_max
 
 $_size
 
 $_useByteString = true
 
- Protected Attributes inherited from Zend_Validate_Abstract
 $_value
 
 $_messageVariables = array()
 
 $_messageTemplates = array()
 
 $_messages = array()
 
 $_obscureValue = false
 
 $_errors = array()
 
 $_translator
 
 $_translatorDisabled = false
 

Additional Inherited Members

- Static Public Member Functions inherited from Zend_Validate_Abstract
static setDefaultTranslator ($translator=null)
 
static getDefaultTranslator ()
 
static hasDefaultTranslator ()
 
static getMessageLength ()
 
static setMessageLength ($length=-1)
 
- Static Protected Attributes inherited from Zend_Validate_Abstract
static $_defaultTranslator
 
static $_messageLength = -1
 

Detailed Description

Definition at line 35 of file Size.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options)

Sets validator options

If $options is a integer, it will be used as maximum filesize As Array is accepts the following keys: 'min': Minimum filesize 'max': Maximum filesize 'bytestring': Use bytestring or real size for messages

Parameters
integer | array$optionsOptions for the adapter
Exceptions
Zend_Validate_Exception

Definition at line 104 of file Size.php.

105  {
106  if ($options instanceof Zend_Config) {
107  $options = $options->toArray();
108  } elseif (is_string($options) || is_numeric($options)) {
109  $options = array('max' => $options);
110  } elseif (!is_array($options)) {
111  #require_once 'Zend/Validate/Exception.php';
112  throw new Zend_Validate_Exception ('Invalid options to validator provided');
113  }
114 
115  if (1 < func_num_args()) {
116  $argv = func_get_args();
117  array_shift($argv);
118  $options['max'] = array_shift($argv);
119  if (!empty($argv)) {
120  $options['bytestring'] = array_shift($argv);
121  }
122  }
123 
124  if (isset($options['bytestring'])) {
125  $this->setUseByteString($options['bytestring']);
126  }
127 
128  if (isset($options['min'])) {
129  $this->setMin($options['min']);
130  }
131 
132  if (isset($options['max'])) {
133  $this->setMax($options['max']);
134  }
135  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setUseByteString($byteString=true)
Definition: Size.php:143

Member Function Documentation

◆ _fromByteString()

_fromByteString (   $size)
protected

Returns the unformatted size

Parameters
string$size
Returns
integer

Definition at line 340 of file Size.php.

341  {
342  if (is_numeric($size)) {
343  return (integer) $size;
344  }
345 
346  $type = trim(substr($size, -2, 1));
347 
348  $value = substr($size, 0, -1);
349  if (!is_numeric($value)) {
350  $value = substr($value, 0, -1);
351  }
352 
353  switch (strtoupper($type)) {
354  case 'Y':
355  $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
356  break;
357  case 'Z':
358  $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024);
359  break;
360  case 'E':
361  $value *= (1024 * 1024 * 1024 * 1024 * 1024 * 1024);
362  break;
363  case 'P':
364  $value *= (1024 * 1024 * 1024 * 1024 * 1024);
365  break;
366  case 'T':
367  $value *= (1024 * 1024 * 1024 * 1024);
368  break;
369  case 'G':
370  $value *= (1024 * 1024 * 1024);
371  break;
372  case 'M':
373  $value *= (1024 * 1024);
374  break;
375  case 'K':
376  $value *= 1024;
377  break;
378  default:
379  break;
380  }
381 
382  return $value;
383  }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ _getSize()

_getSize ( )
protected

Retrieve current detected file size

Returns
int

Definition at line 248 of file Size.php.

249  {
250  return $this->_size;
251  }

◆ _setSize()

_setSize (   $size)
protected

Set current size

Parameters
int$size
Returns
Zend_Validate_File_Size

Definition at line 259 of file Size.php.

260  {
261  $this->_size = $size;
262  return $this;
263  }

◆ _throw()

_throw (   $file,
  $errorType 
)
protected

Throws an error of the given type

Parameters
string$file
string$errorType
Returns
false

Definition at line 392 of file Size.php.

393  {
394  if ($file !== null) {
395  $this->_value = $file['name'];
396  }
397 
398  $this->_error($errorType);
399  return false;
400  }
_error($messageKey, $value=null)
Definition: Abstract.php:284

◆ _toByteString()

_toByteString (   $size)
protected

Returns the formatted size

Parameters
integer$size
Returns
string

Definition at line 324 of file Size.php.

325  {
326  $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
327  for ($i=0; $size >= 1024 && $i < 9; $i++) {
328  $size /= 1024;
329  }
330 
331  return round($size, 2) . $sizes[$i];
332  }
$i
Definition: gallery.phtml:31

◆ getMax()

getMax (   $raw = false)

Returns the maximum filesize

Parameters
bool$rawWhether or not to force return of the raw value (defaults off)
Returns
integer|string

Definition at line 207 of file Size.php.

208  {
209  $max = $this->_max;
210  if (!$raw && $this->useByteString()) {
211  $max = $this->_toByteString($max);
212  }
213 
214  return $max;
215  }

◆ getMin()

getMin (   $raw = false)

Returns the minimum filesize

Parameters
bool$rawWhether or not to force return of the raw value (defaults off)
Returns
integer|string

Definition at line 165 of file Size.php.

166  {
167  $min = $this->_min;
168  if (!$raw && $this->useByteString()) {
169  $min = $this->_toByteString($min);
170  }
171 
172  return $min;
173  }

◆ isValid()

isValid (   $value,
  $file = null 
)

Defined by Zend_Validate_Interface

Returns true if and only if the filesize of $value is at least min and not bigger than max (when max is not null).

Parameters
string$valueReal file to check for size
array$fileFile data from Zend_File_Transfer
Returns
boolean

Definition at line 275 of file Size.php.

276  {
277  // Is file readable ?
278  #require_once 'Zend/Loader.php';
280  return $this->_throw($file, self::NOT_FOUND);
281  }
282 
283  // limited to 4GB files
284  $size = sprintf("%u", @filesize($value));
285  $this->_size = $size;
286 
287  // Check to see if it's smaller than min size
288  $min = $this->getMin(true);
289  $max = $this->getMax(true);
290  if (($min !== null) && ($size < $min)) {
291  if ($this->useByteString()) {
292  $this->_min = $this->_toByteString($min);
293  $this->_size = $this->_toByteString($size);
294  $this->_throw($file, self::TOO_SMALL);
295  $this->_min = $min;
296  $this->_size = $size;
297  } else {
298  $this->_throw($file, self::TOO_SMALL);
299  }
300  }
301 
302  // Check to see if it's larger than max size
303  if (($max !== null) && ($max < $size)) {
304  if ($this->useByteString()) {
305  $this->_max = $this->_toByteString($max);
306  $this->_size = $this->_toByteString($size);
307  $this->_throw($file, self::TOO_BIG);
308  $this->_max = $max;
309  $this->_size = $size;
310  } else {
311  $this->_throw($file, self::TOO_BIG);
312  }
313  }
314 
315  return empty($this->_messages);
316  }
getMax($raw=false)
Definition: Size.php:207
static isReadable($filename)
Definition: Loader.php:162
$value
Definition: gender.phtml:16
getMin($raw=false)
Definition: Size.php:165
_throw($file, $errorType)
Definition: Size.php:392

◆ setMax()

setMax (   $max)

Sets the maximum filesize

Parameters
integer$maxThe maximum filesize
Exceptions
Zend_Validate_ExceptionWhen max is smaller than min
Returns
Zend_Validate_StringLength Provides a fluent interface

Definition at line 224 of file Size.php.

225  {
226  if (!is_string($max) && !is_numeric($max)) {
227  #require_once 'Zend/Validate/Exception.php';
228  throw new Zend_Validate_Exception ('Invalid options to validator provided');
229  }
230 
231  $max = (integer) $this->_fromByteString($max);
232  $min = $this->getMin(true);
233  if (($min !== null) && ($max < $min)) {
234  #require_once 'Zend/Validate/Exception.php';
235  throw new Zend_Validate_Exception("The maximum must be greater than or equal to the minimum filesize, but "
236  . "$max < $min");
237  }
238 
239  $this->_max = $max;
240  return $this;
241  }
_fromByteString($size)
Definition: Size.php:340
getMin($raw=false)
Definition: Size.php:165

◆ setMin()

setMin (   $min)

Sets the minimum filesize

Parameters
integer$minThe minimum filesize
Exceptions
Zend_Validate_ExceptionWhen min is greater than max
Returns
Zend_Validate_File_Size Provides a fluent interface

Definition at line 182 of file Size.php.

183  {
184  if (!is_string($min) and !is_numeric($min)) {
185  #require_once 'Zend/Validate/Exception.php';
186  throw new Zend_Validate_Exception ('Invalid options to validator provided');
187  }
188 
189  $min = (integer) $this->_fromByteString($min);
190  $max = $this->getMax(true);
191  if (($max !== null) && ($min > $max)) {
192  #require_once 'Zend/Validate/Exception.php';
193  throw new Zend_Validate_Exception("The minimum must be less than or equal to the maximum filesize, but $min >"
194  . " $max");
195  }
196 
197  $this->_min = $min;
198  return $this;
199  }
getMax($raw=false)
Definition: Size.php:207
_fromByteString($size)
Definition: Size.php:340

◆ setUseByteString()

setUseByteString (   $byteString = true)

Returns the minimum filesize

Parameters
boolean$byteStringUse bytestring ?
Returns
integer

Definition at line 143 of file Size.php.

144  {
145  $this->_useByteString = (bool) $byteString;
146  return $this;
147  }

◆ useByteString()

useByteString ( )

Will bytestring be used?

Returns
boolean

Definition at line 154 of file Size.php.

155  {
156  return $this->_useByteString;
157  }

Field Documentation

◆ $_max

$_max
protected

Definition at line 76 of file Size.php.

◆ $_messageTemplates

$_messageTemplates
protected
Initial value:
= array(
self::TOO_BIG => "Maximum allowed size for file '%value%' is '%max%' but '%size%' detected",
self::TOO_SMALL => "Minimum expected size for file '%value%' is '%min%' but '%size%' detected",
self::NOT_FOUND => "File '%value%' is not readable or does not exist",
)

Definition at line 48 of file Size.php.

◆ $_messageVariables

$_messageVariables
protected
Initial value:
= array(
'min' => '_min',
'max' => '_max',
'size' => '_size',
)

Definition at line 57 of file Size.php.

◆ $_min

$_min
protected

Definition at line 67 of file Size.php.

◆ $_size

$_size
protected

Definition at line 83 of file Size.php.

◆ $_useByteString

$_useByteString = true
protected

Definition at line 90 of file Size.php.

◆ NOT_FOUND

const NOT_FOUND = 'fileSizeNotFound'

Definition at line 42 of file Size.php.

◆ TOO_BIG

const TOO_BIG = 'fileSizeTooBig'

#+ @const string Error constants

Definition at line 40 of file Size.php.

◆ TOO_SMALL

const TOO_SMALL = 'fileSizeTooSmall'

Definition at line 41 of file Size.php.


The documentation for this class was generated from the following file: