blob: cf91ec5a4bfdf98c16eaf6d86f71b918e560b758 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?php
namespace JsonApi;
use JsonApi\Errors\InternalServerError;
use JsonApi\Middlewares\Authentication;
use Neomerx\JsonApi\Contracts\Schema\ContainerInterface as SchemaContainerInterface;
use Neomerx\JsonApi\Exceptions\JsonApiException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
/**
* TODO.
*/
class NonJsonApiController
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function __invoke(Request $request, Response $response, $args)
{
try {
$response = $this->invoke($request, $response, $args);
} catch (JsonApiException $exception) {
$httpCode = $exception->getHttpCode();
$errors = $exception->getErrors();
$reason = count($errors) ? $errors[0]->getId() : '';
$response = $response->withStatus($httpCode)->write($reason);
}
return $response;
}
public function invoke(Request $request, Response $response, $args)
{
throw new InternalServerError();
}
protected function getUser($request)
{
return $request->getAttribute(Authentication::USER_KEY, null);
}
protected function getSchema($resource)
{
return $this->container[SchemaContainerInterface::class]->getSchema($resource);
}
}
|