Size::valid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Mezon\Security\Validators\File;
3
4
use Mezon\Security\Validators\AbstractValidator;
5
6
/**
7
 * Class Size
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 Size extends AbstractValidator
16
{
17
18
    /**
19
     * Bytes in KB
20
     *
21
     * @var integer
22
     */
23
    public const KB = 1024;
24
25
    /**
26
     * Bytes in MB
27
     *
28
     * @var integer
29
     */
30
    public const MB = 1048576;
31
32
    /**
33
     * Bytes in GB
34
     *
35
     * @var integer
36
     */
37
    public const GB = 1073741824;
38
39
    /**
40
     * Index in the $_FILES array
41
     *
42
     * @var string
43
     */
44
    private $file = '';
45
46
    /**
47
     * Required size in bytes
48
     *
49
     * @var integer
50
     */
51
    private $requiredSize = 0;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param int $size
57
     *            size constraint for the file
58
     * @codeCoverageIgnore
59
     */
60
    public function __construct(int $size)
61
    {
62
        $this->requiredSize = $size;
63
    }
64
65
    /**
66
     *
67
     * {@inheritdoc}
68
     * @see \Mezon\Security\Validators\ValidatorInterface::valid()
69
     */
70
    public function valid(): bool
71
    {
72
        $this->validateFilesFieldExists($this->file);
73
74
        return $_FILES[$this->file]['size'] <= $this->requiredSize;
75
    }
76
77
    /**
78
     *
79
     * {@inheritdoc}
80
     * @see \Mezon\Security\Validators\ValidatorInterface::setValidatingData()
81
     */
82
    public function setValidatingData($data): void
83
    {
84
        $this->file = $data;
85
    }
86
}