-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
121 lines (111 loc) · 3.61 KB
/
Copy pathResponse.php
File metadata and controls
121 lines (111 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use bdk\HttpMessage\Message;
use bdk\HttpMessage\Utility\Response as ResponseUtil;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
/**
* Http Response
*
* @psalm-consistent-constructor
*/
class Response extends Message implements ResponseInterface
{
/** @var string */
private $reasonPhrase = '';
/** @var int */
private $statusCode = 200;
/**
* Constructor
*
* @param int $code The HTTP status code. Defaults to 200.
* @param string $reasonPhrase The reason phrase to associate with the status code
* Defaults to standard phrase for given code
*/
public function __construct($code = 200, $reasonPhrase = '')
{
list($code, $reasonPhrase) = $this->filterCodePhrase($code, $reasonPhrase);
$this->statusCode = $code;
$this->reasonPhrase = $reasonPhrase;
}
/**
* Gets the response status code.
*
* The status code is a 3-digit integer result code of the server's attempt
* to understand and satisfy the request.
*
* @return int Status code.
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* Gets the response reason phrase associated with the status code.
*
* @return string Reason phrase; must return an empty string if none present.
*
* @see https://datatracker.ietf.org/doc/html/rfc7231#section-6
* @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
*/
public function getReasonPhrase(): string
{
return $this->reasonPhrase;
}
/**
* Return an instance with the specified status code and, optionally, reason phrase.
*
* If no reason phrase is specified, implementations MAY choose to default
* to the RFC 7231 or IANA recommended reason phrase for the response's
* status code.
*
* @param int $code The 3-digit integer result code to set.
* @param string $reasonPhrase The reason phrase to use with the
* provided status code; if none is provided, implementations MAY
* use the defaults as suggested in the HTTP specification.
*
* @return static
*
* @see https://datatracker.ietf.org/doc/html/rfc7231#section-6
* @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
*
* @throws InvalidArgumentException For invalid status code arguments.
*/
public function withStatus(int $code, string $reasonPhrase = ''): static
{
list($code, $reasonPhrase) = $this->filterCodePhrase($code, $reasonPhrase);
$new = clone $this;
$new->statusCode = $code;
$new->reasonPhrase = $reasonPhrase;
return $new;
}
/**
* Filter/validate code and reason-phrase
*
* @param int $code Status Code
* @param string|null $phrase Reason Phrase
*
* @return array{0:int,1:string} code & phrase
*
* @throws InvalidArgumentException
*/
private function filterCodePhrase(int $code, string $phrase)
{
$this->assertStatusCode($code);
$code = (int) $code;
if ($phrase === null || $phrase === '') {
$phrase = ResponseUtil::codePhrase($code);
}
$this->assertReasonPhrase($phrase);
return array($code, $phrase);
}
}