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 a7fc989 commit 32e7a0c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
26 changes: 6 additions & 20 deletions src/Concerns/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
namespace TiMacDonald\JsonApi\Concerns;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use TiMacDonald\JsonApi\ServerImplementation;

trait Implementation
{
/**
* @internal
*
* @var (callable(): JsonApiServerImplementation)|null
*/
private static $serverImplementationResolver;

/**
* @api
*
Expand All @@ -24,26 +18,18 @@ trait Implementation
*/
public static function resolveServerImplementationUsing(callable $callback)
{
self::$serverImplementationResolver = $callback;
}

/**
* @internal
*
* @return void
*/
public static function resolveServerImplementationNormally()
{
self::$serverImplementationResolver = null;
App::instance(self::class.':$serverImplementationResolver', $callback);
}

/**
* @internal
*
* @return (callable(Request): JsonApiServerImplementation)
* @return (callable(Request): (JsonApiServerImplementation|null))
*/
public static function serverImplementationResolver()
{
return self::$serverImplementationResolver ??= fn (Request $request): ServerImplementation => new ServerImplementation('1.0');
return App::bound(self::class.':$serverImplementationResolver')
? App::make(self::class.':$serverImplementationResolver')
: fn () => null;
}
}
11 changes: 10 additions & 1 deletion src/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public function toResourceIdentifier(Request $request)
return new ResourceIdentifier($this->resolveType($request), $this->resolveId($request));
}

/**
* @return JsonApiServerImplementation|null
*/
public static function toServerImplementation(Request $request)
{
return self::serverImplementationResolver()($request);
}

/**
* @return array{id: string, type: string, attributes?: stdClass, relationships?: stdClass, meta?: stdClass, links?: stdClass}
*/
Expand Down Expand Up @@ -125,7 +133,8 @@ public function with(Request $request)
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->values()
->all()) ? ['included' => $included] : [],
'jsonapi' => self::serverImplementationResolver()($request),
...($implementation = self::toServerImplementation($request))
? ['jsonapi' => $implementation] : [],
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/JsonApiResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function resolveResourceIdentifiers(Request $request)
}

/**
* @return array{included?: array<int, JsonApiResource>, jsonapi: JsonApiServerImplementation}
* @return array{included?: array<int, JsonApiResource>, jsonapi?: JsonApiServerImplementation}
*/
public function with(Request $request)
{
Expand All @@ -53,7 +53,8 @@ public function with(Request $request)
->uniqueStrict(fn (JsonApiResource $resource): array => $resource->uniqueKey($request))
->values()
->all()) ? ['included' => $included] : [],
'jsonapi' => JsonApiResource::serverImplementationResolver()($request),
...($implementation = $this->collects::toServerImplementation($request))
? ['jsonapi' => $implementation] : [],
];
}

Expand Down
7 changes: 0 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ public function setUp(): void
$this->withoutExceptionHandling();
}

protected function tearDown(): void
{
parent::tearDown();

JsonApiResource::resolveServerImplementationNormally();
}

protected function assertValidJsonApi(TestResponse|string|array $data): void
{
if ($data instanceof TestResponse) {
Expand Down

0 comments on commit 32e7a0c

Please sign in to comment.