Skip to content

Commit 3003245

Browse files
authored
Merge pull request #53 from swisnl/feature/improve-item
Improve code quality and test coverage of Item
2 parents 535b5e4 + 6c3af01 commit 3003245

28 files changed

+1361
-641
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1717
* The `ItemHydrator` now also hydrates the id if provided.
1818
* Added `hasType`, `hasAttributes`, `hasRelationships` and `getRelations` to `ItemInterface`.
1919
* Removed `canBeIncluded` and `getIncluded` from `ItemInterface` as the `DocumentFactory` is now responsible for gathering the included items.
20+
* Renamed `getRelationship` to `getRelation`, `hasRelationship` to `hasRelation` and `removeRelationship` to `unsetRelation` in `Item`.
21+
* Renamed/aligned some parameters in several relation methods in `Item`.
22+
* Renamed namespace `Swis\JsonApi\Client\Traits` to `Swis\JsonApi\Client\Concerns`.
2023

2124
### Removed
2225

src/Concerns/HasId.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Concerns;
4+
5+
trait HasId
6+
{
7+
/**
8+
* @var string|null
9+
*/
10+
protected $id;
11+
12+
/**
13+
* @return string|null
14+
*/
15+
public function getId(): ?string
16+
{
17+
return $this->id;
18+
}
19+
20+
/**
21+
* @param string|null $id
22+
*
23+
* @return static
24+
*/
25+
public function setId(?string $id)
26+
{
27+
$this->id = $id;
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* @return bool
34+
*/
35+
public function hasId(): bool
36+
{
37+
return isset($this->id);
38+
}
39+
}

src/Concerns/HasInitial.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Concerns;
4+
5+
trait HasInitial
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $initial = [];
11+
12+
/**
13+
* @param string|null $key
14+
*
15+
* @return array|mixed
16+
*/
17+
public function getInitial($key = null)
18+
{
19+
if (null === $key) {
20+
return $this->initial;
21+
}
22+
23+
return $this->initial[$key];
24+
}
25+
26+
/**
27+
* @param array $initial
28+
*
29+
* @return static
30+
*/
31+
public function setInitial(array $initial)
32+
{
33+
$this->initial = $initial;
34+
35+
return $this;
36+
}
37+
38+
/**
39+
* @param string|null $key
40+
*
41+
* @return bool
42+
*/
43+
public function hasInitial($key = null): bool
44+
{
45+
if (null === $key) {
46+
return !empty($this->initial);
47+
}
48+
49+
return isset($this->initial[$key]);
50+
}
51+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Swis\JsonApi\Client\Traits;
3+
namespace Swis\JsonApi\Client\Concerns;
44

55
use Swis\JsonApi\Client\Links;
66

@@ -11,6 +11,14 @@ trait HasLinks
1111
*/
1212
protected $links;
1313

14+
/**
15+
* @return \Swis\JsonApi\Client\Links|null
16+
*/
17+
public function getLinks(): ? Links
18+
{
19+
return $this->links;
20+
}
21+
1422
/**
1523
* @param \Swis\JsonApi\Client\Links|null $links
1624
*
@@ -22,12 +30,4 @@ public function setLinks(? Links $links)
2230

2331
return $this;
2432
}
25-
26-
/**
27-
* @return \Swis\JsonApi\Client\Links|null
28-
*/
29-
public function getLinks(): ? Links
30-
{
31-
return $this->links;
32-
}
3333
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Swis\JsonApi\Client\Traits;
3+
namespace Swis\JsonApi\Client\Concerns;
44

55
use Swis\JsonApi\Client\Meta;
66

@@ -11,6 +11,14 @@ trait HasMeta
1111
*/
1212
protected $meta;
1313

14+
/**
15+
* @return \Swis\JsonApi\Client\Meta|null
16+
*/
17+
public function getMeta(): ? Meta
18+
{
19+
return $this->meta;
20+
}
21+
1422
/**
1523
* @param \Swis\JsonApi\Client\Meta|null $meta
1624
*
@@ -22,12 +30,4 @@ public function setMeta(? Meta $meta)
2230

2331
return $this;
2432
}
25-
26-
/**
27-
* @return \Swis\JsonApi\Client\Meta|null
28-
*/
29-
public function getMeta(): ? Meta
30-
{
31-
return $this->meta;
32-
}
3333
}

src/Concerns/HasRelations.php

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Concerns;
4+
5+
use Illuminate\Support\Str;
6+
use Swis\JsonApi\Client\Collection;
7+
use Swis\JsonApi\Client\Interfaces\DataInterface;
8+
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
9+
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
10+
use Swis\JsonApi\Client\Links;
11+
use Swis\JsonApi\Client\Meta;
12+
use Swis\JsonApi\Client\Relations\HasManyRelation;
13+
use Swis\JsonApi\Client\Relations\HasOneRelation;
14+
use Swis\JsonApi\Client\Relations\MorphToManyRelation;
15+
use Swis\JsonApi\Client\Relations\MorphToRelation;
16+
17+
trait HasRelations
18+
{
19+
/**
20+
* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface[]
21+
*/
22+
protected $relations = [];
23+
24+
/**
25+
* Create a singular relation to another item.
26+
*
27+
* @param string $itemClass
28+
* @param string|null $name
29+
*
30+
* @return \Swis\JsonApi\Client\Relations\HasOneRelation
31+
*/
32+
public function hasOne(string $itemClass, string $name = null): OneRelationInterface
33+
{
34+
$name = $name ?: Str::snake(debug_backtrace()[1]['function']);
35+
36+
if (!array_key_exists($name, $this->relations)) {
37+
$this->relations[$name] = new HasOneRelation((new $itemClass())->getType());
38+
}
39+
40+
return $this->relations[$name];
41+
}
42+
43+
/**
44+
* Create a plural relation to another item.
45+
*
46+
* @param string $itemClass
47+
* @param string|null $name
48+
*
49+
* @return \Swis\JsonApi\Client\Relations\HasManyRelation
50+
*/
51+
public function hasMany(string $itemClass, string $name = null): ManyRelationInterface
52+
{
53+
$name = $name ?: Str::snake(debug_backtrace()[1]['function']);
54+
55+
if (!array_key_exists($name, $this->relations)) {
56+
$this->relations[$name] = new HasManyRelation((new $itemClass())->getType());
57+
}
58+
59+
return $this->relations[$name];
60+
}
61+
62+
/**
63+
* Create a singular relation.
64+
*
65+
* @param string|null $name
66+
*
67+
* @return \Swis\JsonApi\Client\Relations\MorphToRelation
68+
*/
69+
public function morphTo(string $name = null): OneRelationInterface
70+
{
71+
$name = $name ?: Str::snake(debug_backtrace()[1]['function']);
72+
73+
if (!array_key_exists($name, $this->relations)) {
74+
$this->relations[$name] = new MorphToRelation();
75+
}
76+
77+
return $this->relations[$name];
78+
}
79+
80+
/**
81+
* Create a plural relation.
82+
*
83+
* @param string|null $name
84+
*
85+
* @return \Swis\JsonApi\Client\Relations\MorphToManyRelation
86+
*/
87+
public function morphToMany(string $name = null): ManyRelationInterface
88+
{
89+
$name = $name ?: Str::snake(debug_backtrace()[1]['function']);
90+
91+
if (!array_key_exists($name, $this->relations)) {
92+
$this->relations[$name] = new MorphToManyRelation();
93+
}
94+
95+
return $this->relations[$name];
96+
}
97+
98+
/**
99+
* @return array
100+
*/
101+
public function getRelations(): array
102+
{
103+
return $this->relations;
104+
}
105+
106+
/**
107+
* @param string $name
108+
*
109+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface|null
110+
*/
111+
public function getRelation(string $name)
112+
{
113+
return $this->relations[$name] ?? null;
114+
}
115+
116+
/**
117+
* Get the relationship data (included).
118+
*
119+
* @param string $name
120+
*
121+
* @return \Swis\JsonApi\Client\Interfaces\DataInterface|null
122+
*/
123+
public function getRelationValue(string $name): ?DataInterface
124+
{
125+
// If the "attribute" exists as a method on the model, we will just assume
126+
// it is a relationship and will load and return the included items in the relationship
127+
$method = Str::camel($name);
128+
if (method_exists($this, $method)) {
129+
return $this->$method()->getIncluded();
130+
}
131+
132+
// If the "attribute" exists as a relationship on the model, we will return
133+
// the included items in the relationship
134+
if ($this->hasRelation($name)) {
135+
return $this->getRelation($name)->getIncluded();
136+
}
137+
138+
return null;
139+
}
140+
141+
/**
142+
* Set the specific relationship on the model.
143+
*
144+
* @param string $name
145+
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
146+
* @param \Swis\JsonApi\Client\Links|null $links
147+
* @param \Swis\JsonApi\Client\Meta|null $meta
148+
*
149+
* @return static
150+
*/
151+
public function setRelation(string $name, DataInterface $data, Links $links = null, Meta $meta = null)
152+
{
153+
$method = Str::camel($name);
154+
if (method_exists($this, $method)) {
155+
/** @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationObject */
156+
$relationObject = $this->$method();
157+
} elseif ($data instanceof Collection) {
158+
$relationObject = $this->morphToMany($name);
159+
} else {
160+
$relationObject = $this->morphTo($name);
161+
}
162+
163+
$relationObject->associate($data);
164+
$relationObject->setLinks($links);
165+
$relationObject->setMeta($meta);
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* @param string $name
172+
*
173+
* @return bool
174+
*/
175+
public function hasRelation(string $name): bool
176+
{
177+
return array_key_exists($name, $this->relations);
178+
}
179+
180+
/**
181+
* @param $name
182+
*
183+
* @return static
184+
*/
185+
public function unsetRelation(string $name)
186+
{
187+
unset($this->relations[$name]);
188+
189+
return $this;
190+
}
191+
}

0 commit comments

Comments
 (0)