-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSize.php
More file actions
34 lines (26 loc) · 869 Bytes
/
Size.php
File metadata and controls
34 lines (26 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
declare(strict_types=1);
namespace Sirius\Validation\Rule\File;
use Sirius\Validation\Rule\AbstractRule;
use Sirius\Validation\Util\RuleHelper;
class Size extends AbstractRule
{
const OPTION_SIZE = 'size';
const MESSAGE = 'The file should not exceed {size}';
const LABELED_MESSAGE = '{label} should not exceed {size}';
protected array $options = [
self::OPTION_SIZE => '2M'
];
public function validate(mixed $value, ?string $valueIdentifier = null): bool
{
$this->value = $value;
if (!file_exists($value)) {
$this->success = false;
} else {
$fileSize = @filesize($value);
$limit = RuleHelper::normalizeFileSize($this->options[self::OPTION_SIZE]);
$this->success = $fileSize && $fileSize <= $limit;
}
return $this->success;
}
}