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