Skip to content

Commit 7b8dca8

Browse files
committed
Merge branch 'master' of github.com:swoftcloud/swoft-component
2 parents 791b577 + 2779fec commit 7b8dca8

File tree

10 files changed

+199
-136
lines changed

10 files changed

+199
-136
lines changed

src/Concern/InteractsWithInput.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace Swoft\Http\Message\Concern;
44

5+
use function array_merge;
6+
use Exception;
7+
use InvalidArgumentException;
8+
use function stripos;
59
use Swoft\Http\Message\Stream\Stream;
10+
use Swoft\Http\Message\Upload\UploadedFile;
611
use Swoft\Stdlib\Helper\ArrayHelper;
712
use Swoft\Stdlib\Helper\JsonHelper;
813

@@ -131,7 +136,7 @@ public function input(string $key = '', $default = null)
131136
{
132137
$parsedBody = $this->getParsedBody();
133138
$parsedBody = is_array($parsedBody) ? $parsedBody : [];
134-
$inputs = \array_merge($parsedBody, $this->getQueryParams());
139+
$inputs = array_merge($parsedBody, $this->getQueryParams());
135140

136141
if (!$key) {
137142
return $inputs;
@@ -191,8 +196,8 @@ public function json(string $key = null, $default = null)
191196
$map = [];
192197
try {
193198
$contentType = $this->getHeader('content-type');
194-
if (!$contentType || false === \stripos($contentType[0], 'application/json')) {
195-
throw new \InvalidArgumentException(sprintf('Invalid Content-Type of the request, expects %s, %s given',
199+
if (!$contentType || false === stripos($contentType[0], 'application/json')) {
200+
throw new InvalidArgumentException(sprintf('Invalid Content-Type of the request, expects %s, %s given',
196201
'application/json', ($contentType ? current($contentType) : 'null')));
197202
}
198203

@@ -201,7 +206,7 @@ public function json(string $key = null, $default = null)
201206
$raw = $body->getContents();
202207
$map = JsonHelper::decode($raw, true);
203208
}
204-
} catch (\Exception $e) {
209+
} catch (Exception $e) {
205210
return $default;
206211
}
207212

@@ -218,7 +223,7 @@ public function json(string $key = null, $default = null)
218223
* @param string $key
219224
* @param mixed $default
220225
*
221-
* @return array|\Swoft\Http\Message\Upload\UploadedFile|null
226+
* @return array|UploadedFile|null
222227
*/
223228
public function file(string $key = '', $default = null)
224229
{

src/Concern/MessageTrait.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
namespace Swoft\Http\Message\Concern;
44

5+
use function array_map;
6+
use function array_merge;
7+
use function bean;
8+
use function implode;
9+
use InvalidArgumentException;
10+
use function is_array;
511
use Psr\Http\Message\MessageInterface;
612
use Psr\Http\Message\StreamInterface;
13+
use ReflectionException;
14+
use function strtolower;
15+
use Swoft\Bean\Exception\ContainerException;
716
use Swoft\Http\Message\Stream\Stream;
17+
use function trim;
818

919
/**
1020
* Class MessageTrait
@@ -113,7 +123,7 @@ public function getHeaders(): array
113123
*/
114124
public function hasHeader($name): bool
115125
{
116-
return isset($this->headerNames[\strtolower($name)]);
126+
return isset($this->headerNames[strtolower($name)]);
117127
}
118128

119129
/**
@@ -133,7 +143,7 @@ public function hasHeader($name): bool
133143
*/
134144
public function getHeader($name): array
135145
{
136-
$header = \strtolower($name);
146+
$header = strtolower($name);
137147
if (!isset($this->headerNames[$header])) {
138148
return [];
139149
}
@@ -164,7 +174,7 @@ public function getHeader($name): array
164174
*/
165175
public function getHeaderLine($name): string
166176
{
167-
return \implode(', ', $this->getHeader($name));
177+
return implode(', ', $this->getHeader($name));
168178
}
169179

170180
/**
@@ -181,16 +191,16 @@ public function getHeaderLine($name): string
181191
* @param string|string[] $value Header value(s).
182192
*
183193
* @return static
184-
* @throws \InvalidArgumentException for invalid header names or values.
194+
* @throws InvalidArgumentException for invalid header names or values.
185195
*/
186196
public function withHeader($name, $value)
187197
{
188-
if (!\is_array($value)) {
198+
if (!is_array($value)) {
189199
$value = [$value];
190200
}
191201

192202
$value = $this->trimHeaderValues($value);
193-
$normalized = \strtolower($name);
203+
$normalized = strtolower($name);
194204
$new = clone $this;
195205

196206
if (isset($new->headerNames[$normalized])) {
@@ -210,21 +220,21 @@ public function withHeader($name, $value)
210220
* @param string|string[] $value Header value(s).
211221
*
212222
* @return static
213-
* @throws \InvalidArgumentException for invalid header names or values.
223+
* @throws InvalidArgumentException for invalid header names or values.
214224
*/
215225
public function withAddedHeader($name, $value)
216226
{
217-
if (!\is_array($value)) {
227+
if (!is_array($value)) {
218228
$value = [$value];
219229
}
220230

221231
$value = $this->trimHeaderValues($value);
222-
$normalized = \strtolower($name);
232+
$normalized = strtolower($name);
223233
$new = clone $this;
224234

225235
if (isset($new->headerNames[$normalized])) {
226236
$name = $this->headerNames[$normalized];
227-
$new->headers[$name] = \array_merge($this->headers[$name], $value);
237+
$new->headers[$name] = array_merge($this->headers[$name], $value);
228238

229239
return $new;
230240
}
@@ -239,18 +249,18 @@ public function withHeaders(array $headers): self
239249
{
240250
$new = clone $this;
241251
foreach ($headers as $header => $value) {
242-
if (!\is_array($value)) {
252+
if (!is_array($value)) {
243253
$value = [$value];
244254
}
245255

246256
$value = $new->trimHeaderValues($value);
247-
$normalized = \strtolower($header);
257+
$normalized = strtolower($header);
248258

249259
if (isset($new->headerNames[$normalized])) {
250260
$headerName = $new->headerNames[$normalized];
251261
$oldValues = $new->headers[$headerName];
252262
// re-save
253-
$new->headers[$headerName] = \array_merge($oldValues, $value);
263+
$new->headers[$headerName] = array_merge($oldValues, $value);
254264
continue;
255265
}
256266

@@ -276,7 +286,7 @@ public function withHeaders(array $headers): self
276286
*/
277287
public function withoutHeader($name)
278288
{
279-
$normalized = \strtolower($name);
289+
$normalized = strtolower($name);
280290
if (!isset($this->headerNames[$normalized])) {
281291
return $this;
282292
}
@@ -292,13 +302,13 @@ public function withoutHeader($name)
292302
* Gets the body of the message.
293303
*
294304
* @return StreamInterface Returns the body as a stream.
295-
* @throws \ReflectionException
296-
* @throws \Swoft\Bean\Exception\ContainerException
305+
* @throws ReflectionException
306+
* @throws ContainerException
297307
*/
298308
public function getBody(): StreamInterface
299309
{
300310
if (!$this->stream) {
301-
$this->stream = \bean(Stream::class);
311+
$this->stream = bean(Stream::class);
302312
}
303313

304314
return $this->stream;
@@ -316,7 +326,7 @@ public function getBody(): StreamInterface
316326
* @param StreamInterface $body Body.
317327
*
318328
* @return static
319-
* @throws \InvalidArgumentException When the body is not valid.
329+
* @throws InvalidArgumentException When the body is not valid.
320330
*/
321331
public function withBody(StreamInterface $body)
322332
{
@@ -341,18 +351,18 @@ protected function setHeaders(array $headers): self
341351
{
342352
$this->headerNames = $this->headers = [];
343353
foreach ($headers as $header => $value) {
344-
if (!\is_array($value)) {
354+
if (!is_array($value)) {
345355
$value = [$value];
346356
}
347357

348358
$value = $this->trimHeaderValues($value);
349-
$normalized = \strtolower($header);
359+
$normalized = strtolower($header);
350360

351361
if (isset($this->headerNames[$normalized])) {
352362
$headerName = $this->headerNames[$normalized];
353363
$oldValues = $this->headers[$headerName];
354364
// re-save
355-
$this->headers[$headerName] = \array_merge($oldValues, $value);
365+
$this->headers[$headerName] = array_merge($oldValues, $value);
356366
continue;
357367
}
358368

@@ -368,7 +378,7 @@ protected function setHeaders(array $headers): self
368378
protected function initializeHeaders(array $headers): void
369379
{
370380
foreach ($headers as $name => $value) {
371-
$name = \strtolower($name);
381+
$name = strtolower($name);
372382

373383
$this->headers[$name] = [$value];
374384
$this->headerNames[$name] = $name;
@@ -391,8 +401,8 @@ protected function initializeHeaders(array $headers): void
391401
*/
392402
private function trimHeaderValues(array $values): array
393403
{
394-
return \array_map(function ($value) {
395-
return \trim((string)$value, " \t");
404+
return array_map(function ($value) {
405+
return trim((string)$value, " \t");
396406
}, $values);
397407
}
398408
}

src/Helper/HttpHelper.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Swoft\Http\Message\Helper;
44

5+
use function array_keys;
6+
use function explode;
7+
use InvalidArgumentException;
8+
use function is_array;
59
use Psr\Http\Message\UploadedFileInterface;
10+
use ReflectionException;
611
use Swoft\Bean\BeanFactory;
12+
use Swoft\Bean\Exception\ContainerException;
713
use Swoft\Http\Message\Upload\UploadedFile;
814
use Swoft\Http\Message\Uri\Uri;
915

@@ -18,7 +24,7 @@ class HttpHelper
1824
*
1925
* @param array $files A array which respect $_FILES structure
2026
*
21-
* @throws \InvalidArgumentException for unrecognized values
27+
* @throws InvalidArgumentException for unrecognized values
2228
* @return array
2329
*/
2430
public static function normalizeFiles(array $files): array
@@ -28,15 +34,15 @@ public static function normalizeFiles(array $files): array
2834
foreach ($files as $key => $value) {
2935
if ($value instanceof UploadedFileInterface) {
3036
$normalized[$key] = $value;
31-
} elseif (\is_array($value)) {
37+
} elseif (is_array($value)) {
3238
if (isset($value['tmp_name'])) {
3339
$normalized[$key] = self::createUploadedFileFromSpec($value);
3440
continue;
3541
}
3642

3743
$normalized[$key] = self::normalizeFiles($value);
3844
} else {
39-
throw new \InvalidArgumentException('Invalid value in files specification');
45+
throw new InvalidArgumentException('Invalid value in files specification');
4046
}
4147
}
4248

@@ -55,7 +61,7 @@ public static function normalizeFiles(array $files): array
5561
*/
5662
private static function createUploadedFileFromSpec(array $value)
5763
{
58-
if (\is_array($value['tmp_name'])) {
64+
if (is_array($value['tmp_name'])) {
5965
return self::normalizeNestedFileSpec($value);
6066
}
6167

@@ -81,7 +87,7 @@ private static function normalizeNestedFileSpec(array $files = []): array
8187
{
8288
$normalizedFiles = [];
8389

84-
foreach (\array_keys($files['tmp_name']) as $key) {
90+
foreach (array_keys($files['tmp_name']) as $key) {
8591
$spec = [
8692
'tmp_name' => $files['tmp_name'][$key],
8793
'size' => $files['size'][$key],
@@ -104,8 +110,10 @@ private static function normalizeNestedFileSpec(array $files = []): array
104110
* @param string $query
105111
* @param string $headerHost
106112
* @param array $server
113+
*
107114
* @return Uri
108-
* @throws \Swoft\Bean\Exception\ContainerException
115+
* @throws ContainerException
116+
* @throws ReflectionException
109117
*/
110118
public static function newUriByCoRequest(string $path, string $query, string $headerHost, array &$server): Uri
111119
{
@@ -115,7 +123,7 @@ public static function newUriByCoRequest(string $path, string $query, string $h
115123
$uri = $uri->withPath($path)->withQuery($query ?: $server['query_string']);
116124

117125
if ($host = $server['http_host']) {
118-
$parts = \explode(':', $host);
126+
$parts = explode(':', $host);
119127
$uri = $uri->withHost($parts[0]);
120128

121129
if (isset($parts[1])) {
@@ -124,7 +132,7 @@ public static function newUriByCoRequest(string $path, string $query, string $h
124132
} elseif ($host = $server['server_name'] ?: $server['server_addr']) {
125133
$uri = $uri->withHost($host);
126134
} elseif ($headerHost) {
127-
$parts = \explode(':', $headerHost, 2);
135+
$parts = explode(':', $headerHost, 2);
128136
$uri = $uri->withHost($parts[0]);
129137

130138
if (isset($parts[1])) {

src/PsrRequest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Swoft\Http\Message;
44

5+
use function in_array;
6+
use InvalidArgumentException;
7+
use function preg_match;
58
use Psr\Http\Message\RequestInterface;
69
use Psr\Http\Message\UriInterface;
10+
use function strtoupper;
711
use Swoft\Http\Message\Concern\MessageTrait;
812

913
/**
@@ -86,8 +90,8 @@ public function getRequestTarget(): string
8690
*/
8791
public function withRequestTarget($requestTarget)
8892
{
89-
if (\preg_match('#\s#', $requestTarget)) {
90-
throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
93+
if (preg_match('#\s#', $requestTarget)) {
94+
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
9195
}
9296

9397
$new = clone $this;
@@ -111,15 +115,15 @@ public function getMethod(): string
111115
* @param string $method Case-sensitive method.
112116
*
113117
* @return static
114-
* @throws \InvalidArgumentException for invalid HTTP methods.
118+
* @throws InvalidArgumentException for invalid HTTP methods.
115119
*/
116120
public function withMethod($method)
117121
{
118-
$method = \strtoupper($method);
122+
$method = strtoupper($method);
119123
$methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'HEAD'];
120124

121-
if (!\in_array($method, $methods, true)) {
122-
throw new \InvalidArgumentException('Invalid Method');
125+
if (!in_array($method, $methods, true)) {
126+
throw new InvalidArgumentException('Invalid Method');
123127
}
124128
$new = clone $this;
125129
$new->method = $method;

0 commit comments

Comments
 (0)