Skip to content

Commit c168e83

Browse files
change to do "or" instead of "and"
1 parent a9c77ac commit c168e83

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

packages/http/src/IsRequest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ public function accepts(ContentType ...$contentTypes): bool
158158
];
159159
}
160160

161-
/** @var array<string, bool> */
162-
$supported = [];
161+
if (empty($mediaTypes)) {
162+
return true;
163+
}
163164

164165
foreach ($contentTypes as $contentType) {
165166
[$mediaType, $subType] = explode('/', $contentType->value);
@@ -169,15 +170,12 @@ public function accepts(ContentType ...$contentTypes): bool
169170
($acceptedType['mediaType'] === '*' || $acceptedType['mediaType'] === $mediaType)
170171
&& ($acceptedType['subType'] === '*' || $acceptedType['subType'] === $subType)
171172
) {
172-
$supported[$contentType->value] = true;
173-
break;
173+
return true;
174174
}
175-
176-
$supported[$contentType->value] = false;
177175
}
178176
}
179177

180-
return every($supported, static fn (bool $isSupported) => $isSupported);
178+
return false;
181179
}
182180

183181
public function withMethod(Method $method): self

packages/http/src/Request.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public function getCookie(string $name): ?Cookie;
6060

6161
/**
6262
* Determines if the request's "Content-Type" header matches the given content type.
63-
*
64-
* If multiple content types are provided, the method returns true if all are matched.
63+
* If multiple content types are provided, the method returns true if any of them matches.
6564
*/
6665
public function accepts(ContentType ...$contentType): bool;
6766
}

packages/http/tests/GenericRequestTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function test_accepts_with_wildcard_subtype(): void
150150
$this->assertTrue($request->accepts(ContentType::XML));
151151
}
152152

153-
public function test_accepts_evaluates_all_content_types(): void
153+
public function test_accepts_can_take_multiple_params(): void
154154
{
155155
$request = new GenericRequest(
156156
method: Method::GET,
@@ -160,9 +160,10 @@ public function test_accepts_evaluates_all_content_types(): void
160160
],
161161
);
162162

163-
$this->assertFalse($request->accepts(ContentType::JSON, ContentType::HTML));
164-
$this->assertTrue($request->accepts(ContentType::JSON, ContentType::XML));
163+
$this->assertTrue($request->accepts(ContentType::HTML, ContentType::JSON));
164+
$this->assertTrue($request->accepts(ContentType::XML, ContentType::JSON));
165165
$this->assertTrue($request->accepts(ContentType::JSON, ContentType::AVIF));
166-
$this->assertFalse($request->accepts(ContentType::AVIF, ContentType::PNG));
166+
$this->assertTrue($request->accepts(ContentType::AVIF, ContentType::PNG));
167+
$this->assertFalse($request->accepts(ContentType::HTML, ContentType::PNG));
167168
}
168169
}

0 commit comments

Comments
 (0)