2015-06-16 04:55:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use SplFileInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate file size.
|
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
2015-07-20 18:42:08 +02:00
|
|
|
class Size extends AbstractRule
|
2015-06-16 04:55:09 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $minSize;
|
|
|
|
|
2015-07-20 19:21:16 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $minValue;
|
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $maxSize;
|
|
|
|
|
2015-07-20 19:21:16 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $maxValue;
|
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
|
|
|
* @param string $minSize
|
|
|
|
* @param string $maxSize
|
|
|
|
*/
|
|
|
|
public function __construct($minSize = null, $maxSize = null)
|
|
|
|
{
|
|
|
|
$this->minSize = $minSize;
|
2015-07-20 18:42:08 +02:00
|
|
|
$this->minValue = $minSize ? $this->toBytes($minSize) : null;
|
2015-06-16 04:55:09 +02:00
|
|
|
$this->maxSize = $maxSize;
|
2015-07-20 18:42:08 +02:00
|
|
|
$this->maxValue = $maxSize ? $this->toBytes($maxSize) : null;
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Move it to a trait
|
|
|
|
*
|
|
|
|
* @param mixed $size
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function toBytes($size)
|
|
|
|
{
|
|
|
|
$value = $size;
|
2015-10-18 03:44:47 +02:00
|
|
|
$units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];
|
2015-06-16 04:55:09 +02:00
|
|
|
foreach ($units as $exponent => $unit) {
|
|
|
|
if (!preg_match("/^(\d+(.\d+)?){$unit}$/i", $size, $matches)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$value = $matches[1] * pow(1024, $exponent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_numeric($value)) {
|
|
|
|
throw new ComponentException(sprintf('"%s" is not a recognized file size.', $size));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:42:08 +02:00
|
|
|
/**
|
|
|
|
* @param int $size
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isValidSize($size)
|
|
|
|
{
|
|
|
|
if (null !== $this->minValue && null !== $this->maxValue) {
|
|
|
|
return ($size >= $this->minValue && $size <= $this->maxValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $this->minValue) {
|
|
|
|
return ($size >= $this->minValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($size <= $this->maxValue);
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function validate($input)
|
|
|
|
{
|
|
|
|
if ($input instanceof SplFileInfo) {
|
2015-07-20 18:42:08 +02:00
|
|
|
return $this->isValidSize($input->getSize());
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:42:08 +02:00
|
|
|
if (is_string($input)) {
|
|
|
|
return $this->isValidSize(filesize($input));
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 18:42:08 +02:00
|
|
|
return false;
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
}
|