Skip to content

Commit 0d488c0

Browse files
committed
Refactor name(space) of JenssegersItem to Item
1 parent 40e72ad commit 0d488c0

25 files changed

+162
-168
lines changed

README.MD

Lines changed: 12 additions & 14 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

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: 1 addition & 2 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;
@@ -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/JsonApi/Hydrator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +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;
13+
use Swis\JsonApi\Client\Item;
1414

1515
class Hydrator
1616
{
@@ -118,7 +118,7 @@ protected function getItemClass(string $type): ItemInterface
118118
return $this->typeMapper->getMapping($type);
119119
}
120120

121-
return (new JenssegersItem())->setType($type);
121+
return (new Item())->setType($type);
122122
}
123123

124124
/**

tests/CollectionDocumentBuilderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Swis\JsonApi\Client\Collection;
66
use Swis\JsonApi\Client\CollectionDocumentBuilder;
7-
use Swis\JsonApi\Client\Items\JenssegersItem;
7+
use Swis\JsonApi\Client\Item;
88

99
class CollectionDocumentBuilderTest extends AbstractTest
1010
{
@@ -16,8 +16,8 @@ public function it_fills_items_from_array()
1616
$collectionDocumentBuilder = new CollectionDocumentBuilder();
1717

1818
$data = [
19-
(new JenssegersItem(['key1' => 'value1']))->setId(1),
20-
(new JenssegersItem(['key2' => 'value2']))->setId(2),
19+
(new Item(['key1' => 'value1']))->setId(1),
20+
(new Item(['key2' => 'value2']))->setId(2),
2121
];
2222

2323
$collectionDocument = $collectionDocumentBuilder->build($data);
@@ -26,11 +26,11 @@ public function it_fills_items_from_array()
2626
$items = $collectionDocument->getData();
2727
static::assertInstanceOf(Collection::class, $items);
2828

29-
static::assertInstanceOf(JenssegersItem::class, $items[0]);
29+
static::assertInstanceOf(Item::class, $items[0]);
3030
static::assertEquals(1, $items[0]->getId());
3131
static::assertEquals($data[0]['key1'], $items[0]->key1);
3232

33-
static::assertInstanceOf(JenssegersItem::class, $items[1]);
33+
static::assertInstanceOf(Item::class, $items[1]);
3434
static::assertEquals(2, $items[1]->getId());
3535
static::assertEquals($data[1]['key2'], $items[1]->key2);
3636
}

tests/ItemDocumentBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Swis\JsonApi\Client\Tests;
44

5+
use Swis\JsonApi\Client\Item;
56
use Swis\JsonApi\Client\ItemDocumentBuilder;
67
use Swis\JsonApi\Client\ItemHydrator;
7-
use Swis\JsonApi\Client\Items\JenssegersItem;
88
use Swis\JsonApi\Client\TypeMapper;
99

1010
class ItemDocumentBuilderTest extends AbstractTest
@@ -20,10 +20,10 @@ public function it_fills_items_from_id_and_attributes()
2020
$itemHydrator = new ItemHydrator($typeMapper);
2121
$itemDocumentBuilder = new ItemDocumentBuilder($itemHydrator);
2222

23-
$itemDocument = $itemDocumentBuilder->build(new JenssegersItem(), $data, 123);
23+
$itemDocument = $itemDocumentBuilder->build(new Item(), $data, 123);
2424

2525
$item = $itemDocument->getData();
26-
static::assertInstanceOf(JenssegersItem::class, $item);
26+
static::assertInstanceOf(Item::class, $item);
2727
static::assertEquals($item->key1, $data['key1']);
2828
static::assertEquals($item->key2, $data['key2']);
2929
}

tests/ItemHydratorTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use InvalidArgumentException;
66
use Swis\JsonApi\Client\Collection;
7+
use Swis\JsonApi\Client\Item;
78
use Swis\JsonApi\Client\ItemHydrator;
8-
use Swis\JsonApi\Client\Items\JenssegersItem;
99
use Swis\JsonApi\Client\Relations\HasManyRelation;
1010
use Swis\JsonApi\Client\Relations\HasOneRelation;
1111
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
1212
use Swis\JsonApi\Client\Relations\MorphToRelation;
13-
use Swis\JsonApi\Client\Tests\Mocks\Items\Jenssegers\AnotherRelatedJenssegersItem;
14-
use Swis\JsonApi\Client\Tests\Mocks\Items\Jenssegers\RelatedJenssegersItem;
15-
use Swis\JsonApi\Client\Tests\Mocks\Items\Jenssegers\WithRelationshipJenssegersItem;
13+
use Swis\JsonApi\Client\Tests\Mocks\Items\AnotherRelatedItem;
14+
use Swis\JsonApi\Client\Tests\Mocks\Items\RelatedItem;
15+
use Swis\JsonApi\Client\Tests\Mocks\Items\WithRelationshipItem;
1616
use Swis\JsonApi\Client\TypeMapper;
1717

1818
class ItemHydratorTest extends AbstractTest
@@ -27,7 +27,7 @@ public function it_hydrates_items_without_relationships()
2727
'testattribute2' => 'test2',
2828
];
2929

30-
$item = new JenssegersItem();
30+
$item = new Item();
3131

3232
$item = $this->getItemHydrator()->hydrate($item, $data);
3333

@@ -40,10 +40,10 @@ public function it_hydrates_items_without_relationships()
4040
private function getItemHydrator()
4141
{
4242
$typeMapper = new TypeMapper();
43-
$typeMapper->setMapping('hydratedItem', JenssegersItem::class);
43+
$typeMapper->setMapping('hydratedItem', Item::class);
4444

45-
$typeMapper->setMapping('related-item', RelatedJenssegersItem::class);
46-
$typeMapper->setMapping('another-related-item', AnotherRelatedJenssegersItem::class);
45+
$typeMapper->setMapping('related-item', RelatedItem::class);
46+
$typeMapper->setMapping('another-related-item', AnotherRelatedItem::class);
4747

4848
return new ItemHydrator($typeMapper);
4949
}
@@ -59,7 +59,7 @@ public function it_hydrates_items_with_hasone_relationships()
5959
'hasone_relation' => 1,
6060
];
6161

62-
$item = new WithRelationshipJenssegersItem();
62+
$item = new WithRelationshipItem();
6363
$item = $this->getItemHydrator()->hydrate($item, $data);
6464

6565
/** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */
@@ -100,7 +100,7 @@ public function it_hydrates_items_with_hasmany_relationships()
100100
],
101101
];
102102

103-
$item = new WithRelationshipJenssegersItem();
103+
$item = new WithRelationshipItem();
104104

105105
$item = $this->getItemHydrator()->hydrate($item, $data);
106106
/** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */
@@ -151,7 +151,7 @@ public function it_throws_exception_when_morphto_relationship_without_type_attri
151151
],
152152
];
153153

154-
$item = new WithRelationshipJenssegersItem();
154+
$item = new WithRelationshipItem();
155155

156156
$this->expectException(InvalidArgumentException::class);
157157
$this->getItemHydrator()->hydrate($item, $data);
@@ -172,7 +172,7 @@ public function it_hydrates_items_with_morphto_relationship()
172172
],
173173
];
174174

175-
$item = new WithRelationshipJenssegersItem();
175+
$item = new WithRelationshipItem();
176176
$item = $this->getItemHydrator()->hydrate($item, $data);
177177

178178
/** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */
@@ -208,7 +208,7 @@ public function it_throws_exception_when_morphtomany_relationship_without_type_a
208208
],
209209
];
210210

211-
$item = new WithRelationshipJenssegersItem();
211+
$item = new WithRelationshipItem();
212212

213213
$this->expectException(InvalidArgumentException::class);
214214
$this->getItemHydrator()->hydrate($item, $data);
@@ -236,7 +236,7 @@ public function it_hydrates_items_with_morphtomany_relationship()
236236
],
237237
];
238238

239-
$item = new WithRelationshipJenssegersItem();
239+
$item = new WithRelationshipItem();
240240
$item = $this->getItemHydrator()->hydrate($item, $data);
241241

242242
/** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */
@@ -279,7 +279,7 @@ public function it_hydrates_nested_relationship_items()
279279
],
280280
];
281281

282-
$item = new WithRelationshipJenssegersItem();
282+
$item = new WithRelationshipItem();
283283
$item = $this->getItemHydrator()->hydrate($item, $data);
284284

285285
/** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */
@@ -314,7 +314,7 @@ public function it_hydrates_a_hasmany_relationship_by_id()
314314
],
315315
];
316316

317-
$item = new WithRelationshipJenssegersItem();
317+
$item = new WithRelationshipItem();
318318

319319
$item = $this->getItemHydrator()->hydrate($item, $data);
320320
/** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */

tests/Items/JenssegersItemRelationsTest.php renamed to tests/ItemRelationsTest.php

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

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

5-
use Swis\JsonApi\Client\Items\JenssegersItem;
5+
use Swis\JsonApi\Client\Item;
66
use Swis\JsonApi\Client\Relations\HasOneRelation;
7-
use Swis\JsonApi\Client\Tests\AbstractTest;
8-
use Swis\JsonApi\Client\Tests\Mocks\Items\Jenssegers\ChildJenssegersItem;
9-
use Swis\JsonApi\Client\Tests\Mocks\Items\Jenssegers\MasterJenssegersItem;
7+
use Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem;
8+
use Swis\JsonApi\Client\Tests\Mocks\Items\MasterItem;
109

11-
class JenssegersItemRelationsTest extends AbstractTest
10+
class ItemRelationsTest extends AbstractTest
1211
{
1312
/**
1413
* @test
1514
*/
1615
public function it_has_relationships_when_added()
1716
{
18-
$masterItem = new MasterJenssegersItem();
17+
$masterItem = new MasterItem();
1918
$this->assertInstanceOf(HasOneRelation::class, $masterItem->child());
2019

21-
$childItem = new ChildJenssegersItem();
20+
$childItem = new ChildItem();
2221
$childItem->setId(1);
2322
$this->assertEquals(1, $childItem->getId());
2423
$masterItem->child()->associate($childItem);
@@ -32,13 +31,13 @@ public function it_has_relationships_when_added()
3231
*/
3332
public function it_can_check_for_relations()
3433
{
35-
$item = new JenssegersItem();
34+
$item = new Item();
3635
$this->assertFalse($item->hasRelationship('test'));
3736

38-
$masterItem = new MasterJenssegersItem();
37+
$masterItem = new MasterItem();
3938
$this->assertFalse($masterItem->hasRelationship('child'));
4039

41-
$childItem = new ChildJenssegersItem();
40+
$childItem = new ChildItem();
4241
$childItem->setId(1);
4342
$masterItem->child()->associate($childItem);
4443
$this->assertTrue($masterItem->hasRelationship('child'));
@@ -49,8 +48,8 @@ public function it_can_check_for_relations()
4948
*/
5049
public function it_can_get_all_relations()
5150
{
52-
$masterItem = new MasterJenssegersItem();
53-
$childItem = new ChildJenssegersItem();
51+
$masterItem = new MasterItem();
52+
$childItem = new ChildItem();
5453
$childItem->setId(1);
5554
$masterItem->child()->setId(1);
5655
$masterItem->child()->associate($childItem);

0 commit comments

Comments
 (0)