Validator::validateNonNegativeInteger()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Barryvanveen\CCA\Config;
6
7
use Barryvanveen\CCA\Exceptions\InvalidOptionException;
8
use Barryvanveen\CCA\Exceptions\InvalidValueException;
9
use Phim\Color\RgbColor;
10
11
class Validator
12
{
13 93
    public static function validate(array $config): bool
14
    {
15 93
        foreach ($config as $option => $value) {
16 93
            if (!in_array($option, Options::VALID_OPTIONS)) {
17 3
                throw new InvalidOptionException("Invalid option ".$option.".");
18
            }
19
20 90
            $methodName = 'validate'.ucwords($option, '_');
21
            {
22
            }
23 90
            if (!self::$methodName($value)) {
24 90
                throw new InvalidValueException("Invalid value for option ".$option.".");
25
            }
26
        }
27
28 33
        return true;
29
    }
30
31
    /**
32
     * Validate whether the columns option is a positive integer.
33
     *
34
     * @param $value
35
     *
36
     * @return bool
37
     */
38 12
    protected static function validateColumns($value): bool
39
    {
40 12
        return self::validateNonNegativeInteger($value);
41
    }
42
43 57
    protected static function validateNonNegativeInteger($value): bool
44
    {
45 57
        if (!is_int($value)) {
46 18
            return false;
47
        }
48
49 39
        if ($value <= 0) {
50 18
            return false;
51
        }
52
53 21
        return true;
54
    }
55
56
    /**
57
     * Validate whether the imageCellSize option is a positive integer.
58
     *
59
     * @param $value
60
     *
61
     * @return bool
62
     */
63 9
    protected static function validateImageCellSize($value): bool
64
    {
65 9
        return self::validateNonNegativeInteger($value);
66
    }
67
68
    /**
69
     * Validate whether the imageColors option is an array of RgbColor instances.
70
     *
71
     * @param $value
72
     *
73
     * @return bool
74
     */
75 9
    protected static function validateImageColors($value): bool
76
    {
77 9
        if (!is_array($value)) {
78 3
            return false;
79
        }
80
81 6
        foreach ($value as $color) {
82 6
            if (!$color instanceof RgbColor) {
83 6
                return false;
84
            }
85
        }
86
87 3
        return true;
88
    }
89
90
    /**
91
     * Validate whether imageHue is an integer between 0 and 360.
92
     *
93
     * @param $value
94
     *
95
     * @return bool
96
     */
97 12
    protected static function validateImageHue($value): bool
98
    {
99 12
        if (!is_int($value)) {
100 3
            return false;
101
        }
102
103 9
        if ($value < 0 || $value > 360) {
104 6
            return false;
105
        }
106
107 3
        return true;
108
    }
109
110
    /**
111
     * Validate whether the neighborhoodSize option is a positive integer.
112
     *
113
     * @param $value
114
     *
115
     * @return bool
116
     */
117 9
    protected static function validateNeighborhoodSize($value): bool
118
    {
119 9
        return self::validateNonNegativeInteger($value);
120
    }
121
122
    /**
123
     * Validate whether the neighborhoodType option is a valid option.
124
     *
125
     * @param $value
126
     *
127
     * @return bool
128
     */
129 6
    protected static function validateNeighborhoodType($value): bool
130
    {
131 6
        if (!in_array($value, NeighborhoodOptions::NEIGHBORHOOD_TYPES)) {
132 3
            return false;
133
        }
134
135 3
        return true;
136
    }
137
138
    /**
139
     * Validate whether the rows option is a positive integer.
140
     *
141
     * @param $value
142
     *
143
     * @return bool
144
     */
145 12
    protected static function validateRows($value): bool
146
    {
147 12
        return self::validateNonNegativeInteger($value);
148
    }
149
150
    /**
151
     * Validate whether the seed option is an integer.
152
     *
153
     * @param $value
154
     *
155
     * @return bool
156
     */
157 6
    protected static function validateSeed($value): bool
158
    {
159 6
        return is_int($value);
160
    }
161
162
    /**
163
     * Validate whether the states option is a positive integer.
164
     *
165
     * @param $value
166
     *
167
     * @return bool
168
     */
169 9
    protected static function validateStates($value): bool
170
    {
171 9
        return self::validateNonNegativeInteger($value);
172
    }
173
174
    /**
175
     * Validate whether the threshold option is a positive integer.
176
     *
177
     * @param $value
178
     *
179
     * @return bool
180
     */
181 9
    protected static function validateThreshold($value): bool
182
    {
183 9
        return self::validateNonNegativeInteger($value);
184
    }
185
}
186