Source of file FilesSize.php
Size: 5,372 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Validator/File/FilesSize.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Validator\File; use Traversable; use Zend\Stdlib\ArrayUtils; use Zend\Stdlib\ErrorHandler; use Zend\Validator\Exception; /** * Validator for the size of all files which will be validated in sum * */ class FilesSize extends Size { /** * @const string Error constants */ const TOO_BIG = 'fileFilesSizeTooBig'; const TOO_SMALL = 'fileFilesSizeTooSmall'; const NOT_READABLE = 'fileFilesSizeNotReadable'; /** * @var array Error message templates */ protected $messageTemplates = array( self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected", self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected", self::NOT_READABLE => "One or more files can not be read", ); /** * Internal file array * * @var array */ protected $files; /** * Sets validator options * * Min limits the used disk space for all files, when used with max=null it is the maximum file size * It also accepts an array with the keys 'min' and 'max' * * @param int|array|Traversable $options Options for this validator * @throws \Zend\Validator\Exception\InvalidArgumentException */ public function __construct($options = null) { $this->files = array(); $this->setSize(0); if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } elseif (is_scalar($options)) { $options = array('max' => $options); } elseif (!is_array($options)) { throw new Exception\InvalidArgumentException('Invalid options to validator provided'); } if (1 < func_num_args()) { $argv = func_get_args(); array_shift($argv); $options['max'] = array_shift($argv); if (!empty($argv)) { $options['useByteString'] = array_shift($argv); } } parent::__construct($options); } /** * Returns true if and only if the disk usage of all files is at least min and * not bigger than max (when max is not null). * * @param string|array $value Real file to check for size * @param array $file File data from \Zend\File\Transfer\Transfer * @return bool */ public function isValid($value, $file = null) { if (is_string($value)) { $value = array($value); } $min = $this->getMin(true); $max = $this->getMax(true); $size = $this->getSize(); foreach ($value as $files) { // Is file readable ? if (empty($files) || false === stream_resolve_include_path($files)) { $this->throwError($file, self::NOT_READABLE); continue; } if (!isset($this->files[$files])) { $this->files[$files] = $files; } else { // file already counted... do not count twice continue; } // limited to 2GB files ErrorHandler::start(); $size += filesize($files); ErrorHandler::stop(); $this->size = $size; if (($max !== null) && ($max < $size)) { if ($this->getByteString()) { $this->options['max'] = $this->toByteString($max); $this->size = $this->toByteString($size); $this->throwError($file, self::TOO_BIG); $this->options['max'] = $max; $this->size = $size; } else { $this->throwError($file, self::TOO_BIG); } } } // Check that aggregate files are >= minimum size if (($min !== null) && ($size < $min)) { if ($this->getByteString()) { $this->options['min'] = $this->toByteString($min); $this->size = $this->toByteString($size); $this->throwError($file, self::TOO_SMALL); $this->options['min'] = $min; $this->size = $size; } else { $this->throwError($file, self::TOO_SMALL); } } if (count($this->getMessages()) > 0) { return false; } return true; } /** * Throws an error of the given type * * @param string $file * @param string $errorType * @return false */ protected function throwError($file, $errorType) { if ($file !== null) { if (is_array($file)) { if (array_key_exists('name', $file)) { $this->value = $file['name']; } } elseif (is_string($file)) { $this->value = $file; } } $this->error($errorType); return false; } } |