| Total Complexity | 7 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | final class MaxsizeCheck implements UploadCheckInterface |
||
| 15 | { |
||
| 16 | |||
| 17 | |||
| 18 | private UploadedFileInterface|false $value; |
||
| 19 | private Ruleable $element; |
||
| 20 | private int|array|string $options; |
||
| 21 | |||
| 22 | 6 | public function __construct(false|UploadedFileInterface $value, Ruleable $element, int|array|string $options) |
|
| 23 | { |
||
| 24 | 6 | $this->value = $value; |
|
| 25 | 6 | $this->element = $element; |
|
| 26 | 6 | $this->options = $options; |
|
| 27 | } |
||
| 28 | |||
| 29 | 6 | public function check(): bool |
|
| 30 | { |
||
| 31 | 6 | if ($this->value === false) { |
|
| 32 | 1 | return true; |
|
| 33 | } |
||
| 34 | |||
| 35 | 5 | $parsed = $this->parseOptions($this->options); |
|
| 36 | |||
| 37 | 5 | $threshold_size = $parsed['param']; |
|
| 38 | |||
| 39 | 5 | $message = $parsed['message']; |
|
| 40 | |||
| 41 | 5 | $file_size = $this->value->getSize() ?? 0; |
|
| 42 | |||
| 43 | 5 | if (is_null($message)) { |
|
| 44 | 4 | $message = 'Размер файла (' . Binary::bytes($file_size)->format(null, " ") . ')' |
|
| 45 | 4 | . ' превышает допустимый размер: ' . Binary::bytes($threshold_size)->format(null, " "); |
|
| 46 | } |
||
| 47 | |||
| 48 | 4 | if ($file_size > $threshold_size) { |
|
| 49 | 2 | $this->element->setRuleError($message); |
|
| 50 | 2 | return false; |
|
| 51 | } |
||
| 52 | 2 | return true; |
|
| 53 | } |
||
| 54 | |||
| 55 | 5 | private function parseOptions(int|array|string $opts): array |
|
| 68 | ]; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |