1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_rules\Rules\File; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_rules\Interfaces\IValidateFile; |
7
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class FileMaxSize |
12
|
|
|
* @package kalanis\kw_rules\Rules\File |
13
|
|
|
* Check if input file has correct size |
14
|
|
|
*/ |
15
|
|
|
class FileMaxSize extends AFileRule |
16
|
|
|
{ |
17
|
5 |
|
public function checkValue($againstValue) |
18
|
|
|
{ |
19
|
5 |
|
return $this->fileSizeString2size($againstValue); |
20
|
|
|
} |
21
|
|
|
|
22
|
5 |
|
public function validate(IValidateFile $entry): void |
23
|
|
|
{ |
24
|
5 |
|
if ($entry->getSize() > $this->againstValue) { |
25
|
2 |
|
throw new RuleException($this->errorText); |
26
|
|
|
} |
27
|
3 |
|
} |
28
|
|
|
|
29
|
5 |
|
protected function fileSizeString2size(string $string): int |
30
|
|
|
{ |
31
|
5 |
|
$size = intval($string); |
32
|
5 |
|
$posK = stripos($string, 'k'); |
33
|
5 |
|
$posM = stripos($string, 'm'); |
34
|
5 |
|
$posG = stripos($string, 'g'); |
35
|
5 |
|
if (false !== $posK) { |
36
|
2 |
|
list($value, ) = (array) explode(strval(substr($string, $posK, 1)), $string); |
37
|
2 |
|
$value = floatval($value); |
38
|
2 |
|
$size = intval($value * 1024); |
39
|
3 |
|
} elseif (false !== $posM) { |
40
|
1 |
|
list($value, ) = (array) explode(strval(substr($string, $posM, 1)), $string); |
41
|
1 |
|
$value = floatval($value); |
42
|
1 |
|
$size = intval($value * 1024 * 1024); |
43
|
2 |
|
} elseif (false !== $posG) { |
44
|
1 |
|
list($value, ) = (array) explode(strval(substr($string, $posG, 1)), $string); |
45
|
1 |
|
$value = floatval($value); |
46
|
1 |
|
$size = intval($value * 1024 * 1024 * 1024); |
47
|
|
|
} |
48
|
5 |
|
return $size; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|