Skip to content

Commit

Permalink
[Bugfix] Allow store to be a relation method on adapters
Browse files Browse the repository at this point in the history
Renames the `store` method to `getStore` so that it can be used
as a relation method name in an adapter.

Closes #185
  • Loading branch information
lindyhopchris committed Jun 22, 2018
1 parent 8800dae commit ab51bfa
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file. This projec
## Unreleased

### Fixed
- [#185](https://github.com/cloudcreativity/laravel-json-api/issues/185)
Rename adapter `store` method to `getStore` to avoid collisions with relation methods.
- [#187](https://github.com/cloudcreativity/laravel-json-api/issues/187)
Ensure hydration of Eloquent morph-many relationship works.
- [#194](https://github.com/cloudcreativity/laravel-json-api/issues/194)
Expand Down
19 changes: 19 additions & 0 deletions docs/basics/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,25 @@ class Adapter extends AbstractAdapter
> The `morphMany` implementation currently has some limitations that we are hoping to resolve during our alpha
and beta releases. If you have problems using it, please create an issue as this will help us out.

#### Customising Relation Method Names

If you want to use a method name for your relation that is different than the JSON API field name, overload
the `methodForRelation` method on your adapter. For example, you would need to do this if the field name collides
with a method that already exists on the abstract adapter.

The method can be overloaded as follows:

```php
protected function methodForRelation($field)
{
if ('myField' === $field) {
return 'myOtherMethodName';
}

return parent::methodForRelation($field);
}
```

## Custom Adapters

Custom adapters can be used for any domain record that is not an Eloquent model. Adapters will work with this
Expand Down
5 changes: 5 additions & 0 deletions docs/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function read(StoreInterface $store, ValidatedRequest $request)
}
```

### Adapters

The `store` method has been renamed to `getStore` to avoid collisions with JSON API relation names. This change
will only affect applications that are overloading internal adapter methods.

### Has-Many

The method signatures for the `sync` and `detach` methods in the JSON API Eloquent `HasMany` relation have been
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/AbstractResourceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function related($field)
$relation->withFieldName($field);

if ($relation instanceof StoreAwareInterface) {
$relation->withStore($this->store());
$relation->withStore($this->getStore());
}

return $relation;
Expand Down
24 changes: 24 additions & 0 deletions src/Eloquent/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,20 @@ protected function findByIds(Builder $query, Collection $filters)
*
* @param Builder $query
* @return Model
* @deprecated 1.0.0 use `searchOne`, renamed to avoid collisions with relation names.
*/
protected function first(Builder $query)
{
return $this->searchOne($query);
}

/**
* Return the result for a search one query.
*
* @param Builder $query
* @return Model
*/
protected function searchOne($query)
{
return $query->first();
}
Expand All @@ -457,8 +469,20 @@ protected function first(Builder $query)
*
* @param Builder $query
* @return mixed
* @deprecated 1.0.0 use `searchAll`, renamed to avoid collisions with relation names.
*/
protected function all($query)
{
return $this->searchAll($query);
}

/**
* Return the result for query that is not paginated.
*
* @param Builder $query
* @return mixed
*/
protected function searchAll($query)
{
return $query->get();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/AbstractManyRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function query($record, EncodingParametersInterface $parameters)
}

$relation = $this->getRelation($record);
$adapter = $this->store()->adapterFor($relation->getModel());
$adapter = $this->getStore()->adapterFor($relation->getModel());

if (!$adapter instanceof AbstractAdapter) {
throw new RuntimeException('Expecting inverse adapter to be an Eloquent adapter.');
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function findRelated(RelationshipInterface $relationship)
{
$identifier = $relationship->hasIdentifier() ? $relationship->getIdentifier() : null;

return $identifier ? $this->store()->find($identifier) : null;
return $identifier ? $this->getStore()->find($identifier) : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function detach($relation, Collection $remove)
protected function findRelated($record, RelationshipInterface $relationship)
{
$inverse = $this->getRelation($record)->getRelated();
$related = $this->store()->findMany($relationship->getIdentifiers());
$related = $this->getStore()->findMany($relationship->getIdentifiers());

$related = collect($related)->filter(function ($model) use ($inverse) {
return $model instanceof $inverse;
Expand Down
2 changes: 1 addition & 1 deletion src/Store/StoreAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function withStore(StoreInterface $store)
/**
* @return StoreInterface
*/
protected function store()
protected function getStore()
{
if (!$this->store) {
throw new RuntimeException('No store injected.');
Expand Down

0 comments on commit ab51bfa

Please sign in to comment.