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