Test Failed
Pull Request — master (#490)
by
unknown
03:38
created

File::getHandlerClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\SerializableRuleInterface;
10
use Yiisoft\Validator\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
14
use Yiisoft\Validator\ValidationContext;
15
16
17
/**
18
 * Checks if the value is a valid IPv4/IPv6 address or subnet.
19
 *
20
 * It also may change the value if normalization of IPv6 expansion is enabled.
21
 */
22
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
23
final class File implements SerializableRuleInterface, BeforeValidationInterface
24
{
25
    use BeforeValidationTrait;
26
    use RuleNameTrait;
27
28
    /**
29
     * @var array|string|null a list of file name extensions that are allowed to be uploaded.
30
     * This can be either an array or a string consisting of file extension names
31
     * separated by space or comma (e.g. "gif, jpg").
32
     * Extension names are case-insensitive. Defaults to null, meaning all file name
33
     * extensions are allowed.
34
     * @see wrongExtension for the customized message for wrong file type.
35
     */
36
    public $extensions;
37
    /**
38
     * @var bool whether to check file type (extension) with mime-type. If extension produced by
39
     * file mime-type check differs from uploaded file extension, the file will be considered as invalid.
40
     */
41
    public $checkExtensionByMimeType = true;
42
    /**
43
     * @var array|string|null a list of file MIME types that are allowed to be uploaded.
44
     * This can be either an array or a string consisting of file MIME types
45
     * separated by space or comma (e.g. "text/plain, image/png").
46
     * The mask with the special character `*` can be used to match groups of mime types.
47
     * For example `image/*` will pass all mime types, that begin with `image/` (e.g. `image/jpeg`, `image/png`).
48
     * Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed.
49
     * @see wrongMimeType for the customized message for wrong MIME type.
50
     */
51
    public $mimeTypes;
52
    /**
53
     * @var int|null the minimum number of bytes required for the uploaded file.
54
     * Defaults to null, meaning no limit.
55
     * @see tooSmall for the customized message for a file that is too small.
56
     */
57
    public $minSize;
58
    /**
59
     * @var int|null the maximum number of bytes required for the uploaded file.
60
     * Defaults to null, meaning no limit.
61
     * Note, the size limit is also affected by `upload_max_filesize` and `post_max_size` INI setting
62
     * and the 'MAX_FILE_SIZE' hidden field value. See [[getSizeLimit()]] for details.
63
     * @see https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
64
     * @see https://www.php.net/post-max-size
65
     * @see getSizeLimit
66
     * @see tooBig for the customized message for a file that is too big.
67
     */
68
    public $maxSize;
69
    /**
70
     * @var int the maximum file count the given attribute can hold.
71
     * Defaults to 1, meaning single file upload. By defining a higher number,
72
     * multiple uploads become possible. Setting it to `0` means there is no limit on
73
     * the number of files that can be uploaded simultaneously.
74
     *
75
     * > Note: The maximum number of files allowed to be uploaded simultaneously is
76
     * also limited with PHP directive `max_file_uploads`, which defaults to 20.
77
     *
78
     * @see https://www.php.net/manual/en/ini.core.php#ini.max-file-uploads
79
     * @see tooMany for the customized message when too many files are uploaded.
80
     */
81
    public $maxFiles = 1;
82
    /**
83
     * @var int the minimum file count the given attribute can hold.
84
     * Defaults to 0. Higher value means at least that number of files should be uploaded.
85
     *
86
     * @see tooFew for the customized message when too few files are uploaded.
87
     * @since 2.0.14
88
     */
89
    public $minFiles = 0;
90
    /**
91
     * @var string the error message used when a file is not uploaded correctly.
92
     */
93
    public $message;
94
    /**
95
     * @var string the error message used when no file is uploaded.
96
     * Note that this is the text of the validation error message. To make uploading files required,
97
     * you have to set [[skipOnEmpty]] to `false`.
98
     */
99
    public $uploadRequired;
100
    public $uploadErrorPartial;
101
    /**
102
     * @var string the error message used when the uploaded file is too large.
103
     * You may use the following tokens in the message:
104
     *
105
     * - {attribute}: the attribute name
106
     * - {file}: the uploaded file name
107
     * - {limit}: the maximum size allowed (see [[getSizeLimit()]])
108
     * - {formattedLimit}: the maximum size formatted
109
     *   with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]]
110
     */
111
    public $tooBig;
112
    /**
113
     * @var string the error message used when the uploaded file is too small.
114
     * You may use the following tokens in the message:
115
     *
116
     * - {attribute}: the attribute name
117
     * - {file}: the uploaded file name
118
     * - {limit}: the value of [[minSize]]
119
     * - {formattedLimit}: the value of [[minSize]] formatted
120
     *   with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]
121
     */
122
    public $tooSmall;
123
    /**
124
     * @var string the error message used if the count of multiple uploads exceeds limit.
125
     * You may use the following tokens in the message:
126
     *
127
     * - {attribute}: the attribute name
128
     * - {limit}: the value of [[maxFiles]]
129
     */
130
    public $tooMany;
131
    /**
132
     * @var string the error message used if the count of multiple uploads less that minFiles.
133
     * You may use the following tokens in the message:
134
     *
135
     * - {attribute}: the attribute name
136
     * - {limit}: the value of [[minFiles]]
137
     *
138
     * @since 2.0.14
139
     */
140
    public $tooFew;
141
    /**
142
     * @var string the error message used when the uploaded file has an extension name
143
     * that is not listed in [[extensions]]. You may use the following tokens in the message:
144
     *
145
     * - {attribute}: the attribute name
146
     * - {file}: the uploaded file name
147
     * - {extensions}: the list of the allowed extensions.
148
     */
149
    public $wrongExtension;
150
    /**
151
     * @var string the error message used when the file has an mime type
152
     * that is not allowed by [[mimeTypes]] property.
153
     * You may use the following tokens in the message:
154
     *
155
     * - {attribute}: the attribute name
156
     * - {file}: the uploaded file name
157
     * - {mimeTypes}: the value of [[mimeTypes]]
158
     */
159
    public $wrongMimeType;
160
161
    public function __construct(
162
        private bool $skipOnEmpty = true,
163
        private bool $skipOnError = true,
164
        /**
165
         * @var Closure(mixed, ValidationContext):bool|null
166
         */
167
        private ?Closure $when = null,
168
    ) {
169
        if ($this->message === null) {
170
            $this->message = 'File upload failed.';
171
        }
172
        if ($this->uploadRequired === null) {
173
            $this->uploadRequired = 'Please upload a file. {value}';
174
        }
175
        if ($this->tooMany === null) {
176
            $this->tooMany = 'You can upload at most {limit, number} {limit, plural, one{file} other{files}}.';
177
        }
178
        if ($this->tooFew === null) {
179
            $this->tooFew = 'You should upload at least {limit, number} {limit, plural, one{file} other{files}}.';
180
        }
181
        if ($this->wrongExtension === null) {
182
            $this->wrongExtension = 'Only files with these extensions are allowed: {extensions}.';
183
        }
184
        if ($this->tooBig === null) {
185
            $this->tooBig = 'The file "{file}" is too big. Its size cannot exceed {formattedLimit}.';
186
        }
187
        if ($this->tooSmall === null) {
188
            $this->tooSmall = 'The file "{file}" is too small. Its size cannot be smaller than {formattedLimit}.';
189
        }
190
        if (!is_array($this->extensions)) {
191
            $this->extensions = preg_split('/[\s,]+/', strtolower((string)$this->extensions), -1, PREG_SPLIT_NO_EMPTY);
192
        } else {
193
            $this->extensions = array_map('strtolower', $this->extensions);
194
        }
195
        if ($this->wrongMimeType === null) {
196
            $this->wrongMimeType = 'Only files with these MIME types are allowed: {mimeTypes}.';
197
        }
198
        if (!is_array($this->mimeTypes)) {
199
            $this->mimeTypes = preg_split('/[\s,]+/', strtolower((string)$this->mimeTypes), -1, PREG_SPLIT_NO_EMPTY);
200
        } else {
201
            $this->mimeTypes = array_map('strtolower', $this->mimeTypes);
202
        }
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getMessage(): string
209
    {
210
        return $this->message;
211
    }
212
213
    public function getOptions(): array
214
    {
215
        return [
216
            'skipOnEmpty' => $this->skipOnEmpty,
217
            'skipOnError' => $this->skipOnError,
218
        ];
219
    }
220
221
    public function getHandlerClassName(): string
222
    {
223
        return FileHandler::class;
224
    }
225
}
226