-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.php
57 lines (45 loc) · 1.17 KB
/
Response.php
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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\HttpMessage;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
/**
* {@inheritdoc}
*
* @author Joshua Estes <[email protected]>
*/
class Response extends Message implements ResponseInterface
{
private int $statusCode;
private string $reasonPhrase = '';
/**
* {@inheritdoc}
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* {@inheritdoc}
*/
public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface
{
if (!Status::tryFrom($code) instanceof Status) {
throw new InvalidArgumentException(sprintf('The status code "%d" is invalid', $code));
}
if (isset($this->statusCode) && $this->statusCode === $code && $this->reasonPhrase === $reasonPhrase) {
return $this;
}
$that = clone $this;
$that->statusCode = $code;
$that->reasonPhrase = $reasonPhrase;
return $that;
}
/**
* {@inheritdoc}
*/
public function getReasonPhrase(): string
{
return $this->reasonPhrase;
}
}