Completed
Push — master ( 17ecb7...725fdf )
by Alexander
15:14
created

Image::init()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
nc 32
nop 0
dl 0
loc 18
cc 6
rs 9.2222
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace Yiisoft\Validator\Rule;
9
10
use yii\helpers\Yii;
0 ignored issues
show
Bug introduced by
The type yii\helpers\Yii was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use yii\http\UploadedFile;
0 ignored issues
show
Bug introduced by
The type yii\http\UploadedFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\Validator\Result;
13
14
/**
15
 * ImageValidator verifies if an attribute is receiving a valid image.
16
 */
17
class Image extends File
18
{
19
    /**
20
     * @var string the error message used when the uploaded file is not an image.
21
     * You may use the following tokens in the message:
22
     *
23
     * - {attribute}: the attribute name
24
     * - {file}: the uploaded file name
25
     */
26
    public $notImage;
27
    /**
28
     * @var int the minimum width in pixels.
29
     * Defaults to null, meaning no limit.
30
     * @see underWidth for the customized message used when image width is too small.
31
     */
32
    public $minWidth;
33
    /**
34
     * @var int the maximum width in pixels.
35
     * Defaults to null, meaning no limit.
36
     * @see overWidth for the customized message used when image width is too big.
37
     */
38
    public $maxWidth;
39
    /**
40
     * @var int the minimum height in pixels.
41
     * Defaults to null, meaning no limit.
42
     * @see underHeight for the customized message used when image height is too small.
43
     */
44
    public $minHeight;
45
    /**
46
     * @var int the maximum width in pixels.
47
     * Defaults to null, meaning no limit.
48
     * @see overWidth for the customized message used when image height is too big.
49
     */
50
    public $maxHeight;
51
    /**
52
     * @var string the error message used when the image is under [[minWidth]].
53
     * You may use the following tokens in the message:
54
     *
55
     * - {attribute}: the attribute name
56
     * - {file}: the uploaded file name
57
     * - {limit}: the value of [[minWidth]]
58
     */
59
    public $underWidth;
60
    /**
61
     * @var string the error message used when the image is over [[maxWidth]].
62
     * You may use the following tokens in the message:
63
     *
64
     * - {attribute}: the attribute name
65
     * - {file}: the uploaded file name
66
     * - {limit}: the value of [[maxWidth]]
67
     */
68
    public $overWidth;
69
    /**
70
     * @var string the error message used when the image is under [[minHeight]].
71
     * You may use the following tokens in the message:
72
     *
73
     * - {attribute}: the attribute name
74
     * - {file}: the uploaded file name
75
     * - {limit}: the value of [[minHeight]]
76
     */
77
    public $underHeight;
78
    /**
79
     * @var string the error message used when the image is over [[maxHeight]].
80
     * You may use the following tokens in the message:
81
     *
82
     * - {attribute}: the attribute name
83
     * - {file}: the uploaded file name
84
     * - {limit}: the value of [[maxHeight]]
85
     */
86
    public $overHeight;
87
88
89
    public function init(): void
90
    {
91
        parent::init();
0 ignored issues
show
introduced by
The method init() does not exist on Yiisoft\Validator\Rule\File. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        parent::/** @scrutinizer ignore-call */ 
92
                init();
Loading history...
92
93
        if ($this->notImage === null) {
94
            $this->notImage = $this->formatMessage('The file "{file}" is not an image.');
95
        }
96
        if ($this->underWidth === null) {
97
            $this->underWidth = $this->formatMessage('The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
98
        }
99
        if ($this->underHeight === null) {
100
            $this->underHeight = $this->formatMessage('The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
101
        }
102
        if ($this->overWidth === null) {
103
            $this->overWidth = $this->formatMessage('The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
104
        }
105
        if ($this->overHeight === null) {
106
            $this->overHeight = $this->formatMessage('The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
107
        }
108
    }
109
110
    public function validateValue($value): Result
111
    {
112
        $result = parent::validateValue($value);
113
114
        return empty($result) ? $this->validateImage($value) : $result;
115
    }
116
117
    /**
118
     * Validates an image file.
119
     * @param UploadedFile $image uploaded file passed to check against a set of rules
120
     * @return array|null the error message and the parameters to be inserted into the error message.
121
     * Null should be returned if the data is valid.
122
     */
123
    protected function validateImage($image)
124
    {
125
        if (false === ($imageInfo = getimagesize($image->tempFilename))) {
126
            return [$this->notImage, ['file' => $image->name]];
127
        }
128
129
        [$width, $height] = $imageInfo;
130
131
        if ($width == 0 || $height == 0) {
132
            return [$this->notImage, ['file' => $image->getClientFilename()]];
133
        }
134
135
        if ($this->minWidth !== null && $width < $this->minWidth) {
136
            return [$this->underWidth, ['file' => $image->getClientFilename(), 'limit' => $this->minWidth]];
137
        }
138
139
        if ($this->minHeight !== null && $height < $this->minHeight) {
140
            return [$this->underHeight, ['file' => $image->getClientFilename(), 'limit' => $this->minHeight]];
141
        }
142
143
        if ($this->maxWidth !== null && $width > $this->maxWidth) {
144
            return [$this->overWidth, ['file' => $image->getClientFilename(), 'limit' => $this->maxWidth]];
145
        }
146
147
        if ($this->maxHeight !== null && $height > $this->maxHeight) {
148
            return [$this->overHeight, ['file' => $image->getClientFilename(), 'limit' => $this->maxHeight]];
149
        }
150
151
        return null;
152
    }
153
}
154