1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Scrawler package. |
4
|
|
|
* |
5
|
|
|
* (c) Pranjal Pandey <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Scrawler\Validator\Storage; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
15
|
|
|
|
16
|
|
|
abstract class AbstractValidator |
17
|
|
|
{ |
18
|
|
|
protected int $maxSize = 0; |
19
|
|
|
|
20
|
|
|
/* |
21
|
|
|
* Get processed file content. |
22
|
|
|
*/ |
23
|
|
|
public function getProcessedContent(UploadedFile|File $file): string |
24
|
|
|
{ |
25
|
|
|
return $file->getContent(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function maxSize(int $maxSize): void |
29
|
|
|
{ |
30
|
|
|
$this->maxSize = $this->maxSize > $maxSize || 0 == $this->maxSize ? $maxSize : $this->maxSize; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Basic Validate the uploaded file. |
35
|
|
|
* |
36
|
|
|
* @throws \Scrawler\Exception\FileValidationException |
37
|
|
|
*/ |
38
|
|
|
public function runValidate(UploadedFile|File $file): void |
39
|
|
|
{ |
40
|
|
|
// @codeCoverageIgnoreStart |
41
|
|
|
if ($file instanceof UploadedFile) { |
42
|
|
|
if (!$file->isValid()) { |
43
|
|
|
throw new \Scrawler\Exception\FileValidationException($file->getErrorMessage()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (\UPLOAD_ERR_OK !== $file->getError()) { |
47
|
|
|
throw new \Scrawler\Exception\FileValidationException($file->getErrorMessage()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
// @codeCoverageIgnoreEnd |
51
|
|
|
if (0 === $this->maxSize) { |
52
|
|
|
$this->maxSize = (int) UploadedFile::getMaxFilesize(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($file->getSize() > $this->maxSize) { |
56
|
|
|
throw new \Scrawler\Exception\FileValidationException('File size size too large.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->validate($file); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Validate the uploaded file. |
64
|
|
|
* |
65
|
|
|
* @throws \Scrawler\Exception\FileValidationException |
66
|
|
|
*/ |
67
|
|
|
abstract public function validate(UploadedFile|File $file): void; |
68
|
|
|
} |
69
|
|
|
|