MimeType::setValidatingData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Mezon\Security\Validators\File;
3
4
use Mezon\Security\Validators\AbstractValidator;
5
6
/**
7
 * Class MimeType
8
 *
9
 * @package Mezon
10
 * @subpackage Security
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2020/05/13)
13
 * @copyright Copyright (c) 2020, aeon.org
14
 */
15
class MimeType extends AbstractValidator
16
{
17
18
    /**
19
     * Index in the $_FILES array
20
     *
21
     * @var string
22
     */
23
    private $file = '';
24
25
    /**
26
     * Available mime types
27
     *
28
     * @var array
29
     */
30
    private $requiredTypes = [];
31
32
    /**
33
     * Constructor
34
     *
35
     * @param array $types
36
     *            mime type constraint for the file
37
     * @codeCoverageIgnore
38
     */
39
    public function __construct(array $types)
40
    {
41
        $this->requiredTypes = $types;
42
    }
43
44
    /**
45
     *
46
     * {@inheritdoc}
47
     * @see \Mezon\Security\Validators\ValidatorInterface::valid()
48
     */
49
    public function valid(): bool
50
    {
51
        $this->validateFilesFieldExists($this->file);
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
62
    {
63
        $this->file = $data;
64
    }
65
}