-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathRelationships.php
130 lines (117 loc) · 3.72 KB
/
Relationships.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
<?php
declare(strict_types=1);
namespace TiMacDonald\JsonApi\Concerns;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\PotentiallyMissing;
use Illuminate\Support\Collection;
use TiMacDonald\JsonApi\JsonApiResource;
use TiMacDonald\JsonApi\JsonApiResourceCollection;
use TiMacDonald\JsonApi\Support\Includes;
use TiMacDonald\JsonApi\Support\UnknownRelationship;
/**
* @internal
*/
trait Relationships
{
/**
* @internal
*/
private string $includePrefix = '';
/**
* @internal
*/
public function withIncludePrefix(string $prefix): self
{
return tap($this, fn (JsonApiResource $resource): string => $resource->includePrefix = "{$this->includePrefix}{$prefix}.");
}
/**
* @internal
*/
public function included(Request $request): Collection
{
return $this->requestedRelationships($request)
->map(
/**
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $include
* @return Collection|JsonApiResource|UnknownRelationship
*/
fn ($include) => $include->includable()
)
->merge($this->nestedIncluded($request))
->flatten()
->filter(
/**
* @param JsonApiResource|UnknownRelationship $resource
*/
fn ($resource): bool => $resource->shouldBePresentInIncludes()
)
->values();
}
/**
* @internal
*/
private function nestedIncluded(Request $request): Collection
{
return $this->requestedRelationships($request)
->flatMap(
/**
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $resource
*/
fn ($resource, string $key): Collection => $resource->included($request)
);
}
/**
* @internal
*/
private function requestedRelationshipsAsIdentifiers(Request $request): Collection
{
return $this->requestedRelationships($request)
->map(
/**
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $resource
* @return mixed
*/
fn ($resource) => $resource->toResourceLink($request)
);
}
/**
* @internal
*/
private function requestedRelationships(Request $request): Collection
{
return $this->rememberRequestRelationships(fn (): Collection => Collection::make($this->toRelationships($request))
->only(Includes::getInstance()->parse($request, $this->includePrefix))
->map(
/**
* @return JsonApiResource|JsonApiResourceCollection|UnknownRelationship
*/
function (Closure $value, string $prefix) {
$resource = $value();
if ($resource instanceof JsonApiResource || $resource instanceof JsonApiResourceCollection) {
return $resource->withIncludePrefix($prefix);
}
return new UnknownRelationship($resource);
}
)->reject(
/**
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $resource
*/
fn ($resource): bool => $resource instanceof PotentiallyMissing && $resource->isMissing()
));
}
/**
* @internal
*/
private function includable(): self
{
return $this;
}
/**
* @internal
*/
private function shouldBePresentInIncludes(): bool
{
return true;
}
}