-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestInput.php
More file actions
215 lines (188 loc) · 5.92 KB
/
Copy pathRequestInput.php
File metadata and controls
215 lines (188 loc) · 5.92 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
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// src/Libs/Italix/Mvc/RequestInput.php
declare(strict_types=1);
namespace Italix\Mvc;
use Italix\Contracts\DataContainer;
use Psr\Http\Message\ServerRequestInterface;
class RequestInput
{
/** @var ServerRequestInterface */
private $request;
public function __construct(ServerRequestInterface $request)
{
$this->request = $request;
}
/**
* Access query string (GET) parameters.
*
* get() → DataContainer of all query params
* get('key', $default) → single value or $default
* get(['key' => $default, ...]) → DataContainer of requested keys, defaults applied
*
* @param string|array|null $key
* @param mixed $default
* @return mixed
*/
public function get($key = null, $default = null)
{
$params = $this->request->getQueryParams();
if ($key === null) {
return new DataContainer($params);
}
if (is_array($key)) {
$result = [];
foreach ($key as $k => $default_val) {
$result[$k] = array_key_exists($k, $params) ? $params[$k] : $default_val;
}
return new DataContainer($result);
}
return array_key_exists($key, $params) ? $params[$key] : $default;
}
/**
* Access parsed body (POST / form) parameters.
*
* post() → DataContainer of all body params
* post('key', $default) → single value or $default
* post(['key' => $default, ...]) → DataContainer of requested keys, defaults applied
*
* @param string|array|null $key
* @param mixed $default
* @return mixed
*/
public function post($key = null, $default = null)
{
$body = $this->request->getParsedBody();
$params = is_array($body) ? $body : (is_object($body) ? (array) $body : []);
if ($key === null) {
return new DataContainer($params);
}
if (is_array($key)) {
$result = [];
foreach ($key as $k => $default_val) {
$result[$k] = array_key_exists($k, $params) ? $params[$k] : $default_val;
}
return new DataContainer($result);
}
return array_key_exists($key, $params) ? $params[$key] : $default;
}
/**
* Access uploaded files.
*
* files() → DataContainer of all UploadedFileInterface objects
* files('key') → UploadedFileInterface or null
* files(['key' => null, ...]) → DataContainer of requested keys, defaults applied
*
* @param string|array|null $key
* @return mixed
*/
public function files($key = null)
{
$uploaded = $this->request->getUploadedFiles();
if ($key === null) {
return new DataContainer($uploaded);
}
if (is_array($key)) {
$result = [];
foreach ($key as $k => $default_val) {
$result[$k] = array_key_exists($k, $uploaded) ? $uploaded[$k] : $default_val;
}
return new DataContainer($result);
}
return $uploaded[$key] ?? null;
}
/**
* Parse and return the raw request body as JSON.
* Returns the decoded value (array/scalar) or null if the body is empty or invalid.
*
* @return mixed
*/
public function json()
{
$body = (string) $this->request->getBody();
if ($body === '') {
return null;
}
return json_decode($body, true);
}
/**
* Get a PSR-7 request attribute set by middleware (e.g. 'lang' from LocaleMiddleware).
*
* @param mixed $default
* @return mixed
*/
public function attr(string $key, $default = null)
{
$value = $this->request->getAttribute($key);
return $value !== null ? $value : $default;
}
/**
* Get the first value of a named request header, case-insensitive.
*/
public function header(string $name, string $default = ''): string
{
$values = $this->request->getHeader($name);
return $values ? $values[0] : $default;
}
/**
* Extract the Bearer token from the Authorization header, or null if absent.
*/
public function bearer_token(): ?string
{
$auth = $this->header('Authorization');
if (strpos($auth, 'Bearer ') === 0) {
return substr($auth, 7);
}
return null;
}
/**
* Get a cookie value by name, or $default if not present.
*
* @param mixed $default
* @return mixed
*/
public function cookie(string $name, $default = null)
{
$cookies = $this->request->getCookieParams();
return array_key_exists($name, $cookies) ? $cookies[$name] : $default;
}
/**
* Return the HTTP method in uppercase (GET, POST, PUT, …).
*/
public function method(): string
{
return strtoupper($this->request->getMethod());
}
/**
* True if the request Content-Type indicates a JSON payload.
*/
public function is_json(): bool
{
return strpos($this->header('Content-Type'), 'application/json') !== false;
}
/**
* True if the request was made via XMLHttpRequest (Ajax).
*/
public function is_ajax(): bool
{
return $this->header('X-Requested-With') === 'XMLHttpRequest';
}
/**
* Return the client IP address, or null if not available.
*/
public function ip(): ?string
{
return $this->request->getServerParams()['REMOTE_ADDR'] ?? null;
}
/**
* Return the URI path (without query string).
*/
public function path(): string
{
return $this->request->getUri()->getPath();
}
}