Skip to content

Commit f5fdd7b

Browse files
wip
1 parent d1ee721 commit f5fdd7b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

packages/http/src/IsRequest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function Tempest\get;
1313
use function Tempest\Support\Arr\get_by_key;
1414
use function Tempest\Support\Arr\has_key;
15+
use function Tempest\Support\str;
1516

1617
/** @phpstan-require-implements \Tempest\Http\Request */
1718
trait IsRequest
@@ -136,6 +137,26 @@ public function hasQuery(string $key): bool
136137
return has_key($this->query, $key);
137138
}
138139

140+
public function accepts(ContentType $contentType): bool
141+
{
142+
$header = $this->headers->get(name: 'accept');
143+
144+
if ($header === null) {
145+
return true;
146+
}
147+
148+
$accepts = str($header)
149+
->explode(separator: ',')
150+
->map(static fn (string $item) => trim($item))
151+
->filter(static fn (string $item) => $item !== '');
152+
153+
if ($accepts->isEmpty() || $accepts->contains(search: '*/*')) {
154+
return true;
155+
}
156+
157+
return $accepts->contains(search: $contentType->value);
158+
}
159+
139160
public function withMethod(Method $method): self
140161
{
141162
$clone = clone $this;

packages/http/src/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ public function get(string $key, mixed $default = null): mixed;
5757
public function getSessionValue(string $name): mixed;
5858

5959
public function getCookie(string $name): ?Cookie;
60+
61+
public function accepts(ContentType $contentType): bool;
6062
}

packages/http/tests/GenericRequestTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use LogicException;
88
use PHPUnit\Framework\TestCase;
9+
use Tempest\Http\ContentType;
910
use Tempest\Http\GenericRequest;
1011
use Tempest\Http\Header;
1112
use Tempest\Http\Method;
@@ -61,4 +62,61 @@ public function test_throws_on_unset(): void
6162
$this->expectException(LogicException::class);
6263
unset($headers['x']);
6364
}
65+
66+
public function test_accepts_with_accept_header(): void
67+
{
68+
$request = new GenericRequest(
69+
method: Method::GET,
70+
uri: '/',
71+
headers: [
72+
'Accept' => 'application/json',
73+
],
74+
);
75+
76+
$this->assertTrue($request->accepts(ContentType::JSON));
77+
$this->assertFalse($request->accepts(ContentType::HTML));
78+
$this->assertFalse($request->accepts(ContentType::XML));
79+
}
80+
81+
public function test_accepts_with_no_accept_header(): void
82+
{
83+
$request = new GenericRequest(
84+
method: Method::GET,
85+
uri: '/',
86+
);
87+
88+
$this->assertTrue($request->accepts(ContentType::JSON));
89+
$this->assertTrue($request->accepts(ContentType::HTML));
90+
$this->assertTrue($request->accepts(ContentType::XML));
91+
}
92+
93+
public function test_accepts_with_wildcard(): void
94+
{
95+
$request = new GenericRequest(
96+
method: Method::GET,
97+
uri: '/',
98+
headers: [
99+
'Accept' => '*/*',
100+
],
101+
);
102+
103+
$this->assertTrue($request->accepts(ContentType::JSON));
104+
$this->assertTrue($request->accepts(ContentType::HTML));
105+
$this->assertTrue($request->accepts(ContentType::XML));
106+
}
107+
108+
public function test_accepts_with_multiple_values(): void
109+
{
110+
$request = new GenericRequest(
111+
method: Method::GET,
112+
uri: '/',
113+
headers: [
114+
'Accept' => 'application/json, text/html',
115+
],
116+
);
117+
118+
$this->assertTrue($request->accepts(ContentType::JSON));
119+
$this->assertTrue($request->accepts(ContentType::HTML));
120+
$this->assertFalse($request->accepts(ContentType::XML));
121+
}
64122
}

0 commit comments

Comments
 (0)