|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of slick/http |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\Http\Message; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Psr\Http\Message\StreamInterface; |
|
17
|
|
|
use Slick\Http\Message\Stream\TextStream; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Response |
|
21
|
|
|
* |
|
22
|
|
|
* @package Slick\Http\Message |
|
23
|
|
|
*/ |
|
24
|
|
|
class Response extends Message implements ResponseInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private int $status; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private string $reasonPhrase; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates an HTTP Response message |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $status |
|
40
|
|
|
* @param string|StreamInterface $body |
|
41
|
|
|
* @param array<string, string> $headers |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(int $status, StreamInterface|string $body = '', array $headers = []) |
|
44
|
|
|
{ |
|
45
|
|
|
$body = \is_string($body) ? new TextStream($body) : $body; |
|
46
|
|
|
$body->rewind(); |
|
47
|
|
|
parent::__construct($body->getContents()); |
|
48
|
|
|
|
|
49
|
|
|
$this->setStatus($status); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($headers as $name => $header) { |
|
52
|
|
|
$this->headers[$name] = [$header]; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets the response status code. |
|
58
|
|
|
* |
|
59
|
|
|
* The status code is a 3-digit integer result code of the server's attempt |
|
60
|
|
|
* to understand and satisfy the request. |
|
61
|
|
|
* |
|
62
|
|
|
* @return int Status code. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getStatusCode(): int |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->status; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return an instance with the specified status code and, optionally, reason phrase. |
|
71
|
|
|
* |
|
72
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
|
73
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
74
|
|
|
* @param int $code The 3-digit integer result code to set. |
|
75
|
|
|
* @param string $reasonPhrase The reason phrase to use with the |
|
76
|
|
|
* provided status code; if none is provided, implementations MAY |
|
77
|
|
|
* use the defaults as suggested in the HTTP specification. |
|
78
|
|
|
* @return static |
|
79
|
|
|
* @throws InvalidArgumentException For invalid status code arguments. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface |
|
82
|
|
|
{ |
|
83
|
|
|
$response = clone $this; |
|
84
|
|
|
$response->setStatus($code, $reasonPhrase); |
|
85
|
|
|
return $response; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Gets the response reason phrase associated with the status code. |
|
90
|
|
|
* |
|
91
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
|
92
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
93
|
|
|
* @return string Reason phrase; must return an empty string if none present. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getReasonPhrase(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->reasonPhrase; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets the response status code |
|
102
|
|
|
* |
|
103
|
|
|
* @param int $status |
|
104
|
|
|
* @param string $reasonPhrase |
|
105
|
|
|
*/ |
|
106
|
|
|
private function setStatus(int $status, string $reasonPhrase = ''): void |
|
107
|
|
|
{ |
|
108
|
|
|
(new HttpCodes())->check($status); |
|
109
|
|
|
$this->status = $status; |
|
110
|
|
|
if ($reasonPhrase === '') { |
|
111
|
|
|
$this->reasonPhrase = (new HttpCodes())->reasonPhraseFor($this->status); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|