-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInput.php
More file actions
281 lines (243 loc) · 7.25 KB
/
Input.php
File metadata and controls
281 lines (243 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
namespace Gt\Input;
use ArrayAccess;
use Countable;
use Gt\Json\JsonDecodeException;
use Gt\Json\JsonObject;
use Gt\Json\JsonObjectBuilder;
use Iterator;
use Psr\Http\Message\StreamInterface;
use Gt\Input\Trigger\Trigger;
use Gt\Input\InputData\InputData;
use Gt\Input\InputData\Datum\InputDatum;
use Gt\Input\InputData\KeyValueArrayAccess;
use Gt\Input\InputData\KeyValueCountable;
use Gt\Input\InputData\KeyValueIterator;
use Gt\Input\InputData\BodyInputData;
use Gt\Input\InputData\CombinedInputData;
use Gt\Input\InputData\FileUploadInputData;
use Gt\Input\InputData\QueryStringInputData;
/**
* @implements ArrayAccess<string, ?string>
* @implements Iterator<string, ?string>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class Input implements ArrayAccess, Countable, Iterator {
use InputValueGetter;
use KeyValueArrayAccess;
use KeyValueCountable;
use KeyValueIterator;
const DATA_QUERYSTRING = "get";
const DATA_BODY = "post";
const DATA_FILES = "files";
const DATA_COMBINED = "combined";
protected BodyStream $bodyStream;
protected QueryStringInputData $queryStringParameters;
protected BodyInputData $bodyParameters;
/**
* @param array<string, string> $get
* @param array<string, string> $post
* @param array<string, array<int|string, string|array<int|string>>> $files
* @param string $bodyPath
*/
public function __construct(
array $get = [],
array $post = [],
array $files = [],
string $bodyPath = "php://input",
) {
$this->bodyStream = new BodyStream($bodyPath);
$this->queryStringParameters = new QueryStringInputData($get);
$this->bodyParameters = new BodyInputData($post);
$this->fileUploadParameters = new FileUploadInputData($files);
$this->parameters = new CombinedInputData(
$this->queryStringParameters,
$this->bodyParameters,
$this->fileUploadParameters
);
}
/**
* Returns the input payload as a streamable HTTP request body.
*/
public function getStream():StreamInterface {
return $this->bodyStream;
}
public function add(string $key, InputDatum $datum, string $method):void {
switch($method) {
case self::DATA_QUERYSTRING:
$this->queryStringParameters =
$this->queryStringParameters->withKeyValue(
$key,
$datum
);
break;
case self::DATA_BODY:
$this->bodyParameters =
$this->bodyParameters->withKeyValue(
$key,
$datum
);
break;
case self::DATA_FILES:
$this->fileUploadParameters =
$this->fileUploadParameters->withKeyValue(
$key,
$datum
);
break;
default:
throw new InvalidInputMethodException($method);
}
$this->parameters = new CombinedInputData(
$this->queryStringParameters,
$this->bodyParameters,
$this->fileUploadParameters
);
}
/**
* Get a particular input value by its key. To specify either GET or
* POST variables, pass Input::METHOD_GET or Input::METHOD_POST as the
* second parameter (defaults to Input::METHOD_BOTH).
*/
public function get(string $key, ?string $method = null):mixed {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}
$data = match($method) {
self::DATA_QUERYSTRING => $this->queryStringParameters->get($key),
self::DATA_BODY => $this->bodyParameters->get($key),
self::DATA_FILES => $this->fileUploadParameters->getFile($key),
self::DATA_COMBINED => $this->parameters->get($key),
default => throw new InvalidInputMethodException($method),
};
return $data?->getValue();
}
/**
* Does the input contain the specified key?
*/
public function contains(string $key, ?string $method = null):bool {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}
switch($method) {
case self::DATA_QUERYSTRING:
$isset = $this->containsQueryStringParameter($key);
break;
case self::DATA_BODY:
$isset =$this->containsBodyParameter($key);
break;
case self::DATA_FILES:
$isset =$this->containsFile($key);
break;
case self::DATA_COMBINED:
$isset = isset($this->parameters[$key]);
break;
default:
throw new InvalidInputMethodException($method);
}
return $isset;
}
public function containsQueryStringParameter(string $key):bool {
return isset($this->queryStringParameters[$key]);
}
public function containsBodyParameter(string $key):bool {
return isset($this->bodyParameters[$key]);
}
public function containsFile(string $key):bool {
return isset($this->fileUploadParameters[$key]);
}
/**
* Get an InputData object containing all request variables. To specify
* only GET or POST variables, pass Input::METHOD_GET
* or Input::METHOD_POST.
*/
public function getAll(?string $method = null):InputData {
if(is_null($method)) {
$method = self::DATA_COMBINED;
}
switch($method) {
case self::DATA_QUERYSTRING:
return $this->queryStringParameters;
case self::DATA_BODY:
return $this->bodyParameters;
case self::DATA_FILES:
return $this->fileUploadParameters;
case self::DATA_COMBINED:
return $this->parameters;
default:
throw new InvalidInputMethodException($method);
}
}
public function getBodyJson():?JsonObject {
$jsonBuilder = new JsonObjectBuilder();
try {
return $jsonBuilder->fromJsonString($this->bodyStream->getContents());
}
catch(JsonDecodeException) {
return null;
}
}
/**
* Return a "do" Trigger, matching when a request variable is present with the
* provided $match value.
*/
public function do(string $match):Trigger {
return $this->when(["do" => $match]);
}
/**
* Return a Trigger, firing when one or more request variables are
* present with the provided key value pair(s) are present.
*
* $matches is an associative array, where the key is a request
* variable's name and the value is the request variable's value
* to match.
*
* @param array<string, string>|string $matches
*/
public function when(array|string...$matches):Trigger {
$trigger = new Trigger($this);
$trigger->when($matches);
return $trigger;
}
/**
* Return a Trigger that will only pass the provided keys to its callback.
*/
public function select(string...$keys):Trigger {
foreach($keys as $key) {
if(!$this->parameters->contains($key)) {
throw new MissingInputParameterException($key);
}
}
return $this->newTrigger("with", ...$keys);
}
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
public function with(string...$keys):Trigger {
return $this->select(...$keys);
}
/**
* Return a Trigger that will pass all keys apart from the provided
* keys to its callback.
*/
public function selectAllExcept(string...$keys):Trigger {
return $this->newTrigger("without", ...$keys);
}
/** @deprecated Use selectAllExcept() instead to avoid ambiguity with immutable `with` functions */
public function without(string...$keys):Trigger {
return $this->selectAllExcept(...$keys);
}
/**
* Return a Trigger that will pass all keys to its callback.
*/
public function selectAll():Trigger {
return $this->newTrigger("withAll");
}
/** @deprecated Use selectAll() instead to avoid ambiguity with immutable `with` functions */
public function withAll():Trigger {
return $this->selectAll();
}
protected function newTrigger(string $functionName, string...$args):Trigger {
$trigger = new Trigger($this);
return $trigger->$functionName(...$args);
}
}