2015-06-16 04:55:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-16 04:55:09 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2019-06-06 22:09:12 +02:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
2015-06-16 04:55:09 +02:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use SplFileInfo;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use function filesize;
|
2023-02-13 05:59:06 +01:00
|
|
|
use function floatval;
|
2019-02-17 19:41:55 +01:00
|
|
|
use function is_numeric;
|
|
|
|
use function is_string;
|
|
|
|
use function preg_match;
|
|
|
|
use function sprintf;
|
2015-06-16 04:55:09 +02:00
|
|
|
|
|
|
|
/**
|
2019-03-04 13:04:25 +01:00
|
|
|
* Validates whether the input is a file that is of a certain size or not.
|
2015-06-16 04:55:09 +02:00
|
|
|
*
|
2019-03-04 13:04:25 +01:00
|
|
|
* @author Danilo Correa <danilosilva87@gmail.com>
|
2015-06-16 04:55:09 +02:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2019-06-06 22:09:12 +02:00
|
|
|
* @author Felipe Stival <v0idpwn@gmail.com>
|
2015-06-16 04:55:09 +02:00
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
final class Size extends AbstractRule
|
2015-06-16 04:55:09 +02:00
|
|
|
{
|
|
|
|
/**
|
2020-07-22 14:02:39 +02:00
|
|
|
* @var string|int|null
|
2015-06-16 04:55:09 +02:00
|
|
|
*/
|
2023-02-18 17:38:47 +01:00
|
|
|
private $minSize;
|
2015-06-16 04:55:09 +02:00
|
|
|
|
2015-07-20 19:21:16 +02:00
|
|
|
/**
|
2019-02-04 01:12:08 +01:00
|
|
|
* @var float|null
|
2015-07-20 19:21:16 +02:00
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $minValue;
|
2015-07-20 19:21:16 +02:00
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
2020-07-22 14:02:39 +02:00
|
|
|
* @var string|int|null
|
2015-06-16 04:55:09 +02:00
|
|
|
*/
|
2023-02-18 17:38:47 +01:00
|
|
|
private $maxSize;
|
2015-06-16 04:55:09 +02:00
|
|
|
|
2015-07-20 19:21:16 +02:00
|
|
|
/**
|
2019-02-04 01:12:08 +01:00
|
|
|
* @var float|null
|
2015-07-20 19:21:16 +02:00
|
|
|
*/
|
2019-02-17 19:59:35 +01:00
|
|
|
private $maxValue;
|
2015-07-20 19:21:16 +02:00
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param string|int|null $minSize
|
|
|
|
* @param string|int|null $maxSize
|
2015-06-16 04:55:09 +02:00
|
|
|
*/
|
|
|
|
public function __construct($minSize = null, $maxSize = null)
|
|
|
|
{
|
|
|
|
$this->minSize = $minSize;
|
2023-02-18 17:38:47 +01:00
|
|
|
$this->minValue = $minSize ? $this->toBytes((string) $minSize) : null;
|
2015-06-16 04:55:09 +02:00
|
|
|
$this->maxSize = $maxSize;
|
2023-02-18 17:38:47 +01:00
|
|
|
$this->maxValue = $maxSize ? $this->toBytes((string) $maxSize) : null;
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 19:59:12 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function validate($input): bool
|
|
|
|
{
|
|
|
|
if ($input instanceof SplFileInfo) {
|
2023-02-18 17:38:47 +01:00
|
|
|
return $this->isValidSize((float) $input->getSize());
|
2019-05-11 19:59:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:09:12 +02:00
|
|
|
if ($input instanceof UploadedFileInterface) {
|
2023-02-18 17:38:47 +01:00
|
|
|
return $this->isValidSize((float) $input->getSize());
|
2019-06-06 22:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($input instanceof StreamInterface) {
|
2023-02-18 17:38:47 +01:00
|
|
|
return $this->isValidSize((float) $input->getSize());
|
2019-06-06 22:09:12 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 19:59:12 +02:00
|
|
|
if (is_string($input)) {
|
2023-02-18 17:38:47 +01:00
|
|
|
return $this->isValidSize((float) filesize($input));
|
2019-05-11 19:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-16 04:55:09 +02:00
|
|
|
/**
|
|
|
|
* @todo Move it to a trait
|
|
|
|
*
|
|
|
|
*/
|
2023-02-18 17:38:47 +01:00
|
|
|
private function toBytes(string $size): float
|
2015-06-16 04:55:09 +02:00
|
|
|
{
|
|
|
|
$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) {
|
2023-02-18 17:38:47 +01:00
|
|
|
if (!preg_match('/^(\d+(.\d+)?)' . $unit . '$/i', $size, $matches)) {
|
2015-06-16 04:55:09 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-13 05:59:06 +01:00
|
|
|
$value = floatval($matches[1]) * 1024 ** $exponent;
|
2015-06-16 04:55:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_numeric($value)) {
|
2023-02-18 17:38:47 +01:00
|
|
|
throw new ComponentException(sprintf('"%s" is not a recognized file size.', $size));
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 01:09:49 +01:00
|
|
|
return (float) $value;
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 01:09:49 +01:00
|
|
|
private function isValidSize(float $size): bool
|
2015-07-20 18:42:08 +02:00
|
|
|
{
|
2019-02-09 13:28:08 +01:00
|
|
|
if ($this->minValue !== null && $this->maxValue !== null) {
|
2016-10-30 10:39:23 +01:00
|
|
|
return $size >= $this->minValue && $size <= $this->maxValue;
|
2015-07-20 18:42:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 13:28:08 +01:00
|
|
|
if ($this->minValue !== null) {
|
2016-10-30 10:39:23 +01:00
|
|
|
return $size >= $this->minValue;
|
2015-07-20 18:42:08 +02:00
|
|
|
}
|
|
|
|
|
2016-10-30 10:39:23 +01:00
|
|
|
return $size <= $this->maxValue;
|
2015-07-20 18:42:08 +02:00
|
|
|
}
|
2015-06-16 04:55:09 +02:00
|
|
|
}
|