Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 16, 2023
1 parent b6cdd87 commit 8160634
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 74 deletions.
23 changes: 13 additions & 10 deletions src/Concerns/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ trait Attributes
/**
* @return void
*/
public static function useMinimalAttributes($value = true)
public static function useMinimalAttributes()
{
App::instance(self::MINIMAL_ATTRIBUTES_KEY, $value);
App::instance(self::MINIMAL_ATTRIBUTES_KEY, true);
}

/**
* @return bool
*/
private static function minimalAttributes()
{
return App::bound(self::MINIMAL_ATTRIBUTES_KEY)
? App::make(self::MINIMAL_ATTRIBUTES_KEY)
: false;
}

/**
Expand Down Expand Up @@ -53,13 +63,6 @@ private function resolveAttributes(Request $request)
*/
private function requestedFields(Request $request)
{
return Fields::getInstance()->parse($request, $this->toType($request), self::resolveMinimalAttributes());
}

private static function resolveMinimalAttributes(): bool
{
return App::bound(self::MINIMAL_ATTRIBUTES_KEY)
? (bool) App::make(self::MINIMAL_ATTRIBUTES_KEY)
: false;
return Fields::getInstance()->parse($request, $this->toType($request), self::minimalAttributes());
}
}
103 changes: 46 additions & 57 deletions src/Concerns/Identification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException;
use TiMacDonald\JsonApi\ResourceIdentifier;

/**
* @internal
*/
trait Identification
{
private const ID_RESOLVER_KEY = self::class.':$idResolver';
Expand All @@ -22,28 +25,11 @@ trait Identification
private string|null $typeCache = null;

/**
* @internal
*
* @var array<int, (callable(ResourceIdentifier): void)>
*/
private array $resourceIdentifierCallbacks = [];

/**
* @api
*
* @param (callable(ResourceIdentifier): void) $callback
* @return $this
*/
public function withResourceIdentifier(callable $callback)
{
$this->resourceIdentifierCallbacks[] = $callback;

return $this;
}

/**
* @api
*
* @param (callable(mixed): string) $callback
* @return void
*/
Expand All @@ -53,8 +39,6 @@ public static function resolveIdUsing(callable $callback)
}

/**
* @api
*
* @param (callable(mixed): string) $callback
* @return void
*/
Expand All @@ -64,44 +48,12 @@ public static function resolveTypeUsing(callable $callback)
}

/**
* @internal
*
* @return string
*/
public function toUniqueResourceIdentifier(Request $request)
{
return "type:{$this->resolveType($request)};id:{$this->resolveId($request)};";
}

/**
* @internal
*
* @return string
*/
private function resolveId(Request $request)
{
return $this->idCache ??= $this->toId($request);
}

/**
* @internal
*
* @return string
*/
private function resolveType(Request $request)
{
return $this->typeCache ??= $this->toType($request);
}

/**
* @internal
*
* @return (callable(mixed, Request): string)
*/
private static function idResolver()
{
if (! App::bound(self::ID_RESOLVER_KEY)) {
App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
return App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
}
Expand All @@ -117,14 +69,12 @@ private static function idResolver()
}

/**
* @internal
*
* @return (callable(mixed, Request): string)
*/
private static function typeResolver()
{
if (! App::bound(self::TYPE_RESOLVER_KEY)) {
App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
return App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
}
Expand All @@ -136,17 +86,56 @@ private static function typeResolver()
return App::make(self::TYPE_RESOLVER_KEY);
}

/**
* @param (callable(ResourceIdentifier): void) $callback
* @return $this
*/
public function pipeResourceIdentifier(callable $callback)
{
$this->resourceIdentifierCallbacks[] = $callback;

return $this;
}

/**
* @internal
*
* @return ResourceIdentifier
*/
public function resolveResourceIdentifier(Request $request)
{
return tap($this->toResourceIdentifier($request), function (ResourceIdentifier $identifier): void {
return with($this->toResourceIdentifier($request), function (ResourceIdentifier $identifier): ResourceIdentifier {
foreach ($this->resourceIdentifierCallbacks as $callback) {
$callback($identifier);
$identifier = $callback($identifier);
}

return $identifier;
});
}

/**
* @internal
*
* @return array{0: string, 1: string}
*/
public function uniqueKey(Request $request)
{
return [$this->resolveType($request), $this->resolveId($request)];
}

/**
* @return string
*/
private function resolveId(Request $request)
{
return $this->idCache ??= $this->toId($request);
}

/**
* @return string
*/
private function resolveType(Request $request)
{
return $this->typeCache ??= $this->toType($request);
}
}
2 changes: 1 addition & 1 deletion src/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function with(Request $request)
{
return [
...($included = $this->included($request)
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->values()
->all()) ? ['included' => $included] : [],
'jsonapi' => self::serverImplementationResolver()($request),
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function toResourceLink(Request $request)
private function resolveResourceIdentifiers(Request $request)
{
return $this->collection
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->map(fn (JsonApiResource $resource): ResourceIdentifier => $resource->resolveResourceIdentifier($request));
}

Expand All @@ -50,7 +50,7 @@ public function with(Request $request)
...($included = $this->collection
->map(fn (JsonApiResource $resource): Collection => $resource->included($request))
->flatten()
->uniqueStrict(fn (JsonApiResource $resource): string => $resource->toUniqueResourceIdentifier($request))
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->values()
->all()) ? ['included' => $included] : [],
'jsonapi' => JsonApiResource::serverImplementationResolver()($request),
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/JsonApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public function toResourceLink($request): RelationshipObject
Link::related('profile-external.com')->withMeta([
'profile-external.com' => 'meta',
]),
])->withResourceIdentifier(
])->pipeResourceIdentifier(
// This should not be in the response.
fn (ResourceIdentifier $identifier) => $identifier->withMeta([
'profile-external-resource-identifier' => 'meta',
Expand Down Expand Up @@ -434,7 +434,7 @@ public function toResourceLink($request): RelationshipObject
Link::related('avatar-external.com')->withMeta([
'avatar-external.com' => 'meta',
]),
])->withResourceIdentifier(
])->pipeResourceIdentifier(
fn (ResourceIdentifier $identifier) => $identifier->withMeta([
'avatar-external-resource-identifier' => 'meta',
])
Expand Down Expand Up @@ -500,7 +500,7 @@ public static function collection($resource): JsonApiResourceCollection
Link::related('posts-internal-collection.com')->withMeta([
'posts-internal-collection.com' => 'meta',
]),
])->withResourceIdentifier(fn ($identifier) => $identifier->withMeta([
])->pipeResourceIdentifier(fn ($identifier) => $identifier->withMeta([
'posts-internal-collection-resource-identifier' => 'meta',
]))
);
Expand All @@ -513,7 +513,7 @@ public static function collection($resource): JsonApiResourceCollection
])->withMeta([
'posts-external-resource-link' => 'meta',
]))
->map(fn ($post) => $post->withResourceIdentifier(static fn ($identifier) => $identifier->withMeta([
->map(fn ($post) => $post->pipeResourceIdentifier(static fn ($identifier) => $identifier->withMeta([
'posts-external-resource-identifier' => 'meta',
]))->withMeta([
'posts-external' => 'meta',
Expand Down

0 comments on commit 8160634

Please sign in to comment.