1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Psr7; |
5
|
|
|
|
6
|
|
|
use Nette\Http\IRequest; |
7
|
|
|
use Nette\Http\IResponse; |
8
|
|
|
use Nette\NotImplementedException; |
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
use Zend\Diactoros\Stream; |
11
|
|
|
|
12
|
|
|
class Response implements ApplicationPsr7ResponseInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $code = IResponse::S200_OK; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $headers = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var StreamInterface |
26
|
|
|
*/ |
27
|
|
|
private $stream; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param IRequest $httpRequest |
31
|
|
|
* @param IResponse $httpResponse |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function send(IRequest $httpRequest, IResponse $httpResponse) |
35
|
|
|
{ |
36
|
|
|
$httpResponse->setCode($this->code); |
37
|
|
|
foreach ($this->headers as $name => $value) { |
38
|
|
|
$httpResponse->setHeader($name, $value); |
39
|
|
|
} |
40
|
|
|
echo (string) $this->getBody(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $name |
45
|
|
|
* @param string|string[] $value |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function withHeader($name, $value) |
49
|
|
|
{ |
50
|
|
|
$response = clone $this; |
51
|
|
|
$response->headers[$name] = $value; |
52
|
|
|
return $response; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return StreamInterface |
57
|
|
|
*/ |
58
|
|
|
public function getBody() |
59
|
|
|
{ |
60
|
|
|
if (!$this->stream) { |
61
|
|
|
$this->stream = new Stream('php://temp', 'r+'); |
62
|
|
|
} |
63
|
|
|
return $this->stream; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $code |
68
|
|
|
* @param string $reasonPhrase |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function withStatus($code, $reasonPhrase = '') |
72
|
|
|
{ |
73
|
|
|
$response = clone $this; |
74
|
|
|
$response->code = $code; |
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return self |
80
|
|
|
*/ |
81
|
|
|
public function withBody(StreamInterface $body) |
82
|
|
|
{ |
83
|
|
|
$response = clone $this; |
84
|
|
|
$response->stream = $body; |
85
|
|
|
return $response; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws NotImplementedException |
90
|
|
|
*/ |
91
|
|
|
public function getProtocolVersion() |
92
|
|
|
{ |
93
|
|
|
throw new NotImplementedException(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws NotImplementedException |
98
|
|
|
*/ |
99
|
|
|
public function withProtocolVersion($version) |
100
|
|
|
{ |
101
|
|
|
throw new NotImplementedException(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws NotImplementedException |
106
|
|
|
*/ |
107
|
|
|
public function getHeaders() |
108
|
|
|
{ |
109
|
|
|
throw new NotImplementedException(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws NotImplementedException |
114
|
|
|
*/ |
115
|
|
|
public function hasHeader($name) |
116
|
|
|
{ |
117
|
|
|
throw new NotImplementedException(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws NotImplementedException |
122
|
|
|
*/ |
123
|
|
|
public function getHeader($name) |
124
|
|
|
{ |
125
|
|
|
throw new NotImplementedException(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws NotImplementedException |
130
|
|
|
*/ |
131
|
|
|
public function getHeaderLine($name) |
132
|
|
|
{ |
133
|
|
|
throw new NotImplementedException(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @throws NotImplementedException |
138
|
|
|
*/ |
139
|
|
|
public function withAddedHeader($name, $value) |
140
|
|
|
{ |
141
|
|
|
throw new NotImplementedException(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @throws NotImplementedException |
146
|
|
|
*/ |
147
|
|
|
public function withoutHeader($name) |
148
|
|
|
{ |
149
|
|
|
throw new NotImplementedException(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @throws NotImplementedException |
154
|
|
|
*/ |
155
|
|
|
public function getStatusCode() |
156
|
|
|
{ |
157
|
|
|
throw new NotImplementedException(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @throws NotImplementedException |
162
|
|
|
*/ |
163
|
|
|
public function getReasonPhrase() |
164
|
|
|
{ |
165
|
|
|
throw new NotImplementedException(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|