-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathIsResponse.php
More file actions
132 lines (97 loc) · 2.75 KB
/
IsResponse.php
File metadata and controls
132 lines (97 loc) · 2.75 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
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Tempest\Http;
use Generator;
use JsonSerializable;
use Tempest\Http\Cookie\Cookie;
use Tempest\Http\Cookie\CookieManager;
use Tempest\Http\Session\Session;
use Tempest\View\View;
use UnitEnum;
use function Tempest\Container\get;
/** @phpstan-require-implements \Tempest\Http\Response */
trait IsResponse
{
private(set) Status $status = Status::OK;
private(set) View|string|array|Generator|JsonSerializable|null $body = null;
/** @var \Tempest\Http\Header[] */
private(set) array $headers = [];
public CookieManager $cookieManager {
get => get(CookieManager::class);
}
public Session $session {
get => get(Session::class);
}
private(set) ?View $view = null;
public function getHeader(string $name): ?Header
{
return array_find(
array: $this->headers,
callback: fn (Header $header) => strcasecmp($header->name, $name) === 0,
);
}
public function addHeaders(array $headers): self
{
foreach ($headers as $key => $values) {
if (! is_array($values)) {
$values = [$values];
}
foreach ($values as $value) {
$this->addHeader($key, $value);
}
}
return $this;
}
public function addHeader(string $key, string $value): self
{
$this->headers[$key] ??= new Header($key);
$this->headers[$key]->add($value);
return $this;
}
public function removeHeader(string $key): self
{
unset($this->headers[$key]);
return $this;
}
public function addCookie(Cookie $cookie): self
{
$this->cookieManager->add($cookie);
return $this;
}
public function removeCookie(string $key): self
{
$this->cookieManager->remove($key);
return $this;
}
public function addSession(string $name, mixed $value): self
{
$this->session->set($name, $value);
return $this;
}
public function removeSession(string $name): self
{
$this->session->remove($name);
return $this;
}
public function flash(string|UnitEnum $key, mixed $value): self
{
$this->session->flash($key, $value);
return $this;
}
public function setContentType(ContentType $contentType): self
{
$this->removeHeader(ContentType::HEADER)
->addHeader(ContentType::HEADER, $contentType->value);
return $this;
}
public function setStatus(Status $status): self
{
$this->status = $status;
return $this;
}
public function setBody(View|string|array|Generator|null $body): self
{
$this->body = $body;
return $this;
}
}