Skip to content

Commit 40776bc

Browse files
committed
wip
1 parent 25ef9ab commit 40776bc

38 files changed

+451
-323
lines changed

README.md

Lines changed: 206 additions & 68 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "tobscure/json-api-server",
2+
"name": "tobyz/json-api-server",
33
"require": {
44
"php": "^7.2",
55
"doctrine/inflector": "^1.3",
@@ -17,12 +17,12 @@
1717
],
1818
"autoload": {
1919
"psr-4": {
20-
"Tobscure\\JsonApiServer\\": "src/"
20+
"Tobyz\\JsonApiServer\\": "src/"
2121
}
2222
},
2323
"autoload-dev": {
2424
"psr-4": {
25-
"Tobscure\\Tests\\JsonApiServer\\": "tests/"
25+
"Tobyz\\Tests\\JsonApiServer\\": "tests/"
2626
}
2727
},
2828
"require-dev": {

src/Adapter/AdapterInterface.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Adapter;
3+
namespace Tobyz\JsonApiServer\Adapter;
44

5-
use Tobscure\JsonApiServer\Schema\Attribute;
6-
use Tobscure\JsonApiServer\Schema\HasMany;
7-
use Tobscure\JsonApiServer\Schema\HasOne;
5+
use Tobyz\JsonApiServer\Schema\Attribute;
6+
use Tobyz\JsonApiServer\Schema\HasMany;
7+
use Tobyz\JsonApiServer\Schema\HasOne;
8+
use Tobyz\JsonApiServer\Schema\Relationship;
89

910
interface AdapterInterface
1011
{
12+
public function handles($model);
13+
1114
public function create();
1215

1316
public function query();
@@ -46,5 +49,7 @@ public function sortByAttribute($query, Attribute $attribute, string $direction)
4649

4750
public function paginate($query, int $limit, int $offset);
4851

49-
public function load(array $models, array $relationships, \Closure $scope);
52+
public function load(array $models, array $relationships);
53+
54+
public function loadIds(array $models, Relationship $relationship);
5055
}

src/Adapter/EloquentAdapter.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Adapter;
3+
namespace Tobyz\JsonApiServer\Adapter;
44

55
use Illuminate\Database\Eloquent\Collection;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
88
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
99
use Illuminate\Database\Eloquent\SoftDeletes;
10-
use Tobscure\JsonApiServer\Schema\Attribute;
11-
use Tobscure\JsonApiServer\Schema\HasMany;
12-
use Tobscure\JsonApiServer\Schema\HasOne;
13-
use Tobscure\JsonApiServer\Schema\Relationship;
10+
use Tobyz\JsonApiServer\Schema\Attribute;
11+
use Tobyz\JsonApiServer\Schema\HasMany;
12+
use Tobyz\JsonApiServer\Schema\HasOne;
13+
use Tobyz\JsonApiServer\Schema\Relationship;
1414

1515
class EloquentAdapter implements AdapterInterface
1616
{
@@ -28,6 +28,11 @@ public function __construct($model)
2828
}
2929
}
3030

31+
public function handles($model)
32+
{
33+
return $model instanceof $this->model;
34+
}
35+
3136
public function create()
3237
{
3338
return $this->model->newInstance();
@@ -186,13 +191,9 @@ public function paginate($query, int $limit, int $offset)
186191
$query->take($limit)->skip($offset);
187192
}
188193

189-
public function load(array $models, array $trail, \Closure $scope)
194+
public function load(array $models, array $trail)
190195
{
191-
(new Collection($models))->load([
192-
$this->relationshipTrailToPath($trail) => function ($relation) use ($scope) {
193-
$scope($relation->getQuery());
194-
}
195-
]);
196+
(new Collection($models))->loadMissing($this->relationshipTrailToPath($trail));
196197
}
197198

198199
public function loadIds(array $models, Relationship $relationship)
@@ -208,7 +209,7 @@ public function loadIds(array $models, Relationship $relationship)
208209
return;
209210
}
210211

211-
(new Collection($models))->load([
212+
(new Collection($models))->loadMissing([
212213
$property => function ($query) use ($relation) {
213214
$query->select([
214215
$relation->getRelated()->getKeyName(),

src/Api.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer;
3+
namespace Tobyz\JsonApiServer;
44

55
use Closure;
66
use JsonApiPhp\JsonApi;
77
use Psr\Http\Message\ResponseInterface as Response;
88
use Psr\Http\Message\ServerRequestInterface as Request;
99
use Psr\Http\Server\RequestHandlerInterface;
10-
use Tobscure\JsonApiServer\Exception\BadRequestException;
11-
use Tobscure\JsonApiServer\Exception\MethodNotAllowedException;
12-
use Tobscure\JsonApiServer\Exception\NotImplementedException;
13-
use Tobscure\JsonApiServer\Exception\ResourceNotFoundException;
14-
use Tobscure\JsonApiServer\Handler\Concerns\FindsResources;
10+
use Tobyz\JsonApiServer\Exception\BadRequestException;
11+
use Tobyz\JsonApiServer\Exception\MethodNotAllowedException;
12+
use Tobyz\JsonApiServer\Exception\NotImplementedException;
13+
use Tobyz\JsonApiServer\Exception\ResourceNotFoundException;
14+
use Tobyz\JsonApiServer\Handler\Concerns\FindsResources;
1515

1616
class Api implements RequestHandlerInterface
1717
{
@@ -30,6 +30,11 @@ public function resource(string $type, $adapter, Closure $buildSchema = null): v
3030
$this->resources[$type] = new ResourceType($type, $adapter, $buildSchema);
3131
}
3232

33+
public function getResources(): array
34+
{
35+
return $this->resources;
36+
}
37+
3338
public function getResource(string $type): ResourceType
3439
{
3540
if (! isset($this->resources[$type])) {
@@ -116,7 +121,7 @@ private function handleWithHandler(Request $request, RequestHandlerInterface $ha
116121
return $handler->handle($request);
117122
}
118123

119-
public function handleError($e)
124+
public function error($e)
120125
{
121126
if (! $e instanceof ErrorProviderInterface) {
122127
$e = new Exception\InternalServerErrorException;

src/ErrorProviderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer;
3+
namespace Tobyz\JsonApiServer;
44

55
interface ErrorProviderInterface
66
{

src/Exception/BadRequestException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Exception;
3+
namespace Tobyz\JsonApiServer\Exception;
44

55
use JsonApiPhp\JsonApi\Error;
6-
use Tobscure\JsonApiServer\ErrorProviderInterface;
6+
use Tobyz\JsonApiServer\ErrorProviderInterface;
77

88
class BadRequestException extends \DomainException implements ErrorProviderInterface
99
{

src/Exception/ForbiddenException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Exception;
3+
namespace Tobyz\JsonApiServer\Exception;
44

55
use JsonApiPhp\JsonApi\Error;
6-
use Tobscure\JsonApiServer\ErrorProviderInterface;
6+
use Tobyz\JsonApiServer\ErrorProviderInterface;
77

88
class ForbiddenException extends \DomainException implements ErrorProviderInterface
99
{

src/Exception/InternalServerErrorException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Exception;
3+
namespace Tobyz\JsonApiServer\Exception;
44

55
use JsonApiPhp\JsonApi\Error;
6-
use Tobscure\JsonApiServer\ErrorProviderInterface;
6+
use Tobyz\JsonApiServer\ErrorProviderInterface;
77

88
class InternalServerErrorException extends \RuntimeException implements ErrorProviderInterface
99
{

src/Exception/MethodNotAllowedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tobscure\JsonApiServer\Exception;
3+
namespace Tobyz\JsonApiServer\Exception;
44

55
use JsonApiPhp\JsonApi\Error;
6-
use Tobscure\JsonApiServer\ErrorProviderInterface;
6+
use Tobyz\JsonApiServer\ErrorProviderInterface;
77

88
class MethodNotAllowedException extends \DomainException implements ErrorProviderInterface
99
{

0 commit comments

Comments
 (0)