Completed
Push — master ( 4dd6d0...6c40dd )
by Taosikai
10:58
created

src/Constraint/SizeConstraint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the slince/upload package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Upload\Constraint;
13
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
16
class SizeConstraint implements ConstraintInterface
17
{
18
    /**
19
     * Maximum file size
20
     * @var int
21
     */
22
    protected $maxSize;
23
24
    /**
25
     * Minimum file size
26
     * @var int
27
     */
28
    protected $minSize;
29
30
    /**
31
     * Limit file size (use "B", "K", M", or "G")
32
     * @param int|string|null $minSize
33
     * @param int|string|null $maxSize
34
     */
35
    public function __construct($minSize = null, $maxSize = null)
36
    {
37
        if ($minSize !== null) {
38
            $this->minSize = static::humanReadableToBytes($minSize);
39
        }
40
        if ($maxSize !== null) {
41
            $this->maxSize = static::humanReadableToBytes($maxSize);
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function validate(UploadedFile $file)
49
    {
50
        $size = method_exists($file, 'getClientSize') ?
51
            $file->getClientSize() : $file->getSize();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpFo...edFile::getClientSize() has been deprecated with message: since Symfony 4.1, use getSize() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
52
53
        return ($this->maxSize === null || $size <= $this->maxSize)
54
            && ($this->minSize === null || $size >= $this->minSize);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getErrorMessage(UploadedFile $file)
61
    {
62
        $message = null;
63
        if ($this->minSize && $this->maxSize) {
64
            $message = sprintf('File size should between %s and %s', $this->minSize, $this->maxSize);
65
        } elseif ($this->minSize === null) {
66
            $message = sprintf('File size should less than %s', $this->maxSize);
67
        } elseif ($this->maxSize === null) {
68
            $message = sprintf('File size should greater than %s', $this->minSize);
69
        }
70
        return $message;
71
    }
72
73
    /**
74
     * Convert human readable file size (e.g. "10K" or "3M") into bytes
75
     * @link https://github.com/brandonsavage/Upload/blob/master/src/Upload/File.php#L446
76
     * @param  string $input
77
     * @return int
78
     */
79
    public static function humanReadableToBytes($input)
80
    {
81
        $number = (int)$input;
82
        $units = array(
83
            'b' => 1,
84
            'k' => 1024,
85
            'm' => 1048576,
86
            'g' => 1073741824
87
        );
88
        $unit = strtolower(substr($input, -1));
89
        if (isset($units[$unit])) {
90
            $number = $number * $units[$unit];
91
        }
92
        return $number;
93
    }
94
}
95