55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Phred\Http\Responses;
|
||
|
|
|
||
|
|
use Phred\Http\Contracts\ApiResponseFactoryInterface;
|
||
|
|
use Phred\Http\Middleware\ContentNegotiationMiddleware as Negotiation;
|
||
|
|
use Phred\Http\RequestContext;
|
||
|
|
use Psr\Http\Message\ResponseInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delegates to REST or JSON:API factory depending on current request format.
|
||
|
|
* Controllers receive this via DI and call its methods; it inspects
|
||
|
|
* RequestContext (set in DispatchMiddleware) to choose the underlying factory.
|
||
|
|
*/
|
||
|
|
final class DelegatingApiResponseFactory implements ApiResponseFactoryInterface
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private RestResponseFactory $rest,
|
||
|
|
private JsonApiResponseFactory $jsonapi
|
||
|
|
) {}
|
||
|
|
|
||
|
|
public function ok(array $data = []): ResponseInterface
|
||
|
|
{
|
||
|
|
return $this->delegate()->ok($data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function created(array $data = [], ?string $location = null): ResponseInterface
|
||
|
|
{
|
||
|
|
return $this->delegate()->created($data, $location);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function noContent(): ResponseInterface
|
||
|
|
{
|
||
|
|
return $this->delegate()->noContent();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function error(int $status, string $title, ?string $detail = null, array $extra = []): ResponseInterface
|
||
|
|
{
|
||
|
|
return $this->delegate()->error($status, $title, $detail, $extra);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function fromArray(array $payload, int $status = 200): ResponseInterface
|
||
|
|
{
|
||
|
|
return $this->delegate()->fromArray($payload, $status);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function delegate(): ApiResponseFactoryInterface
|
||
|
|
{
|
||
|
|
$req = RequestContext::get();
|
||
|
|
$format = $req?->getAttribute(Negotiation::ATTR_API_FORMAT) ?? 'rest';
|
||
|
|
return $format === 'jsonapi' ? $this->jsonapi : $this->rest;
|
||
|
|
}
|
||
|
|
}
|