1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Conia\Chuck; |
6
|
|
|
|
7
|
|
|
use Conia\Chuck\Exception\RuntimeException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
10
|
|
|
use Psr\Http\Message\StreamInterface; |
11
|
|
|
use Stringable; |
12
|
|
|
|
13
|
|
|
class Response |
14
|
|
|
{ |
15
|
|
|
use WrapsMessage; |
16
|
|
|
|
17
|
63 |
|
public function __construct( |
18
|
|
|
protected ResponseInterface $psr7, |
19
|
|
|
protected StreamFactoryInterface $streamFactory, |
20
|
|
|
) { |
21
|
63 |
|
} |
22
|
|
|
|
23
|
9 |
|
public function status(int $statusCode, ?string $reasonPhrase = null): static |
24
|
|
|
{ |
25
|
9 |
|
if (empty($reasonPhrase)) { |
26
|
7 |
|
$this->psr7 = $this->psr7->withStatus($statusCode); |
27
|
|
|
} else { |
28
|
2 |
|
$this->psr7 = $this->psr7->withStatus($statusCode, $reasonPhrase); |
29
|
|
|
} |
30
|
|
|
|
31
|
9 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
12 |
|
public function psr7(): ResponseInterface |
35
|
|
|
{ |
36
|
12 |
|
return $this->psr7; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function setPsr7(ResponseInterface $psr7): static |
40
|
|
|
{ |
41
|
1 |
|
$this->psr7 = $psr7; |
42
|
|
|
|
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
public function getStatusCode(): int |
47
|
|
|
{ |
48
|
8 |
|
return $this->psr7->getStatusCode(); |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
public function getReasonPhrase(): string |
52
|
|
|
{ |
53
|
6 |
|
return $this->psr7->getReasonPhrase(); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function protocolVersion(string $protocol): static |
57
|
|
|
{ |
58
|
1 |
|
$this->psr7 = $this->psr7->withProtocolVersion($protocol); |
59
|
|
|
|
60
|
1 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
public function header(string $name, string $value): static |
64
|
|
|
{ |
65
|
9 |
|
$this->psr7 = $this->psr7->withAddedHeader($name, $value); |
66
|
|
|
|
67
|
9 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function removeHeader(string $name): static |
71
|
|
|
{ |
72
|
1 |
|
$this->psr7 = $this->psr7->withoutHeader($name); |
73
|
|
|
|
74
|
1 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
public function headers(): array |
78
|
|
|
{ |
79
|
10 |
|
return $this->psr7->getHeaders(); |
80
|
|
|
} |
81
|
|
|
|
82
|
13 |
|
public function getHeader(string $name): array |
83
|
|
|
{ |
84
|
13 |
|
return $this->psr7->getHeader($name); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function hasHeader(string $name): bool |
88
|
|
|
{ |
89
|
3 |
|
return $this->psr7->hasHeader($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
public function body(mixed $body): static |
93
|
|
|
{ |
94
|
11 |
|
if (is_string($body) || $body instanceof Stringable) { |
95
|
9 |
|
$stream = $this->streamFactory->createStream((string)$body); |
96
|
2 |
|
} elseif (is_resource($body)) { |
97
|
1 |
|
$stream = $this->streamFactory->createStreamFromResource($body); |
98
|
|
|
} else { |
99
|
1 |
|
throw new RuntimeException('Only strings, Stringable or resources are allowed'); |
100
|
|
|
} |
101
|
|
|
|
102
|
10 |
|
$this->psr7 = $this->psr7->withBody($stream); |
103
|
|
|
|
104
|
10 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
38 |
|
public function getBody(): StreamInterface |
108
|
|
|
{ |
109
|
38 |
|
return $this->psr7->getBody(); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function write(string $content): int |
113
|
|
|
{ |
114
|
1 |
|
return $this->psr7->getBody()->write($content); |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
public function redirect(string $url, int $code = 302): static |
118
|
|
|
{ |
119
|
2 |
|
$this->header('Location', $url); |
120
|
2 |
|
$this->status($code); |
121
|
|
|
|
122
|
2 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
public function withStatus(int $code, string $reasonPhrase = ''): static |
126
|
|
|
{ |
127
|
2 |
|
return $this->status($code, $reasonPhrase); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|