|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\Upload\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Enjoys\Upload\Exception\RuleException; |
|
8
|
|
|
use Enjoys\Upload\RuleInterface; |
|
9
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class MediaType implements RuleInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private array $allowedMediaType = []; |
|
16
|
|
|
private string $errorMessage; |
|
17
|
|
|
private bool $allowedAllMediaType = false; |
|
18
|
|
|
|
|
19
|
16 |
|
public function __construct(string $errorMessage = null) |
|
20
|
|
|
{ |
|
21
|
16 |
|
$this->errorMessage = $errorMessage ?? 'Media type is disallow: `%s`'; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
7 |
|
public function check(UploadedFileInterface $file): void |
|
25
|
|
|
{ |
|
26
|
7 |
|
$mediaType = $file->getClientMediaType() ?? throw new RuleException('Media Type ins null'); |
|
27
|
|
|
|
|
28
|
6 |
|
if ($this->allowedAllMediaType) { |
|
29
|
1 |
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
list($type, $subType) = $this->explode($mediaType); |
|
33
|
|
|
|
|
34
|
5 |
|
if (!array_key_exists($type, $this->allowedMediaType)){ |
|
35
|
1 |
|
throw new RuleException(sprintf($this->errorMessage, sprintf('%s/*', $type))); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** @var string|string[] $allowed */ |
|
39
|
4 |
|
$allowed = $this->allowedMediaType[$type]; |
|
40
|
|
|
|
|
41
|
4 |
|
if ($allowed === '*') { |
|
42
|
1 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** @var string[] $allowed */ |
|
46
|
3 |
|
if (!in_array($subType, $allowed, true)) { |
|
47
|
2 |
|
throw new RuleException(sprintf($this->errorMessage, $mediaType)); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
15 |
|
public function allow(string $string): MediaType |
|
52
|
|
|
{ |
|
53
|
15 |
|
if (!str_contains($string, '/')) { |
|
54
|
2 |
|
throw new RuleException(sprintf('Media Type is wrong: %s', $string)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
13 |
|
list($type, $subType) = $this->explode($string); |
|
58
|
|
|
|
|
59
|
7 |
|
if ($type === '*') { |
|
60
|
2 |
|
$this->allowedAllMediaType = true; |
|
61
|
2 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var string[]|string $allowType */ |
|
65
|
6 |
|
$allowType = $this->allowedMediaType[$type] ?? []; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
6 |
|
if ($allowType === '*') { |
|
69
|
1 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
6 |
|
if ($subType === '*') { |
|
73
|
2 |
|
$allowType = $subType; |
|
74
|
|
|
} else { |
|
75
|
5 |
|
$allowType[] = $subType; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
if (is_array($allowType)) { |
|
79
|
5 |
|
$allowType = array_unique($allowType); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
$this->allowedMediaType[$type] = $allowType; |
|
83
|
6 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
1 |
|
public function getAllowedMediaType(): array|string |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->allowedMediaType; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $string |
|
94
|
|
|
* @return string[] |
|
95
|
|
|
*/ |
|
96
|
13 |
|
private function explode(string $string): array |
|
97
|
|
|
{ |
|
98
|
13 |
|
list($type, $subType) = explode('/', trim($string)); |
|
99
|
|
|
|
|
100
|
13 |
|
if (empty($type) |
|
101
|
11 |
|
|| empty($subType) |
|
102
|
9 |
|
|| str_ends_with($type, ' ') |
|
103
|
13 |
|
|| str_starts_with($subType, ' ') |
|
104
|
|
|
) { |
|
105
|
6 |
|
throw new RuleException(sprintf('Media Type is wrong: `%s`', $string)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
7 |
|
return array($type, $subType); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|