-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathJsonApiResource.php
173 lines (154 loc) · 4.59 KB
/
JsonApiResource.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace TiMacDonald\JsonApi;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\PotentiallyMissing;
use Illuminate\Support\Collection;
use stdClass;
use function property_exists;
abstract class JsonApiResource extends JsonResource
{
use Concerns\Attributes;
use Concerns\Caching;
use Concerns\Identification;
use Concerns\Implementation;
use Concerns\Links;
use Concerns\Meta;
use Concerns\RelationshipLinks;
use Concerns\Relationships;
/**
* @return array<string, mixed>
*/
public function toAttributes(Request $request)
{
return [
//
];
}
/**
* @return array<string, (callable(): JsonApiResource|JsonApiResourceCollection|PotentiallyMissing)>
*/
public function toRelationships(Request $request)
{
return [
//
];
}
/**
* @return array<int, Link>
*/
public function toLinks(Request $request)
{
return [
//
];
}
/**
* @return array<string, mixed>
*/
public function toMeta(Request $request)
{
return [
//
];
}
/**
* @return string
*/
public function toId(Request $request)
{
return self::idResolver()($this->resource, $request);
}
/**
* @return string
*/
public function toType(Request $request)
{
return self::typeResolver()($this->resource, $request);
}
/**
* @return RelationshipObject
*/
public function toResourceLink(Request $request)
{
return $this->resource === null
? RelationshipObject::toOne(null)
: RelationshipObject::toOne($this->resolveResourceIdentifier($request));
}
/**
* @return ResourceIdentifier
*/
public function toResourceIdentifier(Request $request)
{
return new ResourceIdentifier($this->resolveType($request), $this->resolveId($request));
}
/**
* @return ServerImplementation|null
*/
public static function toServerImplementation(Request $request)
{
return self::serverImplementationResolver()($request);
}
/**
* @return array{id: string, type: string, attributes?: stdClass, relationships?: stdClass, meta?: stdClass, links?: stdClass}
*/
public function toArray($request)
{
return [
'id' => $this->resolveId($request),
'type' => $this->resolveType($request),
...Collection::make([
'attributes' => $this->requestedAttributes($request)->all(),
'relationships' => $this->requestedRelationshipsAsIdentifiers($request)->all(),
'links' => self::parseLinks(array_merge($this->toLinks($request), $this->links)),
'meta' => array_merge($this->toMeta($request), $this->meta),
])->filter()->map(fn ($value) => (object) $value),
];
}
/**
* @param Request $request
* @return array{included?: array<int, JsonApiResource>, jsonapi: ServerImplementation}
*/
public function with($request)
{
return [
...($included = $this->included($request)
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->values()
->all()) ? ['included' => $included] : [],
...($implementation = self::toServerImplementation($request))
? ['jsonapi' => $implementation] : [],
];
}
/**
* TODO this may be removed once all supported versions have `newCollection`.
*
* @return JsonApiResourceCollection<int, mixed>
*/
public static function collection(mixed $resource)
{
return tap(static::newCollection($resource), function (JsonApiResourceCollection $collection): void {
if (property_exists(static::class, 'preserveKeys')) {
/** @phpstan-ignore-next-line */
$collection->preserveKeys = (new static([]))->preserveKeys === true;
}
});
}
/**
* @return JsonApiResourceCollection<int, mixed>
*/
public static function newCollection(mixed $resource)
{
return new JsonApiResourceCollection($resource, static::class);
}
/**
* @param Request $request
* @return JsonResponse
*/
public function toResponse($request)
{
return tap(parent::toResponse($request)->header('Content-type', 'application/vnd.api+json'), $this->flush(...));
}
}