respect-validation/library/Rules/Size.php
Henrique Moody c60ca7937b
Update the Coding Standards
This commit will add a couple of rules that we already use but also
introduce new ones and make sure all the code is in accordance with the
new coding standards.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-02-17 19:52:26 +01:00

116 lines
2.6 KiB
PHP

<?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.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException;
use SplFileInfo;
use function filesize;
use function is_numeric;
use function is_string;
use function preg_match;
use function sprintf;
/**
* Validate file size.
*
* @author Henrique Moody <henriquemoody@gmail.com>
*/
class Size extends AbstractRule
{
/**
* @var string
*/
public $minSize;
/**
* @var float|null
*/
public $minValue;
/**
* @var string
*/
public $maxSize;
/**
* @var float|null
*/
public $maxValue;
/**
* @param string|int|null $minSize
* @param string|int|null $maxSize
*/
public function __construct($minSize = null, $maxSize = null)
{
$this->minSize = $minSize;
$this->minValue = $minSize ? $this->toBytes($minSize) : null;
$this->maxSize = $maxSize;
$this->maxValue = $maxSize ? $this->toBytes($maxSize) : null;
}
/**
* @todo Move it to a trait
*
* @param mixed $size
*/
private function toBytes($size): float
{
$value = $size;
$units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];
foreach ($units as $exponent => $unit) {
if (!preg_match('/^(\d+(.\d+)?)'.$unit.'$/i', (string) $size, $matches)) {
continue;
}
$value = $matches[1] * 1024 ** $exponent;
break;
}
if (!is_numeric($value)) {
throw new ComponentException(sprintf('"%s" is not a recognized file size.', (string) $size));
}
return (float) $value;
}
private function isValidSize(float $size): bool
{
if ($this->minValue !== null && $this->maxValue !== null) {
return $size >= $this->minValue && $size <= $this->maxValue;
}
if ($this->minValue !== null) {
return $size >= $this->minValue;
}
return $size <= $this->maxValue;
}
/**
* {@inheritdoc}
*/
public function validate($input): bool
{
if ($input instanceof SplFileInfo) {
return $this->isValidSize($input->getSize());
}
if (is_string($input)) {
return $this->isValidSize((int) filesize($input));
}
return false;
}
}