Skip to content

Commit bbe7aff

Browse files
authored
Merge pull request swisnl#21 from swisnl/cleanup-items
Remove EloquentItem and NullItem and rename JenssegersItem
2 parents cec9097 + d9f5fed commit bbe7aff

35 files changed

+202
-630
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Changed
10+
11+
* Refactored name(space) of `\Swis\JsonApi\Client\Items\JenssegersItem` to `\Swis\JsonApi\Client\Item` as we only have one item now.
12+
13+
### Removed
14+
15+
* `EloquentItem` is removed because it had some limitations which could not be fixed without being too opinionated.
16+
* `NullItem` is removed in favor of simply `null`. This item was only used internally so this should not affect you.
17+
718
## [0.7.5] - 2018-07-04
819

920
### Fixed

README.MD

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ The `DocumentClient` follows the following steps internally:
128128
1. Send the request using your HTTP client;
129129
2. Use [art4/json-api-client](https://github.com/art4/json-api-client) to parse and validate the response;
130130
3. Create the correct document instance;
131-
4. Hydrate every item by using the item model registered with the `TypeMapper` or a `JenssegersItem` as fallback;
131+
4. Hydrate every item by using the item model registered with the `TypeMapper` or a `\Swis\JsonApi\Client\Item` as fallback;
132132
5. Hydrate all relationships;
133133
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).
134134

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

141141

142-
## Item models
142+
## Items
143143

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

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

155-
#### Relations
152+
### Relations
156153

157-
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.
154+
On top of [jenssegers/model](https://github.com/jenssegers/model), this package has implemented [Laravel Eloquent-like relations](https://laravel.com/docs/eloquent-relationships).
155+
These relations provide a fluent interface to retrieve the related items.
158156
There are currently four relations available:
159157

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

167165
``` php
168-
use Swis\JsonApi\Client\Items\JenssegersItem;
166+
use Swis\JsonApi\Client\Item;
169167

170-
class AuthorItem extends JenssegersItem
168+
class AuthorItem extends Item
171169
{
172170
protected $type = 'author';
173171

@@ -177,7 +175,7 @@ class AuthorItem extends JenssegersItem
177175
}
178176
}
179177

180-
class BlogItem extends JenssegersItem
178+
class BlogItem extends Item
181179
{
182180
protected $type = 'blog';
183181

@@ -188,11 +186,6 @@ class BlogItem extends JenssegersItem
188186
}
189187
```
190188

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

197190
## Collections
198191

src/Items/JenssegersItem.php renamed to src/Item.php

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

3-
namespace Swis\JsonApi\Client\Items;
3+
namespace Swis\JsonApi\Client;
44

55
use Jenssegers\Model\Model;
6-
use Swis\JsonApi\Client\Collection;
76
use Swis\JsonApi\Client\Interfaces\ItemInterface;
87
use Swis\JsonApi\Client\Interfaces\RelationInterface;
98
use Swis\JsonApi\Client\Relations\HasManyRelation;
109
use Swis\JsonApi\Client\Relations\HasOneRelation;
1110
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
1211
use Swis\JsonApi\Client\Relations\MorphToRelation;
1312

14-
class JenssegersItem extends Model implements ItemInterface
13+
class Item extends Model implements ItemInterface
1514
{
1615
/**
1716
* @var string

src/ItemHydrator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Swis\JsonApi\Client\Interfaces\ItemInterface;
66
use Swis\JsonApi\Client\Interfaces\RelationInterface;
77
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
8-
use Swis\JsonApi\Client\Items\JenssegersItem;
98
use Swis\JsonApi\Client\Relations\HasManyRelation;
109
use Swis\JsonApi\Client\Relations\HasOneRelation;
1110
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
@@ -187,9 +186,9 @@ protected function hydrateMorphToManyRelation(array $attributes, MorphToManyRela
187186
*
188187
* @throws \Exception
189188
*
190-
* @return \Swis\JsonApi\Client\Items\JenssegersItem
189+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
191190
*/
192-
protected function buildRelationItem(RelationInterface $relation, array $relationData, string $type = null): JenssegersItem
191+
protected function buildRelationItem(RelationInterface $relation, array $relationData, string $type = null): ItemInterface
193192
{
194193
// Sometimes the relatedType is provided from the relationship, but not always (i.e. Polymorphic Relationships)
195194
if (null === $type) {
@@ -199,7 +198,7 @@ protected function buildRelationItem(RelationInterface $relation, array $relatio
199198
if ($this->typeMapper->hasMapping($type)) {
200199
$relationItem = $this->typeMapper->getMapping($type);
201200
} else {
202-
$relationItem = new JenssegersItem();
201+
$relationItem = new Item();
203202
$relationItem->setType($type);
204203
}
205204

src/Items/EloquentItem.php

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/Items/NullItem.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/JsonApi/Hydrator.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
use Swis\JsonApi\Client\Collection;
1111
use Swis\JsonApi\Client\Interfaces\ItemInterface;
1212
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
13-
use Swis\JsonApi\Client\Items\JenssegersItem;
14-
use Swis\JsonApi\Client\Items\NullItem;
13+
use Swis\JsonApi\Client\Item;
1514

1615
class Hydrator
1716
{
@@ -81,7 +80,7 @@ function (ResourceItemInterface $jsonApiItem) use ($keyedItems) {
8180

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

84-
if ($item instanceof NullItem) {
83+
if ($item === null) {
8584
return;
8685
}
8786

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

96-
if ($includedItem instanceof NullItem) {
95+
if ($includedItem === null) {
9796
continue;
9897
}
9998

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

122-
return (new JenssegersItem())->setType($type);
121+
return (new Item())->setType($type);
123122
}
124123

125124
/**
126125
* @param \Swis\JsonApi\Client\Collection $included
127126
* @param \Art4\JsonApiClient\ResourceIdentifierInterface|\Art4\JsonApiClient\ResourceItemInterface $identifier
128127
*
129-
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
128+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
130129
*/
131-
protected function getItem(Collection $included, $identifier): ItemInterface
130+
protected function getItem(Collection $included, $identifier)
132131
{
133-
return $included->get(
134-
$this->getElementKey($identifier),
135-
function () {
136-
return new NullItem();
137-
}
138-
);
132+
return $included->get($this->getElementKey($identifier));
139133
}
140134

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

154-
if ($item instanceof NullItem) {
148+
if ($item === null) {
155149
continue;
156150
}
157151

0 commit comments

Comments
 (0)