From d889701ec681a15a9624d060dfddc90c5f6cf1a3 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 16 Nov 2019 10:15:03 +1030 Subject: [PATCH] Revert "Update resource definition API to allow extending schema" This reverts commit 57ffa867c895eef478d3f923bca81b3f8659bca2. --- src/JsonApi.php | 8 ++------ src/ResourceType.php | 25 ++++++++----------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/JsonApi.php b/src/JsonApi.php index e7cf939..a065094 100644 --- a/src/JsonApi.php +++ b/src/JsonApi.php @@ -31,13 +31,9 @@ final class JsonApi implements RequestHandlerInterface $this->baseUrl = $baseUrl; } - public function resource(string $type): ResourceType + public function resource(string $type, $adapter, Closure $buildSchema = null): void { - if (! isset($this->resources[$type])) { - $this->resources[$type] = new ResourceType($type); - } - - return $this->resources[$type]; + $this->resources[$type] = new ResourceType($type, $adapter, $buildSchema); } public function getResources(): array diff --git a/src/ResourceType.php b/src/ResourceType.php index 8ef2976..09f403d 100644 --- a/src/ResourceType.php +++ b/src/ResourceType.php @@ -2,6 +2,7 @@ namespace Tobyz\JsonApiServer; +use Closure; use Tobyz\JsonApiServer\Adapter\AdapterInterface; use Tobyz\JsonApiServer\Schema\Type; @@ -9,12 +10,14 @@ final class ResourceType { private $type; private $adapter; - private $schemaCallbacks = []; + private $buildSchema; private $schema; - public function __construct(string $type) + public function __construct(string $type, AdapterInterface $adapter, Closure $buildSchema = null) { $this->type = $type; + $this->adapter = $adapter; + $this->buildSchema = $buildSchema; } public function getType(): string @@ -22,31 +25,19 @@ final class ResourceType return $this->type; } - public function adapter(AdapterInterface $adapter) - { - $this->adapter = $adapter; - - return $this; - } - public function getAdapter(): AdapterInterface { return $this->adapter; } - public function schema(callable $callback) - { - $this->schemaCallbacks[] = $callback; - - return $this; - } - public function getSchema(): Type { if (! $this->schema) { $this->schema = new Type; - run_callbacks($this->schemaCallbacks, [$this->schema]); + if ($this->buildSchema) { + ($this->buildSchema)($this->schema); + } } return $this->schema;