Validator::contrast()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 4
nop 1
1
<?php
2
3
/**
4
 * @package Image
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\image\handlers;
11
12
/**
13
 * Image action validators for Image module
14
 */
15
class Validator
16
{
17
18
    /**
19
     * @param array $value
20
     * @return bool
21
     */
22
    public function noArguments(array $value)
23
    {
24
        return empty($value);
25
    }
26
27
    /**
28
     * @param array $value
29
     * @return bool
30
     */
31
    public function bestFit(array $value)
32
    {
33
        return count($value) == 2 && count(array_filter($value, 'ctype_digit')) == 2;
34
    }
35
36
    /**
37
     * @param array $value
38
     * @return bool
39
     */
40
    public function flip(array $value)
41
    {
42
        return count($value) == 1 && in_array($value[0], array('x', 'y', 'both'), true);
43
    }
44
45
    /**
46
     * @param array $value
47
     * @return bool
48
     */
49
    public function overlay(array $value)
50
    {
51
        if (count($value) != 5) {
52
            return false;
53
        }
54
55
        list($overlay, $anchor, $opacity, $xoffset, $yoffset) = $value;
56
57
        $positions = array('center', 'top', 'bottom', 'left', 'right',
58
            'top left', 'top right', 'bottom left', 'bottom right');
59
60
        return is_file(gplcart_file_absolute($overlay))
61
            && in_array($anchor, $positions)
62
            && (is_numeric($opacity) && (0 <= $opacity) && ($opacity <= 1))
63
            && ctype_digit($xoffset)
64
            && ctype_digit($yoffset);
65
    }
66
67
    /**
68
     * @param array $value
69
     * @return bool
70
     */
71
    public function rotate(array $value)
72
    {
73
        if (count($value) != 2) {
74
            return false;
75
        }
76
77
        list($angle, $color) = $value;
78
79
        return filter_var($angle, FILTER_VALIDATE_INT) !== false
80
            && (-360 <= $angle) && ($angle <= 360)
81
            && ($this->isHex($color) || $color === 'transparent');
82
    }
83
84
    /**
85
     * @param array $value
86
     * @return bool
87
     */
88
    public function border(array $value)
89
    {
90
        return count($value) == 2 && $this->isHex($value[0]) && ctype_digit($value[1]);
91
    }
92
93
    /**
94
     * @param array $value
95
     * @return bool
96
     */
97
    public function blur(array $value)
98
    {
99
        return count($value) == 2 && in_array($value[0], array('selective', 'gaussian')) && ctype_digit($value[1]);
100
    }
101
102
    /**
103
     * @param array $value
104
     * @return bool
105
     */
106
    public function brighten(array $value)
107
    {
108
        return count($value) == 1 && ctype_digit($value[0]) && (0 <= $value[0]) && ($value[0] <= 100);
109
    }
110
111
    /**
112
     * @param array $value
113
     * @return bool
114
     */
115
    public function contrast(array $value)
116
    {
117
        return count($value) == 1
118
            && filter_var($value[0], FILTER_VALIDATE_INT) !== false
119
            && (-100 <= $value[0]) && ($value[0] <= 100);
120
    }
121
122
    /**
123
     * @param array $value
124
     * @return bool
125
     */
126
    public function darken(array $value)
127
    {
128
        return count($value) == 1 && ctype_digit($value[0]) && (0 <= $value[0]) && ($value[0] <= 100);
129
    }
130
131
    /**
132
     * @param array $value
133
     * @return bool
134
     */
135
    public function duotone(array $value)
136
    {
137
        return count($value) == 2 && $this->isHex($value[0]) && $this->isHex($value[1]);
138
    }
139
140
    /**
141
     * @param array $value
142
     * @return bool
143
     */
144
    public function opacity(array $value)
145
    {
146
        return count($value) == 1 && is_numeric($value[0]) && (0 <= $value[0]) && ($value[0] <= 1);
147
    }
148
149
    /**
150
     * @param array $value
151
     * @return bool
152
     */
153
    public function pixelate(array $value)
154
    {
155
        return count($value) == 1 && ctype_digit($value[0]);
156
    }
157
158
    /**
159
     * @param array $value
160
     * @return bool
161
     */
162
    public function color(array $value)
163
    {
164
        return count($value) == 1 && $this->isHex($value[0]);
165
    }
166
167
    /**
168
     * Whether the value is a HEX color code
169
     * @param string $value
170
     * @return bool
171
     */
172
    protected function isHex($value)
173
    {
174
        return preg_match('/#([a-fA-F0-9]{3}){1,2}\b/', $value) === 1;
175
    }
176
177
}
178