|
4 | 4 |
|
5 | 5 | namespace Arxy\FilesBundle\Validator\Constraint; |
6 | 6 |
|
| 7 | +use Attribute; |
7 | 8 | use Exception; |
8 | 9 | use Symfony\Component\Validator\Constraint; |
9 | 10 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
|
16 | 17 | * @Annotation |
17 | 18 | * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) |
18 | 19 | */ |
| 20 | +#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)] |
19 | 21 | class File extends Constraint |
20 | 22 | { |
21 | 23 | public ?int $maxSize = null; |
22 | | - public string $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}.'; |
| 24 | + |
23 | 25 | /** @var array<int, string> */ |
24 | 26 | public array $mimeTypes = []; |
25 | | - public string $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.'; |
26 | 27 |
|
27 | 28 | /** |
28 | | - * @param array<string, mixed> $options |
29 | | - * @param array<int, string> $groups |
30 | | - * @param mixed $payload |
| 29 | + * @param array<string>|string $mimeTypes |
| 30 | + * @param array<string>|null $groups |
31 | 31 | */ |
32 | | - public function __construct(array $options = null, array $groups = null, $payload = null) |
33 | | - { |
34 | | - if (isset($options['maxSize']) && is_string($options['maxSize'])) { |
35 | | - $options['maxSize'] = $this->normalizeBinaryFormat($options['maxSize']); |
| 32 | + public function __construct( |
| 33 | + int|string|null $maxSize, |
| 34 | + public string $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}.', |
| 35 | + array|string $mimeTypes = [], |
| 36 | + public string $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.', |
| 37 | + array $groups = null, |
| 38 | + ) { |
| 39 | + if (is_string($maxSize)) { |
| 40 | + $maxSize = $this->normalizeBinaryFormat($maxSize); |
36 | 41 | } |
| 42 | + $this->maxSize = $maxSize; |
37 | 43 |
|
38 | | - if (isset($options['mimeTypes']) && !is_array($options['mimeTypes'])) { |
39 | | - $options['mimeTypes'] = [$options['mimeTypes']]; |
| 44 | + if (!is_array($mimeTypes)) { |
| 45 | + $mimeTypes = [$mimeTypes]; |
40 | 46 | } |
41 | | - parent::__construct($options, $groups, $payload); |
| 47 | + $this->mimeTypes = $mimeTypes; |
| 48 | + parent::__construct(groups: $groups); |
42 | 49 | } |
43 | 50 |
|
44 | 51 | private function normalizeBinaryFormat(string $maxSize): int |
|
0 commit comments