Skip to content

Commit 8160634

Browse files
committed
wip
1 parent b6cdd87 commit 8160634

File tree

5 files changed

+66
-74
lines changed

5 files changed

+66
-74
lines changed

src/Concerns/Attributes.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ trait Attributes
2020
/**
2121
* @return void
2222
*/
23-
public static function useMinimalAttributes($value = true)
23+
public static function useMinimalAttributes()
2424
{
25-
App::instance(self::MINIMAL_ATTRIBUTES_KEY, $value);
25+
App::instance(self::MINIMAL_ATTRIBUTES_KEY, true);
26+
}
27+
28+
/**
29+
* @return bool
30+
*/
31+
private static function minimalAttributes()
32+
{
33+
return App::bound(self::MINIMAL_ATTRIBUTES_KEY)
34+
? App::make(self::MINIMAL_ATTRIBUTES_KEY)
35+
: false;
2636
}
2737

2838
/**
@@ -53,13 +63,6 @@ private function resolveAttributes(Request $request)
5363
*/
5464
private function requestedFields(Request $request)
5565
{
56-
return Fields::getInstance()->parse($request, $this->toType($request), self::resolveMinimalAttributes());
57-
}
58-
59-
private static function resolveMinimalAttributes(): bool
60-
{
61-
return App::bound(self::MINIMAL_ATTRIBUTES_KEY)
62-
? (bool) App::make(self::MINIMAL_ATTRIBUTES_KEY)
63-
: false;
66+
return Fields::getInstance()->parse($request, $this->toType($request), self::minimalAttributes());
6467
}
6568
}

src/Concerns/Identification.php

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException;
1212
use TiMacDonald\JsonApi\ResourceIdentifier;
1313

14+
/**
15+
* @internal
16+
*/
1417
trait Identification
1518
{
1619
private const ID_RESOLVER_KEY = self::class.':$idResolver';
@@ -22,28 +25,11 @@ trait Identification
2225
private string|null $typeCache = null;
2326

2427
/**
25-
* @internal
26-
*
2728
* @var array<int, (callable(ResourceIdentifier): void)>
2829
*/
2930
private array $resourceIdentifierCallbacks = [];
3031

3132
/**
32-
* @api
33-
*
34-
* @param (callable(ResourceIdentifier): void) $callback
35-
* @return $this
36-
*/
37-
public function withResourceIdentifier(callable $callback)
38-
{
39-
$this->resourceIdentifierCallbacks[] = $callback;
40-
41-
return $this;
42-
}
43-
44-
/**
45-
* @api
46-
*
4733
* @param (callable(mixed): string) $callback
4834
* @return void
4935
*/
@@ -53,8 +39,6 @@ public static function resolveIdUsing(callable $callback)
5339
}
5440

5541
/**
56-
* @api
57-
*
5842
* @param (callable(mixed): string) $callback
5943
* @return void
6044
*/
@@ -64,44 +48,12 @@ public static function resolveTypeUsing(callable $callback)
6448
}
6549

6650
/**
67-
* @internal
68-
*
69-
* @return string
70-
*/
71-
public function toUniqueResourceIdentifier(Request $request)
72-
{
73-
return "type:{$this->resolveType($request)};id:{$this->resolveId($request)};";
74-
}
75-
76-
/**
77-
* @internal
78-
*
79-
* @return string
80-
*/
81-
private function resolveId(Request $request)
82-
{
83-
return $this->idCache ??= $this->toId($request);
84-
}
85-
86-
/**
87-
* @internal
88-
*
89-
* @return string
90-
*/
91-
private function resolveType(Request $request)
92-
{
93-
return $this->typeCache ??= $this->toType($request);
94-
}
95-
96-
/**
97-
* @internal
98-
*
9951
* @return (callable(mixed, Request): string)
10052
*/
10153
private static function idResolver()
10254
{
10355
if (! App::bound(self::ID_RESOLVER_KEY)) {
104-
App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
56+
return App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
10557
if (! $resource instanceof Model) {
10658
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
10759
}
@@ -117,14 +69,12 @@ private static function idResolver()
11769
}
11870

11971
/**
120-
* @internal
121-
*
12272
* @return (callable(mixed, Request): string)
12373
*/
12474
private static function typeResolver()
12575
{
12676
if (! App::bound(self::TYPE_RESOLVER_KEY)) {
127-
App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
77+
return App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
12878
if (! $resource instanceof Model) {
12979
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
13080
}
@@ -136,17 +86,56 @@ private static function typeResolver()
13686
return App::make(self::TYPE_RESOLVER_KEY);
13787
}
13888

89+
/**
90+
* @param (callable(ResourceIdentifier): void) $callback
91+
* @return $this
92+
*/
93+
public function pipeResourceIdentifier(callable $callback)
94+
{
95+
$this->resourceIdentifierCallbacks[] = $callback;
96+
97+
return $this;
98+
}
99+
139100
/**
140101
* @internal
141102
*
142103
* @return ResourceIdentifier
143104
*/
144105
public function resolveResourceIdentifier(Request $request)
145106
{
146-
return tap($this->toResourceIdentifier($request), function (ResourceIdentifier $identifier): void {
107+
return with($this->toResourceIdentifier($request), function (ResourceIdentifier $identifier): ResourceIdentifier {
147108
foreach ($this->resourceIdentifierCallbacks as $callback) {
148-
$callback($identifier);
109+
$identifier = $callback($identifier);
149110
}
111+
112+
return $identifier;
150113
});
151114
}
115+
116+
/**
117+
* @internal
118+
*
119+
* @return array{0: string, 1: string}
120+
*/
121+
public function uniqueKey(Request $request)
122+
{
123+
return [$this->resolveType($request), $this->resolveId($request)];
124+
}
125+
126+
/**
127+
* @return string
128+
*/
129+
private function resolveId(Request $request)
130+
{
131+
return $this->idCache ??= $this->toId($request);
132+
}
133+
134+
/**
135+
* @return string
136+
*/
137+
private function resolveType(Request $request)
138+
{
139+
return $this->typeCache ??= $this->toType($request);
140+
}
152141
}

src/JsonApiResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function with(Request $request)
122122
{
123123
return [
124124
...($included = $this->included($request)
125-
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
125+
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
126126
->values()
127127
->all()) ? ['included' => $included] : [],
128128
'jsonapi' => self::serverImplementationResolver()($request),

src/JsonApiResourceCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function toResourceLink(Request $request)
3737
private function resolveResourceIdentifiers(Request $request)
3838
{
3939
return $this->collection
40-
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
40+
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
4141
->map(fn (JsonApiResource $resource): ResourceIdentifier => $resource->resolveResourceIdentifier($request));
4242
}
4343

@@ -50,7 +50,7 @@ public function with(Request $request)
5050
...($included = $this->collection
5151
->map(fn (JsonApiResource $resource): Collection => $resource->included($request))
5252
->flatten()
53-
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
53+
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
5454
->values()
5555
->all()) ? ['included' => $included] : [],
5656
'jsonapi' => JsonApiResource::serverImplementationResolver()($request),

tests/Feature/JsonApiTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function toResourceLink($request): RelationshipObject
380380
Link::related('profile-external.com')->withMeta([
381381
'profile-external.com' => 'meta',
382382
]),
383-
])->withResourceIdentifier(
383+
])->pipeResourceIdentifier(
384384
// This should not be in the response.
385385
fn (ResourceIdentifier $identifier) => $identifier->withMeta([
386386
'profile-external-resource-identifier' => 'meta',
@@ -434,7 +434,7 @@ public function toResourceLink($request): RelationshipObject
434434
Link::related('avatar-external.com')->withMeta([
435435
'avatar-external.com' => 'meta',
436436
]),
437-
])->withResourceIdentifier(
437+
])->pipeResourceIdentifier(
438438
fn (ResourceIdentifier $identifier) => $identifier->withMeta([
439439
'avatar-external-resource-identifier' => 'meta',
440440
])
@@ -500,7 +500,7 @@ public static function collection($resource): JsonApiResourceCollection
500500
Link::related('posts-internal-collection.com')->withMeta([
501501
'posts-internal-collection.com' => 'meta',
502502
]),
503-
])->withResourceIdentifier(fn ($identifier) => $identifier->withMeta([
503+
])->pipeResourceIdentifier(fn ($identifier) => $identifier->withMeta([
504504
'posts-internal-collection-resource-identifier' => 'meta',
505505
]))
506506
);
@@ -513,7 +513,7 @@ public static function collection($resource): JsonApiResourceCollection
513513
])->withMeta([
514514
'posts-external-resource-link' => 'meta',
515515
]))
516-
->map(fn ($post) => $post->withResourceIdentifier(static fn ($identifier) => $identifier->withMeta([
516+
->map(fn ($post) => $post->pipeResourceIdentifier(static fn ($identifier) => $identifier->withMeta([
517517
'posts-external-resource-identifier' => 'meta',
518518
]))->withMeta([
519519
'posts-external' => 'meta',

0 commit comments

Comments
 (0)