Skip to content

Commit 57ffa86

Browse files
committed
Update resource definition API to allow extending schema
1 parent ae46a35 commit 57ffa86

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/JsonApi.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ public function __construct(string $baseUrl)
3131
$this->baseUrl = $baseUrl;
3232
}
3333

34-
public function resource(string $type, $adapter, Closure $buildSchema = null): void
34+
public function resource(string $type): ResourceType
3535
{
36-
$this->resources[$type] = new ResourceType($type, $adapter, $buildSchema);
36+
if (! isset($this->resources[$type])) {
37+
$this->resources[$type] = new ResourceType($type);
38+
}
39+
40+
return $this->resources[$type];
3741
}
3842

3943
public function getResources(): array

src/ResourceType.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,51 @@
22

33
namespace Tobyz\JsonApiServer;
44

5-
use Closure;
65
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
76
use Tobyz\JsonApiServer\Schema\Type;
87

98
final class ResourceType
109
{
1110
private $type;
1211
private $adapter;
13-
private $buildSchema;
12+
private $schemaCallbacks = [];
1413
private $schema;
1514

16-
public function __construct(string $type, AdapterInterface $adapter, Closure $buildSchema = null)
15+
public function __construct(string $type)
1716
{
1817
$this->type = $type;
19-
$this->adapter = $adapter;
20-
$this->buildSchema = $buildSchema;
2118
}
2219

2320
public function getType(): string
2421
{
2522
return $this->type;
2623
}
2724

25+
public function adapter(AdapterInterface $adapter)
26+
{
27+
$this->adapter = $adapter;
28+
29+
return $this;
30+
}
31+
2832
public function getAdapter(): AdapterInterface
2933
{
3034
return $this->adapter;
3135
}
3236

37+
public function schema(callable $callback)
38+
{
39+
$this->schemaCallbacks[] = $callback;
40+
41+
return $this;
42+
}
43+
3344
public function getSchema(): Type
3445
{
3546
if (! $this->schema) {
3647
$this->schema = new Type;
3748

38-
if ($this->buildSchema) {
39-
($this->buildSchema)($this->schema);
40-
}
49+
run_callbacks($this->schemaCallbacks, [$this->schema]);
4150
}
4251

4352
return $this->schema;

0 commit comments

Comments
 (0)