/**
  * 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 = [];
     $this->setSize(0);
     if ($options instanceof Traversable) {
         $options = ArrayUtils::iteratorToArray($options);
     } elseif (is_scalar($options)) {
         $options = ['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);
 }
Example #2
0
 /**
  * Sets validator options
  *
  * Min limits the used diskspace for all files, when used with max=null it is the maximum filesize
  * It also accepts an array with the keys 'min' and 'max'
  *
  * @param  integer|array|\Zend\Config\Config $options Options for this validator
  * @return void
  */
 public function __construct($options = null)
 {
     $this->_files = array();
     $this->_setSize(0);
     if ($options instanceof \Zend\Config\Config) {
         $options = $options->toArray();
     } elseif (is_scalar($options)) {
         $options = array('max' => $options);
     } elseif (!is_array($options)) {
         throw new \Zend\Validator\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);
 }
Example #3
0
 /**
  * @group ZF-11258
  */
 public function testZF11258()
 {
     $validator = new File\Size(array('min' => 1, 'max' => 10000));
     $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
     $this->assertTrue(array_key_exists('fileSizeNotFound', $validator->getMessages()));
     $this->assertContains("'nofile.mo'", current($validator->getMessages()));
 }
Example #4
0
 /**
  * Ensures that the validator returns size infos
  *
  * @return void
  */
 public function testFailureMessage()
 {
     $validator = new File\Size(array('min' => 9999, 'max' => 10000));
     $this->assertFalse($validator->isValid(__DIR__ . '/_files/testsize.mo'));
     $messages = $validator->getMessages();
     $this->assertContains('9.76kB', current($messages));
     $this->assertContains('794B', current($messages));
     $validator = new File\Size(array('min' => 9999, 'max' => 10000, 'bytestring' => false));
     $this->assertFalse($validator->isValid(__DIR__ . '/_files/testsize.mo'));
     $messages = $validator->getMessages();
     $this->assertContains('9999', current($messages));
     $this->assertContains('794', current($messages));
 }
Example #5
0
 public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage()
 {
     $validator = new File\Size();
     $this->assertFalse($validator->isValid(''));
     $this->assertArrayHasKey(File\Size::NOT_FOUND, $validator->getMessages());
     $filesArray = array('name' => '', 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'type' => '');
     $this->assertFalse($validator->isValid($filesArray));
     $this->assertArrayHasKey(File\Size::NOT_FOUND, $validator->getMessages());
 }
Example #6
0
 public function getSizeValidator()
 {
     if ($this->sizeValidator === NULL) {
         $validator = new Size();
         $validator->setOptions($this->getSizeConfig())->setMessages(array(Size::TOO_BIG => $this->getErrorMessage('TOO_BIG')));
         $this->setSizeValidator($validator);
     }
     return $this->sizeValidator;
 }