|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Contact; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Yiisoft\Validator\Exception\UnexpectedRuleException; |
|
10
|
|
|
use Yiisoft\Validator\Formatter; |
|
11
|
|
|
use Yiisoft\Validator\FormatterInterface; |
|
12
|
|
|
use Yiisoft\Validator\Result; |
|
13
|
|
|
use Yiisoft\Validator\RuleHandlerInterface; |
|
14
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Validates that the value is a valid HTTP or HTTPS URL. |
|
19
|
|
|
* |
|
20
|
|
|
* Note that this rule only checks if the URL scheme and host part are correct. |
|
21
|
|
|
* It does not check the remaining parts of a URL. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class FileHandler implements RuleHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
private FormatterInterface $formatter; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(?FormatterInterface $formatter = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->formatter = $formatter ?? new Formatter(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function validate(mixed $value, object $rule, ValidationContext $context): Result |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$rule instanceof File) { |
|
35
|
|
|
throw new UnexpectedRuleException(File::class, $rule); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!is_array($value)) { |
|
39
|
|
|
throw new RuntimeException('Value should be array of UploadedFileInterface'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
foreach ($value as $attachFiles) { |
|
43
|
|
|
foreach ($attachFiles as $file) { |
|
44
|
|
|
if (!$file instanceof UploadedFileInterface) { |
|
45
|
|
|
throw new RuntimeException('File should be UploadedFileInterface'); |
|
46
|
|
|
} elseif ($file->getError() == UPLOAD_ERR_NO_FILE) { |
|
47
|
|
|
$formattedMessage = $this->formatter->format( |
|
48
|
|
|
$rule->uploadRequired, |
|
49
|
|
|
['attribute' => $context->getAttribute(), 'value' => $file] |
|
50
|
|
|
); |
|
51
|
|
|
} else { |
|
52
|
|
|
$error = $file->getError(); |
|
53
|
|
|
if ($error == UPLOAD_ERR_OK) { |
|
54
|
|
|
if ($rule->maxSize !== null && $file->getSize() > $rule->getSizeLimit()) { |
|
|
|
|
|
|
55
|
|
|
$formattedMessage = $this->formatter->format( |
|
56
|
|
|
$rule->tooBig, |
|
57
|
|
|
[ |
|
58
|
|
|
'file' => $file->getClientFilename(), |
|
59
|
|
|
'limit' => $rule->getSizeLimit(), |
|
60
|
|
|
'formattedLimit' => $this->formatter->asShortSize($rule->getSizeLimit()), |
|
|
|
|
|
|
61
|
|
|
], |
|
62
|
|
|
); |
|
63
|
|
|
} elseif ($rule->minSize !== null && $file->getSize() < $rule->minSize) { |
|
64
|
|
|
$formattedMessage = $this->formatter->format( |
|
65
|
|
|
$rule->tooSmall, |
|
66
|
|
|
[ |
|
67
|
|
|
'file' => $file->getClientFilename(), |
|
68
|
|
|
'limit' => $rule->minSize, |
|
69
|
|
|
'formattedLimit' => Yii::$app->formatter->asShortSize($rule->minSize), |
|
|
|
|
|
|
70
|
|
|
], |
|
71
|
|
|
); |
|
72
|
|
|
} elseif (!empty($rule->extensions) && !$this->validateExtension($file)) { |
|
|
|
|
|
|
73
|
|
|
$formattedMessage = $this->formatter->format( |
|
74
|
|
|
$rule->wrongExtension, |
|
75
|
|
|
['file' => $file->name, 'extensions' => implode(', ', $rule->extensions)], |
|
|
|
|
|
|
76
|
|
|
); |
|
77
|
|
|
} elseif (!empty($rule->mimeTypes) && !$this->validateMimeType($file)) { |
|
|
|
|
|
|
78
|
|
|
$formattedMessage = $this->formatter->format( |
|
79
|
|
|
$rule->wrongMimeType, |
|
80
|
|
|
['file' => $file->name, 'mimeTypes' => implode(', ', $rule->mimeTypes)], |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} elseif ($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE) { |
|
84
|
|
|
$formattedMessage = $this->formatter->format( |
|
85
|
|
|
$rule->tooBig, |
|
86
|
|
|
[ |
|
87
|
|
|
'file' => $file->name, |
|
88
|
|
|
'limit' => $rule->getSizeLimit(), |
|
89
|
|
|
'formattedLimit' => Yii::$app->formatter->asShortSize($rule->getSizeLimit()), |
|
90
|
|
|
], |
|
91
|
|
|
); |
|
92
|
|
|
} elseif ($error == UPLOAD_ERR_PARTIAL) { |
|
93
|
|
|
$formattedMessage = $this->formatter->format( |
|
94
|
|
|
$rule->uploadErrorPartial, |
|
95
|
|
|
['attribute' => $context->getAttribute(), 'value' => $file] |
|
96
|
|
|
); |
|
97
|
|
|
} elseif ($error == UPLOAD_ERR_NO_TMP_DIR) { |
|
98
|
|
|
throw new RuntimeException('Missing the temporary folder to store the uploaded file'); |
|
99
|
|
|
} elseif ($error == UPLOAD_ERR_CANT_WRITE) { |
|
100
|
|
|
throw new RuntimeException('Failed to write the uploaded file to disk'); |
|
101
|
|
|
} elseif ($error == UPLOAD_ERR_EXTENSION) { |
|
102
|
|
|
throw new RuntimeException('File upload was stopped by some PHP extension'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$result = new Result(); |
|
109
|
|
|
if (isset($formattedMessage)) { |
|
110
|
|
|
$result->addError($formattedMessage); |
|
111
|
|
|
} |
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.