Skip to content

Commit

Permalink
improve types and identification caching
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Dec 7, 2021
1 parent 85f8e6e commit ea0360e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/Concerns/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ private function requestedAttributes(Request $request): Collection
{
return Collection::make($this->toAttributes($request))
->only($this->fields($request))
->map(fn ($value) => value($value));
->map(
/**
* @param mixed $value
* @return mixed
*/
fn ($value) => value($value)
);
}

/**
Expand Down
22 changes: 19 additions & 3 deletions src/Concerns/Identification.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function toResourceIdentifier(Request $request): array
{
return [
'data' => [
'id' => $this->toId($request),
'type' => $this->toType($request),
'id' => $this->resolveId($request),
'type' => $this->resolveType($request),
],
];
}
Expand All @@ -66,7 +66,23 @@ public function toResourceIdentifier(Request $request): array
*/
public function toUniqueResourceIdentifier(Request $request): string
{
return "type:{$this->toType($request)} id:{$this->toId($request)}";
return "type:{$this->resolveType($request)} id:{$this->resolveId($request)}";
}

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

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

/**
Expand Down
1 change: 1 addition & 0 deletions src/Concerns/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private function requestedRelationshipsAsIdentifiers(Request $request): Collecti
->map(
/**
* @param JsonApiResource|JsonApiResourceCollection|UnknownRelationship $resource
* @return mixed
*/
fn ($resource) => $resource->toResourceIdentifier($request)
);
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/ResourceIdentificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ class ResourceIdentificationException extends RuntimeException
*/
public static function attemptingToDetermineIdFor($model): self
{
return new self('Unable to resolve resource object id for '.self::resolveType($model).'.');
return new self('Unable to resolve resource object id for '.self::determineType($model).'.');
}

/**
* @param mixed $model
*/
public static function attemptingToDetermineTypeFor($model): self
{
return new self('Unable to resolve resource object type for '.self::resolveType($model).'.');
return new self('Unable to resolve resource object type for '.self::determineType($model).'.');
}

/**
* @param mixed $model
*/
private static function resolveType($model): string
private static function determineType($model): string
{
return is_object($model)
? get_class($model)
Expand Down
12 changes: 6 additions & 6 deletions src/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ protected function toMeta(Request $request): array

protected function toId(Request $request): string
{
return $this->rememberId(fn () => (self::$idResolver ??= static function ($resource): string {
return (self::$idResolver ??= static function ($resource): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
}

return (string) $resource->getKey();
})($this->resource));
})($this->resource);
}

protected function toType(Request $request): string
{
return $this->rememberType(fn () => (self::$typeResolver ??= static function ($resource): string {
return (self::$typeResolver ??= static function ($resource): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
}

return Str::camel($resource->getTable());
})($this->resource));
})($this->resource);
}

/**
Expand All @@ -91,8 +91,8 @@ protected function toType(Request $request): string
public function toArray($request): array
{
return [
'id' => $this->toId($request),
'type' => $this->toType($request),
'id' => $this->resolveId($request),
'type' => $this->resolveType($request),
'attributes' => (object) $this->requestedAttributes($request)->all(),
'relationships' => (object) $this->requestedRelationshipsAsIdentifiers($request)->all(),
'meta' => (object) $this->toMeta($request),
Expand Down

0 comments on commit ea0360e

Please sign in to comment.