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