Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

ImageValidator::getClientOptions()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 39
cp 0
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 27
nc 32
nop 2
crap 42
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 yii\validators;
9
10
use Yii;
11
use yii\http\UploadedFile;
12
13
/**
14
 * ImageValidator verifies if an attribute is receiving a valid image.
15
 *
16
 * @author Taras Gudz <[email protected]>
17
 * @since 2.0
18
 */
19
class ImageValidator extends FileValidator
20
{
21
    /**
22
     * @var string the error message used when the uploaded file is not an image.
23
     * You may use the following tokens in the message:
24
     *
25
     * - {attribute}: the attribute name
26
     * - {file}: the uploaded file name
27
     */
28
    public $notImage;
29
    /**
30
     * @var int the minimum width in pixels.
31
     * Defaults to null, meaning no limit.
32
     * @see underWidth for the customized message used when image width is too small.
33
     */
34
    public $minWidth;
35
    /**
36
     * @var int the maximum width in pixels.
37
     * Defaults to null, meaning no limit.
38
     * @see overWidth for the customized message used when image width is too big.
39
     */
40
    public $maxWidth;
41
    /**
42
     * @var int the minimum height in pixels.
43
     * Defaults to null, meaning no limit.
44
     * @see underHeight for the customized message used when image height is too small.
45
     */
46
    public $minHeight;
47
    /**
48
     * @var int the maximum width in pixels.
49
     * Defaults to null, meaning no limit.
50
     * @see overWidth for the customized message used when image height is too big.
51
     */
52
    public $maxHeight;
53
    /**
54
     * @var string the error message used when the image is under [[minWidth]].
55
     * You may use the following tokens in the message:
56
     *
57
     * - {attribute}: the attribute name
58
     * - {file}: the uploaded file name
59
     * - {limit}: the value of [[minWidth]]
60
     */
61
    public $underWidth;
62
    /**
63
     * @var string the error message used when the image is over [[maxWidth]].
64
     * You may use the following tokens in the message:
65
     *
66
     * - {attribute}: the attribute name
67
     * - {file}: the uploaded file name
68
     * - {limit}: the value of [[maxWidth]]
69
     */
70
    public $overWidth;
71
    /**
72
     * @var string the error message used when the image is under [[minHeight]].
73
     * You may use the following tokens in the message:
74
     *
75
     * - {attribute}: the attribute name
76
     * - {file}: the uploaded file name
77
     * - {limit}: the value of [[minHeight]]
78
     */
79
    public $underHeight;
80
    /**
81
     * @var string the error message used when the image is over [[maxHeight]].
82
     * You may use the following tokens in the message:
83
     *
84
     * - {attribute}: the attribute name
85
     * - {file}: the uploaded file name
86
     * - {limit}: the value of [[maxHeight]]
87
     */
88
    public $overHeight;
89
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function init()
95
    {
96
        parent::init();
97
98
        if ($this->notImage === null) {
99
            $this->notImage = Yii::t('yii', 'The file "{file}" is not an image.');
100
        }
101
        if ($this->underWidth === null) {
102
            $this->underWidth = Yii::t('yii', 'The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
103
        }
104
        if ($this->underHeight === null) {
105
            $this->underHeight = Yii::t('yii', 'The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
106
        }
107
        if ($this->overWidth === null) {
108
            $this->overWidth = Yii::t('yii', 'The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
109
        }
110
        if ($this->overHeight === null) {
111
            $this->overHeight = Yii::t('yii', 'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
112
        }
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    protected function validateValue($value)
119
    {
120
        $result = parent::validateValue($value);
121
122
        return empty($result) ? $this->validateImage($value) : $result;
123
    }
124
125
    /**
126
     * Validates an image file.
127
     * @param UploadedFile $image uploaded file passed to check against a set of rules
128
     * @return array|null the error message and the parameters to be inserted into the error message.
129
     * Null should be returned if the data is valid.
130
     */
131
    protected function validateImage($image)
132
    {
133
        if (false === ($imageInfo = getimagesize($image->tempFilename))) {
134
            return [$this->notImage, ['file' => $image->name]];
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<yii\http\UploadedFile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
        }
136
137
        [$width, $height] = $imageInfo;
0 ignored issues
show
Bug introduced by
The variable $width does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $height does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
138
139
        if ($width == 0 || $height == 0) {
140
            return [$this->notImage, ['file' => $image->getClientFilename()]];
141
        }
142
143
        if ($this->minWidth !== null && $width < $this->minWidth) {
144
            return [$this->underWidth, ['file' => $image->getClientFilename(), 'limit' => $this->minWidth]];
145
        }
146
147
        if ($this->minHeight !== null && $height < $this->minHeight) {
148
            return [$this->underHeight, ['file' => $image->getClientFilename(), 'limit' => $this->minHeight]];
149
        }
150
151
        if ($this->maxWidth !== null && $width > $this->maxWidth) {
152
            return [$this->overWidth, ['file' => $image->getClientFilename(), 'limit' => $this->maxWidth]];
153
        }
154
155
        if ($this->maxHeight !== null && $height > $this->maxHeight) {
156
            return [$this->overHeight, ['file' => $image->getClientFilename(), 'limit' => $this->maxHeight]];
157
        }
158
159
        return null;
160
    }
161
}
162