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;