1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tomaj\NetteApi\Params; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class InputParam implements ParamInterface |
8
|
|
|
{ |
9
|
|
|
const TYPE_POST = 'POST'; |
10
|
|
|
const TYPE_GET = 'GET'; |
11
|
|
|
const TYPE_PUT = 'PUT'; |
12
|
|
|
const TYPE_FILE = 'FILE'; |
13
|
|
|
const TYPE_COOKIE = 'COOKIE'; |
14
|
|
|
const TYPE_POST_RAW = 'POST_RAW'; |
15
|
|
|
const TYPE_POST_JSON_KEY = 'POST_JSON_KEY'; |
16
|
|
|
|
17
|
|
|
const OPTIONAL = false; |
18
|
|
|
const REQUIRED = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $required; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|null |
37
|
|
|
*/ |
38
|
|
|
private $availableValues; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $multi; |
44
|
|
|
|
45
|
48 |
|
public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
46
|
|
|
{ |
47
|
48 |
|
$this->type = $type; |
48
|
48 |
|
$this->key = $key; |
49
|
48 |
|
$this->required = $required; |
50
|
48 |
|
$this->availableValues = $availableValues; |
51
|
48 |
|
$this->multi = $multi; |
52
|
48 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
9 |
|
public function getType() |
58
|
|
|
{ |
59
|
9 |
|
return $this->type; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
21 |
|
public function getKey() |
66
|
|
|
{ |
67
|
21 |
|
return $this->key; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
6 |
|
public function isRequired() |
74
|
|
|
{ |
75
|
6 |
|
return $this->required; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
public function getAvailableValues() |
79
|
|
|
{ |
80
|
6 |
|
return $this->availableValues; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
21 |
|
public function isMulti() |
87
|
|
|
{ |
88
|
21 |
|
return $this->multi; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if actual value from environment is valid |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
* |
96
|
|
|
* @throws Exception if actual InputParam has unsupported type |
97
|
|
|
*/ |
98
|
18 |
|
public function isValid() |
99
|
|
|
{ |
100
|
18 |
|
if ($this->type == self::TYPE_POST_JSON_KEY) { |
101
|
|
|
$input = file_get_contents("php://input"); |
102
|
|
|
$params = json_decode($input, true); |
103
|
|
|
if ($input && $params === null) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
18 |
|
if ($this->required == self::OPTIONAL) { |
109
|
6 |
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
18 |
|
$value = $this->getValue(); |
113
|
18 |
|
if ($this->availableValues !== null) { |
114
|
9 |
|
if (is_array($this->availableValues)) { |
115
|
6 |
|
return empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues)); |
116
|
|
|
} |
117
|
2 |
|
} |
118
|
|
|
|
119
|
15 |
|
if ($this->required) { |
120
|
15 |
|
if ($value === null || $value == '') { |
121
|
6 |
|
return false; |
122
|
|
|
} |
123
|
9 |
|
if (is_string($this->availableValues)) { |
124
|
3 |
|
return $value == $this->availableValues; |
125
|
|
|
} |
126
|
4 |
|
} |
127
|
6 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Process environment variables like POST|GET|etc.. and return actual value |
132
|
|
|
* |
133
|
|
|
* @return mixed |
134
|
|
|
* |
135
|
|
|
* @throws Exception if actual InputParam has unsupported type |
136
|
|
|
*/ |
137
|
39 |
|
public function getValue() |
138
|
|
|
{ |
139
|
39 |
View Code Duplication |
if ($this->type == self::TYPE_GET) { |
|
|
|
|
140
|
15 |
|
if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) { |
141
|
6 |
|
return $_GET[$this->key]; |
142
|
|
|
} |
143
|
9 |
|
if ($this->isMulti()) { |
144
|
|
|
return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
145
|
|
|
} |
146
|
9 |
|
return filter_input(INPUT_GET, $this->key); |
147
|
|
|
} |
148
|
33 |
View Code Duplication |
if ($this->type == self::TYPE_POST) { |
|
|
|
|
149
|
12 |
|
if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) { |
150
|
6 |
|
return $_POST[$this->key]; |
151
|
|
|
} |
152
|
9 |
|
if ($this->isMulti()) { |
153
|
|
|
return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
154
|
|
|
} |
155
|
9 |
|
return filter_input(INPUT_POST, $this->key); |
156
|
|
|
} |
157
|
24 |
|
if ($this->type == self::TYPE_FILE) { |
158
|
9 |
|
if (isset($_FILES[$this->key])) { |
159
|
6 |
|
if ($this->isMulti()) { |
160
|
3 |
|
return $this->processMultiFileUploads($_FILES[$this->key]); |
161
|
|
|
} else { |
162
|
3 |
|
return $_FILES[$this->key]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
3 |
|
return null; |
166
|
|
|
} |
167
|
15 |
|
if ($this->type == self::TYPE_COOKIE) { |
168
|
3 |
|
if (isset($_COOKIE[$this->key])) { |
169
|
3 |
|
return $_COOKIE[$this->key]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
12 |
|
if ($this->type == self::TYPE_POST_RAW) { |
173
|
3 |
|
return file_get_contents("php://input"); |
174
|
|
|
} |
175
|
9 |
|
if ($this->type == self::TYPE_PUT) { |
176
|
6 |
|
parse_str(file_get_contents("php://input"), $params); |
177
|
6 |
|
if (isset($params[$this->key])) { |
178
|
|
|
return $params[$this->key]; |
179
|
|
|
} |
180
|
6 |
|
return ''; |
181
|
|
|
} |
182
|
3 |
|
if ($this->type == self::TYPE_POST_JSON_KEY) { |
183
|
|
|
$params = file_get_contents("php://input"); |
184
|
|
|
$params = json_decode($params, true); |
185
|
|
|
if (isset($params[$this->key])) { |
186
|
|
|
return $params[$this->key]; |
187
|
|
|
} |
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
3 |
|
throw new Exception('Invalid type'); |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
private function processMultiFileUploads($files) |
194
|
|
|
{ |
195
|
3 |
|
$result = []; |
196
|
3 |
|
foreach ($files as $key => $values) { |
197
|
3 |
|
foreach ($values as $index => $value) { |
198
|
3 |
|
$result[$index][$key] = $value; |
199
|
2 |
|
} |
200
|
2 |
|
} |
201
|
3 |
|
return $result; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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.