1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComicVine\Api; |
4
|
|
|
|
5
|
|
|
use ComicVine\Api\Filters\FilterCheck; |
6
|
|
|
use ComicVine\Api\Filters\FilterValidation; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Check validation of inputs for ControllerQuery. |
10
|
|
|
* |
11
|
|
|
* Class Validation |
12
|
|
|
* |
13
|
|
|
* @package grzgajda/comicvine-api |
14
|
|
|
* @author Grzegorz Gajda <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Validation |
17
|
|
|
{ |
18
|
|
|
use FilterCheck; |
19
|
|
|
use FilterValidation; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Mock for enabled filters. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $enabledFilters = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Validation constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $filters |
32
|
|
|
*/ |
33
|
|
|
public function __construct($filters = []) |
34
|
|
|
{ |
35
|
|
|
if ($filters !== []) { |
36
|
|
|
$this->enabledFilters = $filters; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check validation for $input |
42
|
|
|
* |
43
|
|
|
* @param string $type |
44
|
|
|
* @param string|array $input |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function validation($type = "", $input) |
49
|
|
|
{ |
50
|
|
|
switch ($type) { |
51
|
|
|
case 'field_list': |
52
|
|
|
return $this->validFieldList($input); |
53
|
|
|
case 'limit': |
54
|
|
|
return $this->validLimit($input); |
55
|
|
|
case 'offset': |
56
|
|
|
return $this->validOffset($input); |
57
|
|
|
case 'filter': |
58
|
|
|
return $this->validFilter($input); |
59
|
|
|
case 'sort': |
60
|
|
|
return $this->validSort($input); |
61
|
|
|
default: |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validation for FIELD_LIST parameter. |
68
|
|
|
* |
69
|
|
|
* @param string|array $input |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
protected function validFieldList($input) |
74
|
|
|
{ |
75
|
|
|
if (is_array($input) === false) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($input as $key => $value) { |
80
|
|
|
if ($this->isKeyAndValueAre($key, 'int', $value, 'string') === false) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Validation for LIMIT parameter. |
90
|
|
|
* |
91
|
|
|
* @param string|array $input |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
protected function validLimit($input) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if ($this->isIntAndBetween($input, 0, 100) === false) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->isEnabledFilter('limit', $this->enabledFilters); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validation for OFFSET parameter. |
106
|
|
|
* |
107
|
|
|
* @param string|array $input |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
protected function validOffset($input) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if ($this->isIntAndBetween($input, 0) === false) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->isEnabledFilter('offset', $this->enabledFilters); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Validation for FILTER parameter. |
122
|
|
|
* |
123
|
|
|
* @param string|array $input |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
protected function validFilter($input) |
128
|
|
|
{ |
129
|
|
|
if (is_array($input) === false) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
foreach ($input as $key => $value) { |
134
|
|
|
if ($this->isKeyAndValueAre($key, 'string', $value, ['string', 'int', 'float']) === false) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->isEnabledFilter('filter', $this->enabledFilters); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Validation for SORT parameter. |
144
|
|
|
* |
145
|
|
|
* @param string|array $input |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
protected function validSort($input) |
150
|
|
|
{ |
151
|
|
|
if (is_array($input) === false) { |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach ($input as $key => $value) { |
156
|
|
|
if (is_string($key) === false) { |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
if ($value !== 'asc' && $value !== 'desc') { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->isEnabledFilter('sort', $this->enabledFilters); |
165
|
|
|
} |
166
|
|
|
} |
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.