Skip to content

Commit 5d76c0f

Browse files
committed
wip
1 parent 8a4a09b commit 5d76c0f

35 files changed

+1141
-508
lines changed

src/Adapter/AdapterInterface.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Adapter;
413

514
use Closure;
@@ -151,20 +160,20 @@ public function getAttribute($model, Attribute $attribute);
151160
*
152161
* @param $model
153162
* @param HasOne $relationship
154-
* @param array $fields
163+
* @param bool $linkage
155164
* @return mixed|null
156165
*/
157-
public function getHasOne($model, HasOne $relationship, array $fields = null);
166+
public function getHasOne($model, HasOne $relationship, bool $linkage);
158167

159168
/**
160169
* Get a list of models for a has-many relationship for the model.
161170
*
162171
* @param $model
163172
* @param HasMany $relationship
164-
* @param array $fields
173+
* @param bool $linkage
165174
* @return array
166175
*/
167-
public function getHasMany($model, HasMany $relationship, array $fields = null): array;
176+
public function getHasMany($model, HasMany $relationship, bool $linkage): array;
168177

169178
/**
170179
* Apply an attribute value to the model.
@@ -219,9 +228,11 @@ public function delete($model): void;
219228
* @param array $relationships
220229
* @param Closure $scope Should be called to give the deepest relationship
221230
* an opportunity to scope the query that will fetch related resources
231+
* @param bool $linkage true if we just need the IDs of the related
232+
* resources and not their full data
222233
* @return mixed
223234
*/
224-
public function load(array $models, array $relationships, Closure $scope): void;
235+
public function load(array $models, array $relationships, Closure $scope, bool $linkage): void;
225236

226237
/**
227238
* Load information about the IDs of related resources onto a collection
@@ -231,5 +242,5 @@ public function load(array $models, array $relationships, Closure $scope): void;
231242
* @param Relationship $relationship
232243
* @return mixed
233244
*/
234-
public function loadIds(array $models, Relationship $relationship): void;
245+
// public function loadIds(array $models, Relationship $relationship): void;
235246
}

src/Adapter/EloquentAdapter.php

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Adapter;
413

514
use Closure;
@@ -69,12 +78,15 @@ public function getAttribute($model, Attribute $attribute)
6978
return $model->{$this->getAttributeProperty($attribute)};
7079
}
7180

72-
public function getHasOne($model, HasOne $relationship, array $fields = null)
81+
public function getHasOne($model, HasOne $relationship, bool $linkage)
7382
{
7483
$relation = $this->getEloquentRelation($model, $relationship);
7584

76-
// comment
77-
if ($fields === ['id'] && $relation instanceof BelongsTo) {
85+
// If it's a belongs-to relationship and we only need to get the ID,
86+
// then we don't have to actually load the relation because the ID is
87+
// stored in a column directly on the model. We will mock up a related
88+
// model with the value of the ID filled.
89+
if ($linkage && $relation instanceof BelongsTo) {
7890
if ($key = $model->{$relation->getForeignKeyName()}) {
7991
$related = $relation->getRelated();
8092

@@ -87,7 +99,7 @@ public function getHasOne($model, HasOne $relationship, array $fields = null)
8799
return $this->getRelationValue($model, $relationship);
88100
}
89101

90-
public function getHasMany($model, HasMany $relationship, array $fields = null): array
102+
public function getHasMany($model, HasMany $relationship, bool $linkage): array
91103
{
92104
$collection = $this->getRelationValue($model, $relationship);
93105

@@ -188,38 +200,42 @@ public function paginate($query, int $limit, int $offset): void
188200
$query->take($limit)->skip($offset);
189201
}
190202

191-
public function load(array $models, array $relationships, Closure $scope): void
203+
public function load(array $models, array $relationships, Closure $scope, bool $linkage): void
192204
{
205+
// TODO: Find the relation on the model that we're after. If it's a
206+
// belongs-to relation, and we only need linkage, then we won't need
207+
// to load anything as the related ID is store directly on the model.
208+
193209
(new Collection($models))->loadMissing([
194210
$this->getRelationshipPath($relationships) => $scope
195211
]);
196212
}
197213

198-
public function loadIds(array $models, Relationship $relationship): void
199-
{
200-
if (empty($models)) {
201-
return;
202-
}
203-
204-
$property = $this->getRelationshipProperty($relationship);
205-
$relation = $models[0]->$property();
206-
207-
// If it's a belongs-to relationship, then the ID is stored on the model
208-
// itself, so we don't need to load anything in advance.
209-
if ($relation instanceof BelongsTo) {
210-
return;
211-
}
212-
213-
(new Collection($models))->loadMissing([
214-
$property => function ($query) use ($relation) {
215-
$query->select($relation->getRelated()->getKeyName());
216-
217-
if (! $relation instanceof BelongsToMany) {
218-
$query->addSelect($relation->getForeignKeyName());
219-
}
220-
}
221-
]);
222-
}
214+
// public function loadIds(array $models, Relationship $relationship): void
215+
// {
216+
// if (empty($models)) {
217+
// return;
218+
// }
219+
//
220+
// $property = $this->getRelationshipProperty($relationship);
221+
// $relation = $models[0]->$property();
222+
//
223+
// // If it's a belongs-to relationship, then the ID is stored on the model
224+
// // itself, so we don't need to load anything in advance.
225+
// if ($relation instanceof BelongsTo) {
226+
// return;
227+
// }
228+
//
229+
// (new Collection($models))->loadMissing([
230+
// $property => function ($query) use ($relation) {
231+
// $query->select($relation->getRelated()->getKeyName());
232+
//
233+
// if (! $relation instanceof BelongsToMany) {
234+
// $query->addSelect($relation->getForeignKeyName());
235+
// }
236+
// }
237+
// ]);
238+
// }
223239

224240
private function getAttributeProperty(Attribute $attribute): string
225241
{

src/ErrorProviderInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
<?php
22

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer;
413

14+
use JsonApiPhp\JsonApi\Error;
15+
516
interface ErrorProviderInterface
617
{
18+
/**
19+
* Get JSON:API error objects that represent this error.
20+
*
21+
* @return Error[]
22+
*/
723
public function getJsonApiErrors(): array;
824

25+
/**
26+
* Get the most generally applicable HTTP error code for this error.
27+
*/
928
public function getJsonApiStatus(): string;
1029
}

src/Exception/BadRequestException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use DomainException;

src/Exception/ForbiddenException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use DomainException;

src/Exception/InternalServerErrorException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use JsonApiPhp\JsonApi\Error;

src/Exception/MethodNotAllowedException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use DomainException as DomainExceptionAlias;

src/Exception/NotAcceptableException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use JsonApiPhp\JsonApi\Error;

src/Exception/NotImplementedException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use DomainException;

src/Exception/ResourceNotFoundException.php

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

3+
/*
4+
* This file is part of tobyz/json-api-server.
5+
*
6+
* (c) Toby Zerner <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Tobyz\JsonApiServer\Exception;
413

514
use JsonApiPhp\JsonApi\Error;

0 commit comments

Comments
 (0)