1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2019 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace ArangoDb; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Http\JsonStream; |
15
|
|
|
use ArangoDb\Util\Json; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
final class HttpHelper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Separator between header and body |
22
|
|
|
*/ |
23
|
|
|
private const BODY_SEPARATOR = "\r\n\r\n"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Splits the message in HTTP status code, headers and body. |
27
|
|
|
* |
28
|
|
|
* @param string $message |
29
|
|
|
* @return array Values are HTTP status code, PSR-7 headers and body |
30
|
|
|
*/ |
31
|
41 |
|
public static function parseMessage(string $message): array |
32
|
|
|
{ |
33
|
41 |
|
$startLine = null; |
34
|
41 |
|
$headers = []; |
35
|
41 |
|
[$headerLines, $body] = explode(self::BODY_SEPARATOR, $message, 2); |
36
|
41 |
|
$headerLines = explode("\n", $headerLines); |
37
|
|
|
|
38
|
41 |
|
foreach ($headerLines as $header) { |
39
|
|
|
// Parse message headers |
40
|
41 |
|
if ($startLine === null) { |
41
|
41 |
|
$startLine = explode(' ', $header, 3); |
42
|
41 |
|
continue; |
43
|
|
|
} |
44
|
41 |
|
$parts = explode(':', $header, 2); |
45
|
41 |
|
$key = trim($parts[0]); |
46
|
41 |
|
$value = isset($parts[1]) ? trim($parts[1]) : ''; |
47
|
|
|
|
48
|
41 |
|
if (! isset($headers[$key])) { |
49
|
41 |
|
$headers[$key] = []; |
50
|
|
|
} |
51
|
41 |
|
$headers[$key][] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return [ |
55
|
41 |
|
(int) ($startLine[1] ?? 0), |
56
|
41 |
|
$headers, |
57
|
41 |
|
$body, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ResponseInterface $response |
63
|
|
|
* @param string|null $key |
64
|
|
|
* @return string|bool|int|float|array|null |
65
|
|
|
*/ |
66
|
8 |
|
public static function responseContentAsJson(ResponseInterface $response, string $key = null) |
67
|
|
|
{ |
68
|
8 |
|
$body = $response->getBody(); |
69
|
|
|
|
70
|
8 |
|
if ($body instanceof JsonStream) { |
71
|
4 |
|
if ($key === null) { |
72
|
1 |
|
return $body->toJson(); |
73
|
|
|
} |
74
|
3 |
|
$value = $body->toArray()[$key] ?? null; |
75
|
|
|
|
76
|
3 |
|
if (null === $value |
77
|
2 |
|
|| is_bool($value) |
78
|
3 |
|
|| is_int($value) |
79
|
|
|
) { |
80
|
2 |
|
return $value; |
81
|
|
|
} |
82
|
1 |
|
return Json::encode($value); |
83
|
|
|
} |
84
|
4 |
|
if ($key === null) { |
85
|
1 |
|
return $body->getContents(); |
86
|
|
|
} |
87
|
3 |
|
$value = Json::decode($body->getContents())[$key] ?? null; |
88
|
|
|
|
89
|
3 |
|
if ($value !== null && ! \is_scalar($value)) { |
90
|
1 |
|
$value = Json::encode($value); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param ResponseInterface $response |
98
|
|
|
* @param string|null $key |
99
|
|
|
* @return string|bool|int|float|array|null |
100
|
|
|
*/ |
101
|
11 |
|
public static function responseContentAsArray(ResponseInterface $response, string $key = null) |
102
|
|
|
{ |
103
|
11 |
|
$body = $response->getBody(); |
104
|
|
|
|
105
|
11 |
|
if ($body instanceof JsonStream) { |
106
|
6 |
|
if ($key === null) { |
107
|
2 |
|
return $body->toArray(); |
108
|
|
|
} |
109
|
4 |
|
return $body->toArray()[$key] ?? null; |
110
|
|
|
} |
111
|
5 |
|
if ($key === null) { |
112
|
1 |
|
return Json::decode($body->getContents()); |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
return Json::decode($body->getContents())[$key] ?? null; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|