/
JsonApi.php
143 lines (123 loc) · 4 KB
/
JsonApi.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace WoohooLabs\Yin\JsonApi;
use Psr\Http\Message\ResponseInterface;
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
use WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface;
use WoohooLabs\Yin\JsonApi\Exception\InclusionUnsupported;
use WoohooLabs\Yin\JsonApi\Exception\JsonApiExceptionInterface;
use WoohooLabs\Yin\JsonApi\Exception\SortingUnsupported;
use WoohooLabs\Yin\JsonApi\Hydrator\HydratorInterface;
use WoohooLabs\Yin\JsonApi\Hydrator\UpdateRelationshipHydratorInterface;
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequestInterface;
use WoohooLabs\Yin\JsonApi\Request\Pagination\PaginationFactory;
use WoohooLabs\Yin\JsonApi\Response\Responder;
use WoohooLabs\Yin\JsonApi\Serializer\JsonSerializer;
use WoohooLabs\Yin\JsonApi\Serializer\SerializerInterface;
class JsonApi
{
/**
* @var JsonApiRequestInterface
*/
public $request;
/**
* @var ResponseInterface
*/
public $response;
/**
* @var ExceptionFactoryInterface
*/
protected $exceptionFactory;
/**
* @var SerializerInterface
*/
protected $serializer;
public function __construct(
JsonApiRequestInterface $request,
ResponseInterface $response,
?ExceptionFactoryInterface $exceptionFactory = null,
?SerializerInterface $serializer = null
) {
$this->request = $request;
$this->response = $response;
$this->exceptionFactory = $exceptionFactory ?? new DefaultExceptionFactory();
$this->serializer = $serializer ?? new JsonSerializer();
}
public function getRequest(): JsonApiRequestInterface
{
return $this->request;
}
public function setRequest(JsonApiRequestInterface $request): void
{
$this->request = $request;
}
public function getResponse(): ResponseInterface
{
return $this->response;
}
public function setResponse(ResponseInterface $response): void
{
$this->response = $response;
}
public function getExceptionFactory(): ExceptionFactoryInterface
{
return $this->exceptionFactory;
}
public function setExceptionFactory(ExceptionFactoryInterface $exceptionFactory): void
{
$this->exceptionFactory = $exceptionFactory;
}
public function getPaginationFactory(): PaginationFactory
{
return new PaginationFactory($this->request);
}
public function respond(): Responder
{
return new Responder($this->request, $this->response, $this->exceptionFactory, $this->serializer);
}
/**
* @param mixed $object
* @return mixed
*/
public function hydrate(HydratorInterface $hydrator, $object)
{
return $hydrator->hydrate($this->request, $this->exceptionFactory, $object);
}
/**
* @param mixed $object
* @return mixed
*/
public function hydrateRelationship(
string $relationship,
UpdateRelationshipHydratorInterface $hydrator,
$object
) {
return $hydrator->hydrateRelationship($relationship, $this->request, $this->exceptionFactory, $object);
}
/**
* Disables inclusion of related resources.
*
* If the current request asks for inclusion of related resources, it throws an InclusionNotSupported exception.
*
* @throws InclusionUnsupported|JsonApiExceptionInterface
*/
public function disableIncludes(): void
{
if ($this->request->getQueryParam("include") !== null) {
throw $this->exceptionFactory->createInclusionUnsupportedException($this->request);
}
}
/**
* Disables sorting.
*
* If the current request contains sorting criteria, it throws a SortingNotSupported exception.
*
* @throws SortingUnsupported|JsonApiExceptionInterface
*/
public function disableSorting(): void
{
if ($this->request->getQueryParam("sort") !== null) {
throw $this->exceptionFactory->createSortingUnsupportedException($this->request);
}
}
}