Total Complexity | 4 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
13 | class MimeType implements \Mezon\Security\Validators\ValidatorInterface |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Index in the $_FILES array |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | private $file = ''; |
||
22 | |||
23 | /** |
||
24 | * Available mime types |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $requiredTypes = []; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param array $types |
||
34 | * mime type constraint for the file |
||
35 | * @codeCoverageIgnore |
||
36 | */ |
||
37 | public function __construct(array $types) |
||
38 | { |
||
39 | $this->requiredTypes = $types; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * {@inheritdoc} |
||
45 | * @see \Mezon\Security\Validators\ValidatorInterface::valid() |
||
46 | */ |
||
47 | public function valid(): bool |
||
48 | { |
||
49 | if (isset($_FILES[$this->file]) === false) { |
||
50 | throw (new \Exception('The index "' . $this->file . '" was not found in the $_FILES array', - 1)); |
||
51 | } |
||
52 | |||
53 | return in_array(mime_content_type($_FILES[$this->file]['tmp_name']), $this->requiredTypes); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | * {@inheritdoc} |
||
59 | * @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData() |
||
60 | */ |
||
61 | public function setValidatingData($data): void |
||
64 | } |
||
65 | } |