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
|
42 |
|
public function __construct($type, $key, $required = self::OPTIONAL, $availableValues = null, $multi = false) |
44
|
|
|
{ |
45
|
42 |
|
$this->type = $type; |
46
|
42 |
|
$this->key = $key; |
47
|
42 |
|
$this->required = $required; |
48
|
42 |
|
$this->availableValues = $availableValues; |
49
|
42 |
|
$this->multi = $multi; |
50
|
42 |
|
} |
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
|
18 |
|
public function getKey() |
64
|
|
|
{ |
65
|
18 |
|
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
|
18 |
|
public function isMulti() |
85
|
|
|
{ |
86
|
18 |
|
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
|
15 |
|
public function isValid() |
97
|
|
|
{ |
98
|
15 |
|
$value = $this->getValue(); |
99
|
15 |
|
if ($this->availableValues !== null) { |
100
|
6 |
|
if (is_array($this->availableValues)) { |
101
|
3 |
|
return in_array($value, $this->availableValues); |
102
|
|
|
} |
103
|
3 |
|
} |
104
|
|
|
|
105
|
15 |
|
if ($this->required) { |
106
|
15 |
|
if ($value === null || $value == '') { |
107
|
6 |
|
return false; |
108
|
|
|
} |
109
|
9 |
|
if (is_string($this->availableValues)) { |
110
|
3 |
|
return $value == $this->availableValues; |
111
|
|
|
} |
112
|
6 |
|
} |
113
|
6 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Process environment variables like POST|GET|etc.. and return actual value |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
* |
121
|
|
|
* @throws Exception if actual InputParam has unsupported type |
122
|
|
|
*/ |
123
|
33 |
|
public function getValue() |
|
|
|
|
124
|
|
|
{ |
125
|
33 |
View Code Duplication |
if ($this->type == self::TYPE_GET) { |
|
|
|
|
126
|
12 |
|
if (!filter_has_var(INPUT_GET, $this->key) && isset($_GET[$this->key])) { |
127
|
6 |
|
return $_GET[$this->key]; |
128
|
|
|
} |
129
|
6 |
|
if ($this->isMulti()) { |
130
|
|
|
return filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
131
|
|
|
} |
132
|
6 |
|
return filter_input(INPUT_GET, $this->key); |
133
|
|
|
} |
134
|
30 |
View Code Duplication |
if ($this->type == self::TYPE_POST) { |
|
|
|
|
135
|
12 |
|
if (!filter_has_var(INPUT_POST, $this->key) && isset($_POST[$this->key])) { |
136
|
6 |
|
return $_POST[$this->key]; |
137
|
|
|
} |
138
|
9 |
|
if ($this->isMulti()) { |
139
|
|
|
return filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
140
|
|
|
} |
141
|
9 |
|
return filter_input(INPUT_POST, $this->key); |
142
|
|
|
} |
143
|
18 |
|
if ($this->type == self::TYPE_FILE) { |
144
|
9 |
|
if (isset($_FILES[$this->key])) { |
145
|
6 |
|
if ($this->isMulti()) { |
146
|
3 |
|
return $this->processMultiFileUploads($_FILES[$this->key]); |
147
|
|
|
} else { |
148
|
3 |
|
return $_FILES[$this->key]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
3 |
|
return null; |
152
|
|
|
} |
153
|
9 |
|
if ($this->type == self::TYPE_COOKIE) { |
154
|
3 |
|
if (isset($_COOKIE[$this->key])) { |
155
|
3 |
|
return $_COOKIE[$this->key]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
6 |
|
if ($this->type == self::TYPE_POST_RAW) { |
159
|
3 |
|
return file_get_contents("php://input"); |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
throw new Exception('Invalid type'); |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
private function processMultiFileUploads($files) |
166
|
|
|
{ |
167
|
3 |
|
$result = []; |
168
|
3 |
|
foreach ($files as $key => $values) { |
169
|
3 |
|
foreach ($values as $index => $value) { |
170
|
3 |
|
$result[$index][$key] = $value; |
171
|
3 |
|
} |
172
|
3 |
|
} |
173
|
3 |
|
return $result; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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: