Skip to content

Commit

Permalink
Bind and resolver type / id resolver from container
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Oct 16, 2023
1 parent f112ff6 commit 0e9f0cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 59 deletions.
83 changes: 31 additions & 52 deletions src/Concerns/Identification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use TiMacDonald\JsonApi\Exceptions\ResourceIdentificationException;
use TiMacDonald\JsonApi\ResourceIdentifier;

trait Identification
{
private string|null $idCache = null;
private const ID_RESOLVER_KEY = self::class.':$idResolver';

private string|null $typeCache = null;
private const TYPE_RESOLVER_KEY = self::class.':$typeResolver';

/**
* @internal
*
* @var (callable(mixed, Request): string)|null
*/
private static $idResolver;
private string|null $idCache = null;

/**
* @internal
*
* @var (callable(mixed, Request): string)|null
*/
private static $typeResolver;
private string|null $typeCache = null;

/**
* @internal
Expand Down Expand Up @@ -58,7 +49,7 @@ public function withResourceIdentifier(callable $callback)
*/
public static function resolveIdUsing(callable $callback)
{
self::$idResolver = $callback;
App::instance(self::ID_RESOLVER_KEY, $callback);
}

/**
Expand All @@ -69,27 +60,7 @@ public static function resolveIdUsing(callable $callback)
*/
public static function resolveTypeUsing(callable $callback)
{
self::$typeResolver = $callback;
}

/**
* @internal
*
* @return void
*/
public static function resolveIdNormally()
{
self::$idResolver = null;
}

/**
* @internal
*
* @return void
*/
public static function resolveTypeNormally()
{
self::$typeResolver = null;
App::instance(self::TYPE_RESOLVER_KEY, $callback);
}

/**
Expand Down Expand Up @@ -129,16 +100,20 @@ private function resolveType(Request $request)
*/
private static function idResolver()
{
return self::$idResolver ??= function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
}

/**
* @phpstan-ignore-next-line
*/
return (string) $resource->getKey();
};
if (! App::bound(self::ID_RESOLVER_KEY)) {
App::instance(self::ID_RESOLVER_KEY, function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineIdFor($resource);
}

/**
* @phpstan-ignore-next-line
*/
return (string) $resource->getKey();
});
}

return App::make(self::ID_RESOLVER_KEY);
}

/**
Expand All @@ -148,13 +123,17 @@ private static function idResolver()
*/
private static function typeResolver()
{
return self::$typeResolver ??= function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
}
if (! App::bound(self::TYPE_RESOLVER_KEY)) {
App::instance(self::TYPE_RESOLVER_KEY, function (mixed $resource, Request $request): string {
if (! $resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineTypeFor($resource);
}

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

return Str::camel($resource->getTable());
};
return App::make(self::TYPE_RESOLVER_KEY);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions tests/Feature/JsonApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ public function testItCanCustomiseTheTypeResolution(): void
],
]);
$this->assertValidJsonApi($response);

JsonApiResource::resolveTypeNormally();
}

public function testItCanCustomiseTheIdResolution(): void
Expand All @@ -229,8 +227,6 @@ public function testItCanCustomiseTheIdResolution(): void
],
]);
$this->assertValidJsonApi($response);

JsonApiResource::resolveIdNormally();
}

public function testItExcludesEmptyResourceIdentifierMeta(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,5 @@ public function testItCanUseHash()
$links = json_encode($resource->toArray($request)['links']);

$this->assertSame('{"foo":{"href":"http:\/\/foo.com"}}', $links);

JsonApiResource::resolveIdNormally();
JsonApiResource::resolveTypeNormally();
}
}

0 comments on commit 0e9f0cc

Please sign in to comment.