|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\ApiHttp\ApiProblem\ClientError\NotAcceptable; |
|
8
|
|
|
use Chubbyphp\ApiHttp\ApiProblem\ClientError\UnsupportedMediaType; |
|
9
|
|
|
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; |
|
10
|
|
|
use Chubbyphp\Negotiation\AcceptNegotiatorInterface; |
|
11
|
|
|
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
14
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
15
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class AcceptAndContentTypeMiddleware implements MiddlewareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var AcceptNegotiatorInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $acceptNegotiator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ContentTypeNegotiatorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $contentTypeNegotiator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ResponseManagerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $responseManager; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct( |
|
35
|
|
|
AcceptNegotiatorInterface $acceptNegotiator, |
|
36
|
|
|
ContentTypeNegotiatorInterface $contentTypeNegotiator, |
|
37
|
|
|
ResponseManagerInterface $responseManager |
|
38
|
|
|
) { |
|
39
|
4 |
|
$this->acceptNegotiator = $acceptNegotiator; |
|
40
|
|
|
$this->contentTypeNegotiator = $contentTypeNegotiator; |
|
41
|
|
|
$this->responseManager = $responseManager; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
45
|
4 |
|
{ |
|
46
|
4 |
|
if (null === $accept = $this->acceptNegotiator->negotiate($request)) { |
|
47
|
4 |
|
$supportedMediaTypes = $this->acceptNegotiator->getSupportedMediaTypes(); |
|
48
|
|
|
|
|
49
|
|
|
return $this->responseManager->createFromApiProblem( |
|
50
|
|
|
new NotAcceptable( |
|
51
|
|
|
$request->getHeaderLine('Accept'), |
|
52
|
|
|
$supportedMediaTypes |
|
53
|
|
|
), |
|
54
|
|
|
$supportedMediaTypes[0] |
|
55
|
4 |
|
); |
|
56
|
|
|
} |
|
57
|
4 |
|
|
|
58
|
1 |
|
$request = $request->withAttribute('accept', $accept->getValue()); |
|
59
|
1 |
|
|
|
60
|
1 |
|
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) { |
|
61
|
1 |
|
if (null === $contentType = $this->contentTypeNegotiator->negotiate($request)) { |
|
62
|
|
|
return $this->responseManager->createFromApiProblem( |
|
63
|
|
|
new UnsupportedMediaType( |
|
64
|
1 |
|
$request->getHeaderLine('Content-Type'), |
|
65
|
|
|
$this->contentTypeNegotiator->getSupportedMediaTypes() |
|
66
|
|
|
), |
|
67
|
|
|
$accept->getValue() |
|
68
|
3 |
|
); |
|
69
|
|
|
} |
|
70
|
3 |
|
|
|
71
|
2 |
|
$request = $request->withAttribute('contentType', $contentType->getValue()); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
|
|
74
|
1 |
|
return $handler->handle($request); |
|
75
|
1 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|