Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Middleware/AcceptProvider/AcceptProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\DataResponse\Middleware\AcceptProvider;

use Psr\Http\Message\ServerRequestInterface;

interface AcceptProviderInterface
{
/**
* @param ServerRequestInterface $request The request instance.
* @return string[] The array of acceptable content types in order of preference.
*/
public function get(ServerRequestInterface $request): array;
}
26 changes: 26 additions & 0 deletions src/Middleware/AcceptProvider/HeaderAcceptProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\DataResponse\Middleware\AcceptProvider;

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Http\Header;

final class HeaderAcceptProvider implements AcceptProviderInterface
{
public function get(ServerRequestInterface $request): array
{
$values = [];
foreach ($request->getHeader(Header::ACCEPT) as $headerValue) {
$values[] = array_filter(

Check warning on line 16 in src/Middleware/AcceptProvider/HeaderAcceptProvider.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayFilter": @@ @@ { $values = []; foreach ($request->getHeader(Header::ACCEPT) as $headerValue) { - $values[] = array_filter(array_map(trim(...), explode(',', $headerValue)), static fn(string $value) => $value !== ''); + $values[] = array_map(trim(...), explode(',', $headerValue)); } return array_merge(...$values); } }
array_map(

Check warning on line 17 in src/Middleware/AcceptProvider/HeaderAcceptProvider.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMap": @@ @@ { $values = []; foreach ($request->getHeader(Header::ACCEPT) as $headerValue) { - $values[] = array_filter(array_map(trim(...), explode(',', $headerValue)), static fn(string $value) => $value !== ''); + $values[] = array_filter(explode(',', $headerValue), static fn(string $value) => $value !== ''); } return array_merge(...$values); } }
trim(...),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value should be clean from extra parameters (as example, q):

text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7

explode(',', $headerValue),
),
static fn(string $value) => $value !== '',
);
}
return array_merge(...$values);
}
}
63 changes: 63 additions & 0 deletions src/Middleware/AcceptProvider/RequestParameterAcceptProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Yiisoft\DataResponse\Middleware\AcceptProvider;

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Http\Method;

use function is_array;
use function is_string;

final class RequestParameterAcceptProvider implements AcceptProviderInterface
{
public function __construct(
public string $name = 'format',
) {
}

public function get(ServerRequestInterface $request): array
{
return $request->getMethod() === Method::GET
? $this->fromQueryParams($request)
: [...$this->fromParsedBody($request), ...$this->fromQueryParams($request)];
}

/**
* @return string[]
*/
public function fromQueryParams(ServerRequestInterface $request): array
{
return $this->prepareValue($request->getQueryParams()[$this->name] ?? null);
}

/**
* @return string[]
*/
private function fromParsedBody(ServerRequestInterface $request): array
{
$body = $request->getParsedBody();
return is_array($body)
? $this->prepareValue($body[$this->name] ?? null)
: [];
}

/**
* @return string[]
*/
private function prepareValue(mixed $value): array
{
if (!is_string($value)) {
return [];
}

return array_filter(
array_map(
trim(...),
explode(',', $value),
),
static fn(string $accept) => $accept !== '',
);
}
}
19 changes: 9 additions & 10 deletions src/Middleware/ContentNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use RuntimeException;
use Yiisoft\DataResponse\DataResponse;
use Yiisoft\DataResponse\DataResponseFormatterInterface;
use Yiisoft\Http\Header;
use Yiisoft\DataResponse\Middleware\AcceptProvider\AcceptProviderInterface;
use Yiisoft\DataResponse\Middleware\AcceptProvider\HeaderAcceptProvider;

use function gettype;
use function is_string;
Expand All @@ -35,8 +36,10 @@ final class ContentNegotiator implements MiddlewareInterface
*
* @psalm-param array<string, DataResponseFormatterInterface> $contentFormatters
*/
public function __construct(array $contentFormatters)
{
public function __construct(
array $contentFormatters,
private readonly AcceptProviderInterface $acceptProvider = new HeaderAcceptProvider(),
) {
$this->checkFormatters($contentFormatters);
$this->contentFormatters = $contentFormatters;
}
Expand All @@ -61,13 +64,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
$response = $handler->handle($request);
if ($response instanceof DataResponse && !$response->hasResponseFormatter()) {
$accepted = $request->getHeader(Header::ACCEPT);

foreach ($accepted as $accept) {
foreach ($this->contentFormatters as $contentType => $formatter) {
if (str_contains($accept, $contentType)) {
return $response->withResponseFormatter($formatter);
}
foreach ($this->acceptProvider->get($request) as $accept) {
if (isset($this->contentFormatters[$accept])) {
return $response->withResponseFormatter($this->contentFormatters[$accept]);
}
}
}
Expand Down
Loading