1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
17
|
|
|
use SplFileInfo; |
18
|
|
|
use function filesize; |
19
|
|
|
use function is_numeric; |
20
|
|
|
use function is_string; |
21
|
|
|
use function preg_match; |
22
|
|
|
use function sprintf; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validate file size. |
26
|
|
|
* |
27
|
|
|
* @author Danilo Correa <[email protected]> |
28
|
|
|
* @author Henrique Moody <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class Size extends AbstractRule |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
private $minSize; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var float|null |
39
|
|
|
*/ |
40
|
|
|
private $minValue; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
private $maxSize; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var float|null |
49
|
|
|
*/ |
50
|
|
|
private $maxValue; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|int|null $minSize |
54
|
|
|
* @param string|int|null $maxSize |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct($minSize = null, $maxSize = null) |
57
|
|
|
{ |
58
|
1 |
|
$this->minSize = $minSize; |
59
|
1 |
|
$this->minValue = $minSize ? $this->toBytes($minSize) : null; |
60
|
1 |
|
$this->maxSize = $maxSize; |
61
|
1 |
|
$this->maxValue = $maxSize ? $this->toBytes($maxSize) : null; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @todo Move it to a trait |
66
|
|
|
* |
67
|
|
|
* @param mixed $size |
68
|
|
|
*/ |
69
|
1 |
|
private function toBytes($size): float |
70
|
|
|
{ |
71
|
1 |
|
$value = $size; |
72
|
1 |
|
$units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb']; |
73
|
1 |
|
foreach ($units as $exponent => $unit) { |
74
|
1 |
|
if (!preg_match('/^(\d+(.\d+)?)'.$unit.'$/i', (string) $size, $matches)) { |
75
|
1 |
|
continue; |
76
|
|
|
} |
77
|
1 |
|
$value = $matches[1] * 1024 ** $exponent; |
78
|
1 |
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
if (!is_numeric($value)) { |
82
|
1 |
|
throw new ComponentException(sprintf('"%s" is not a recognized file size.', (string) $size)); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return (float) $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
20 |
|
private function isValidSize(float $size): bool |
89
|
|
|
{ |
90
|
20 |
|
if ($this->minValue !== null && $this->maxValue !== null) { |
91
|
7 |
|
return $size >= $this->minValue && $size <= $this->maxValue; |
92
|
|
|
} |
93
|
|
|
|
94
|
14 |
|
if ($this->minValue !== null) { |
95
|
8 |
|
return $size >= $this->minValue; |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
return $size <= $this->maxValue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
21 |
|
public function validate($input): bool |
105
|
|
|
{ |
106
|
21 |
|
if ($input instanceof SplFileInfo) { |
107
|
2 |
|
return $this->isValidSize($input->getSize()); |
108
|
|
|
} |
109
|
|
|
|
110
|
19 |
|
if (is_string($input)) { |
111
|
18 |
|
return $this->isValidSize((int) filesize($input)); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return false; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|