Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

* Refactored name(space) of `\Swis\JsonApi\Client\Items\JenssegersItem` to `\Swis\JsonApi\Client\Item` as we only have one item now.

### Removed

* `EloquentItem` is removed because it had some limitations which could not be fixed without being too opinionated.
* `NullItem` is removed in favor of simply `null`. This item was only used internally so this should not affect you.

## [0.7.5] - 2018-07-04

### Fixed
Expand Down
31 changes: 12 additions & 19 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ The `DocumentClient` follows the following steps internally:
1. Send the request using your HTTP client;
2. Use [art4/json-api-client](https://github.com/art4/json-api-client) to parse and validate the response;
3. Create the correct document instance;
4. Hydrate every item by using the item model registered with the `TypeMapper` or a `JenssegersItem` as fallback;
4. Hydrate every item by using the item model registered with the `TypeMapper` or a `\Swis\JsonApi\Client\Item` as fallback;
5. Hydrate all relationships;
6. Add meta data to the document such as [errors](http://jsonapi.org/format/#errors), [links](http://jsonapi.org/format/#document-links) and [meta](http://jsonapi.org/format/#document-meta).

Expand All @@ -139,22 +139,20 @@ It can take everything your request factory takes as input data and returns the
It does not parse or validate the response or hydrate items!


## Item models
## Items

By default, all items are an instance of `JenssegersItem`.
You can define your own models by extending the `JenssegersItem` or by implementing the `\Swis\JsonApi\Client\Interfaces\ItemInterface` yourself.
By default, all items are an instance of `\Swis\JsonApi\Client\Item`.
The `Item` extends [jenssegers/model](https://github.com/jenssegers/model), which provides a Laravel Eloquent-like base class.
Please see it's documentation about the features it provides.
You can define your own models by extending `\Swis\JsonApi\Client\Item` or by implementing the `\Swis\JsonApi\Client\Interfaces\ItemInterface` yourself.
This can be useful if you want to define, for example, hidden attributes, casts or get/set mutators.
If you use custom models, you must register them with the [TypeMapper](#typemapper).

### JenssegersItem

The `JenssegersItem` extends [jenssegers/model](https://github.com/jenssegers/model), which provides a Laravel Eloquent-like base class.
Please see it's documentation about the features it provides.
On top of that, this package has implemented Laravel Eloquent-like relations.

#### Relations
### Relations

The relations are basic variants of [Laravel Eloquent relationships](https://laravel.com/docs/eloquent-relationships) and provide a fluent interface to retrieve the related items.
On top of [jenssegers/model](https://github.com/jenssegers/model), this package has implemented [Laravel Eloquent-like relations](https://laravel.com/docs/eloquent-relationships).
These relations provide a fluent interface to retrieve the related items.
There are currently four relations available:

* `HasOneRelation`
Expand All @@ -165,9 +163,9 @@ There are currently four relations available:
Please see the following example about defining the relationships:

``` php
use Swis\JsonApi\Client\Items\JenssegersItem;
use Swis\JsonApi\Client\Item;

class AuthorItem extends JenssegersItem
class AuthorItem extends Item
{
protected $type = 'author';

Expand All @@ -177,7 +175,7 @@ class AuthorItem extends JenssegersItem
}
}

class BlogItem extends JenssegersItem
class BlogItem extends Item
{
protected $type = 'blog';

Expand All @@ -188,11 +186,6 @@ class BlogItem extends JenssegersItem
}
```

### EloquentItem (EXPERIMENTAL)

The `EloquentItem` directly extends the [Laravel Eloquent Model](https://laravel.com/docs/eloquent) and uses the provided relations.
Please note that this implementation is experimental and should not be used in production!


## Collections

Expand Down
5 changes: 2 additions & 3 deletions src/Items/JenssegersItem.php → src/Item.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php

namespace Swis\JsonApi\Client\Items;
namespace Swis\JsonApi\Client;

use Jenssegers\Model\Model;
use Swis\JsonApi\Client\Collection;
use Swis\JsonApi\Client\Interfaces\ItemInterface;
use Swis\JsonApi\Client\Interfaces\RelationInterface;
use Swis\JsonApi\Client\Relations\HasManyRelation;
use Swis\JsonApi\Client\Relations\HasOneRelation;
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
use Swis\JsonApi\Client\Relations\MorphToRelation;

class JenssegersItem extends Model implements ItemInterface
class Item extends Model implements ItemInterface
{
/**
* @var string
Expand Down
7 changes: 3 additions & 4 deletions src/ItemHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Swis\JsonApi\Client\Interfaces\ItemInterface;
use Swis\JsonApi\Client\Interfaces\RelationInterface;
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
use Swis\JsonApi\Client\Items\JenssegersItem;
use Swis\JsonApi\Client\Relations\HasManyRelation;
use Swis\JsonApi\Client\Relations\HasOneRelation;
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
Expand Down Expand Up @@ -187,9 +186,9 @@ protected function hydrateMorphToManyRelation(array $attributes, MorphToManyRela
*
* @throws \Exception
*
* @return \Swis\JsonApi\Client\Items\JenssegersItem
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
*/
protected function buildRelationItem(RelationInterface $relation, array $relationData, string $type = null): JenssegersItem
protected function buildRelationItem(RelationInterface $relation, array $relationData, string $type = null): ItemInterface
{
// Sometimes the relatedType is provided from the relationship, but not always (i.e. Polymorphic Relationships)
if (null === $type) {
Expand All @@ -199,7 +198,7 @@ protected function buildRelationItem(RelationInterface $relation, array $relatio
if ($this->typeMapper->hasMapping($type)) {
$relationItem = $this->typeMapper->getMapping($type);
} else {
$relationItem = new JenssegersItem();
$relationItem = new Item();
$relationItem->setType($type);
}

Expand Down
152 changes: 0 additions & 152 deletions src/Items/EloquentItem.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Items/NullItem.php

This file was deleted.

22 changes: 8 additions & 14 deletions src/JsonApi/Hydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Swis\JsonApi\Client\Collection;
use Swis\JsonApi\Client\Interfaces\ItemInterface;
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
use Swis\JsonApi\Client\Items\JenssegersItem;
use Swis\JsonApi\Client\Items\NullItem;
use Swis\JsonApi\Client\Item;

class Hydrator
{
Expand Down Expand Up @@ -81,7 +80,7 @@ function (ResourceItemInterface $jsonApiItem) use ($keyedItems) {

$item = $this->getItem($keyedItems, $jsonApiItem);

if ($item instanceof NullItem) {
if ($item === null) {
return;
}

Expand All @@ -93,7 +92,7 @@ function (ResourceItemInterface $jsonApiItem) use ($keyedItems) {
if ($data instanceof ResourceIdentifierInterface) {
$includedItem = $this->getItem($keyedItems, $data);

if ($includedItem instanceof NullItem) {
if ($includedItem === null) {
continue;
}

Expand All @@ -119,23 +118,18 @@ protected function getItemClass(string $type): ItemInterface
return $this->typeMapper->getMapping($type);
}

return (new JenssegersItem())->setType($type);
return (new Item())->setType($type);
}

/**
* @param \Swis\JsonApi\Client\Collection $included
* @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceItemInterface $identifier
*
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
*/
protected function getItem(Collection $included, $identifier): ItemInterface
protected function getItem(Collection $included, $identifier)
{
return $included->get(
$this->getElementKey($identifier),
function () {
return new NullItem();
}
);
return $included->get($this->getElementKey($identifier));
}

/**
Expand All @@ -151,7 +145,7 @@ protected function getCollection(Collection $included, $identifierCollection): C
foreach ($identifierCollection->asArray() as $identifier) {
$item = $this->getItem($included, $identifier);

if ($item instanceof NullItem) {
if ($item === null) {
continue;
}

Expand Down
Loading