1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace projectcleverweb\color; |
4
|
|
|
|
5
|
|
|
class validate { |
6
|
|
|
|
7
|
|
|
public static function hex($input) { |
8
|
|
|
return (is_string($input) && preg_match('/\A\#?([0-9a-f]{6}|[0-9a-f]{3})\Z/i', $input)); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
View Code Duplication |
public static function rgb($input) { |
|
|
|
|
12
|
|
|
if ( |
13
|
|
|
is_array($input) |
14
|
|
|
&& static::has_keys($input, ['r', 'g', 'b']) |
15
|
|
|
&& static::in_range($input['r'], 0, 255) |
16
|
|
|
&& static::in_range($input['g'], 0, 255) |
17
|
|
|
&& static::in_range($input['b'], 0, 255) |
18
|
|
|
) { |
19
|
|
|
return TRUE; |
20
|
|
|
} |
21
|
|
|
return FALSE; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public static function hsl($input) { |
|
|
|
|
25
|
|
|
if ( |
26
|
|
|
is_array($input) |
27
|
|
|
&& static::has_keys($input, ['h', 's', 'l']) |
28
|
|
|
&& static::in_range($input['h'], 0, 359) |
29
|
|
|
&& static::in_range($input['s'], 0, 100) |
30
|
|
|
&& static::in_range($input['l'], 0, 100) |
31
|
|
|
) { |
32
|
|
|
return TRUE; |
33
|
|
|
} |
34
|
|
|
return FALSE; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public static function hsb($input) { |
|
|
|
|
38
|
|
|
if ( |
39
|
|
|
is_array($input) |
40
|
|
|
&& static::has_keys($input, ['h', 's', 'b']) |
41
|
|
|
&& static::in_range($input['h'], 0, 359) |
42
|
|
|
&& static::in_range($input['s'], 0, 100) |
43
|
|
|
&& static::in_range($input['b'], 0, 100) |
44
|
|
|
) { |
45
|
|
|
return TRUE; |
46
|
|
|
} |
47
|
|
|
return FALSE; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function cmyk($input) { |
51
|
|
|
if ( |
52
|
|
|
is_array($input) |
53
|
|
|
&& static::has_keys($input, ['c', 'm', 'y', 'k']) |
54
|
|
|
&& static::in_range($input['c'], 0, 100) |
55
|
|
|
&& static::in_range($input['m'], 0, 100) |
56
|
|
|
&& static::in_range($input['y'], 0, 100) |
57
|
|
|
&& static::in_range($input['k'], 0, 100) |
58
|
|
|
) { |
59
|
|
|
return TRUE; |
60
|
|
|
} |
61
|
|
|
return FALSE; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function css($input) { |
65
|
|
|
return (is_string($input) && preg_match('/\A(?:(rgb|hsl)\((\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+)|(rgb|hsl)a\((\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),(\d+|\d+\.(?:\d+)?|\.\d+),((?:0|1)|(?:0|1)\.(?:\d+)?|\.\d+))\)\Z/i', $input)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function has_keys(array $array, array $keys) { |
69
|
|
|
$result = TRUE; |
70
|
|
|
foreach ($keys as $key) { |
71
|
|
|
if (!isset($array[$key]) || !is_numeric($array[$key])) { |
72
|
|
|
$result = FALSE; |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function in_range(float $value, float $min, float $max) :bool { |
80
|
|
|
return ($value >= $min && $value <= $max); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function in_between(float $value, float $min, float $max) :bool { |
84
|
|
|
return ($value > $min && $value < $max); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.