|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequestPsr\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\RequestException; |
|
7
|
|
|
use kalanis\RemoteRequestPsr\Http\Answer; |
|
8
|
|
|
use Psr\Http\Message\MessageInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\StreamInterface; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ResponseAdapter |
|
15
|
|
|
* @package kalanis\RemoteRequestPsr\Adapters |
|
16
|
|
|
* Response adapter for PSR Interface |
|
17
|
|
|
* It is not possible to use that to set things, it just returns the data |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResponseAdapter implements ResponseInterface |
|
20
|
|
|
{ |
|
21
|
|
|
protected Answer $answer; |
|
22
|
|
|
protected StreamAdapter $stream; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Answer $answer |
|
26
|
|
|
* @throws RequestException |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct(Answer $answer) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->answer = $answer; |
|
31
|
4 |
|
$this->stream = new StreamAdapter($answer); |
|
32
|
4 |
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function getProtocolVersion(): string |
|
35
|
|
|
{ |
|
36
|
2 |
|
return '1.1'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function withProtocolVersion(string $version): MessageInterface |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function getHeaders(): array |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->answer->getAllHeaders(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function hasHeader(string $name): bool |
|
50
|
|
|
{ |
|
51
|
1 |
|
return !empty($this->getHeader($name)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function getHeader(string $name): array |
|
55
|
|
|
{ |
|
56
|
1 |
|
$want = mb_strtolower($name); |
|
57
|
1 |
|
$available = []; |
|
58
|
1 |
|
foreach ($this->answer->getAllHeaders() as $key => $values) { |
|
59
|
1 |
|
if (strtolower($key) == $want) { |
|
60
|
1 |
|
$available = array_merge($available, $values); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
1 |
|
return $available; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getHeaderLine(string $name): string |
|
67
|
|
|
{ |
|
68
|
1 |
|
return implode(',', $this->getHeader($name)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function withHeader(string $name, $value): MessageInterface |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function withAddedHeader(string $name, $value): MessageInterface |
|
77
|
|
|
{ |
|
78
|
1 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function withoutHeader(string $name): MessageInterface |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
public function getBody(): StreamInterface |
|
87
|
|
|
{ |
|
88
|
1 |
|
return $this->stream; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
public function withBody(StreamInterface $body): MessageInterface |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function getStatusCode(): int |
|
97
|
|
|
{ |
|
98
|
4 |
|
return $this->answer->getCode(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
public function getReasonPhrase(): string |
|
107
|
|
|
{ |
|
108
|
2 |
|
return $this->answer->getReason(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|