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