Skip to content

Commit 8c413c8

Browse files
authored
Merge pull request #97 from swisnl/bugfix/meta-in-relationship-data
Keep relationship data when linking items from included
2 parents 8f53423 + f8ee9e8 commit 8c413c8

File tree

10 files changed

+297
-45
lines changed

10 files changed

+297
-45
lines changed

src/Concerns/HasRelations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ public function getRelationValue(string $name): ?DataInterface
160160
// it is a relationship and will load and return the included items in the relationship
161161
$method = Util::stringCamel($name);
162162
if (method_exists($this, $method)) {
163-
return $this->$method()->getIncluded();
163+
return $this->$method()->getAssociated();
164164
}
165165

166166
// If the "attribute" exists as a relationship on the model, we will return
167167
// the included items in the relationship
168168
if ($this->hasRelation($name)) {
169-
return $this->getRelation($name)->getIncluded();
169+
return $this->getRelation($name)->getAssociated();
170170
}
171171

172172
return null;

src/Interfaces/ManyRelationInterface.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010

1111
interface ManyRelationInterface
1212
{
13+
/**
14+
* @param \Swis\JsonApi\Client\Collection|null $data
15+
*/
16+
public function setData(?Collection $data);
17+
18+
/**
19+
* @return \Swis\JsonApi\Client\Collection|null
20+
*/
21+
public function getData(): ?Collection;
22+
23+
/**
24+
* @return bool
25+
*/
26+
public function hasData(): bool;
27+
28+
/**
29+
* @param \Swis\JsonApi\Client\Collection $included
30+
*/
31+
public function setIncluded(Collection $included);
32+
33+
/**
34+
* @return \Swis\JsonApi\Client\Collection
35+
*/
36+
public function getIncluded(): Collection;
37+
38+
/**
39+
* @return bool
40+
*/
41+
public function hasIncluded(): bool;
42+
1343
/**
1444
* @param \Swis\JsonApi\Client\Collection $included
1545
*
@@ -23,14 +53,14 @@ public function associate(Collection $included);
2353
public function dissociate();
2454

2555
/**
26-
* @return bool
56+
* @return \Swis\JsonApi\Client\Collection
2757
*/
28-
public function hasIncluded(): bool;
58+
public function getAssociated(): Collection;
2959

3060
/**
31-
* @return \Swis\JsonApi\Client\Collection
61+
* @return bool
3262
*/
33-
public function getIncluded(): Collection;
63+
public function hasAssociated(): bool;
3464

3565
/**
3666
* @param bool $omitIncluded

src/Interfaces/OneRelationInterface.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@
99

1010
interface OneRelationInterface
1111
{
12+
/**
13+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $data
14+
*/
15+
public function setData(?ItemInterface $data);
16+
17+
/**
18+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
19+
*/
20+
public function getData(): ?ItemInterface;
21+
22+
/**
23+
* @return bool
24+
*/
25+
public function hasData(): bool;
26+
27+
/**
28+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $included
29+
*/
30+
public function setIncluded(?ItemInterface $included);
31+
32+
/**
33+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
34+
*/
35+
public function getIncluded(): ?ItemInterface;
36+
37+
/**
38+
* @return bool
39+
*/
40+
public function hasIncluded(): bool;
41+
1242
/**
1343
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included
1444
*
@@ -22,14 +52,14 @@ public function associate(ItemInterface $included);
2252
public function dissociate();
2353

2454
/**
25-
* @return bool
55+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
2656
*/
27-
public function hasIncluded(): bool;
57+
public function getAssociated(): ?ItemInterface;
2858

2959
/**
30-
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
60+
* @return bool
3161
*/
32-
public function getIncluded(): ?ItemInterface;
62+
public function hasAssociated(): bool;
3363

3464
/**
3565
* @param bool $omitIncluded

src/Item.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,26 @@ public function getRelationships(): array
248248
$relationships = [];
249249

250250
foreach ($this->getRelations() as $name => $relation) {
251-
if (!$relation->hasIncluded()) {
251+
if (!$relation->hasAssociated()) {
252252
continue;
253253
}
254254

255255
if ($relation instanceof OneRelationInterface) {
256256
$relationships[$name]['data'] = null;
257257

258-
if ($relation->getIncluded() !== null) {
258+
if ($relation->getAssociated() !== null) {
259259
$relationships[$name]['data'] = [
260-
'type' => $relation->getIncluded()->getType(),
261-
'id' => $relation->getIncluded()->getId(),
260+
'type' => $relation->getAssociated()->getType(),
261+
'id' => $relation->getAssociated()->getId(),
262262
];
263-
if ($relation->getIncluded()->getMeta()) {
264-
$relationships[$name]['data']['meta'] = $relation->getIncluded()->getMeta()->toArray();
263+
if ($relation->getAssociated()->getMeta()) {
264+
$relationships[$name]['data']['meta'] = $relation->getAssociated()->getMeta()->toArray();
265265
}
266266
}
267267
} elseif ($relation instanceof ManyRelationInterface) {
268268
$relationships[$name]['data'] = [];
269269

270-
foreach ($relation->getIncluded() as $item) {
270+
foreach ($relation->getAssociated() as $item) {
271271
$data = [
272272
'type' => $item->getType(),
273273
'id' => $item->getId(),

src/Parsers/DocumentParser.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,27 +213,29 @@ function (ItemInterface $item) use ($keyedItems) {
213213
foreach ($item->getRelations() as $name => $relation) {
214214
if ($relation instanceof OneRelationInterface) {
215215
/** @var \Swis\JsonApi\Client\Interfaces\ItemInterface|null $relatedItem */
216-
$relatedItem = $relation->getIncluded();
216+
$relatedItem = $relation->getData();
217217

218218
if ($relatedItem === null) {
219219
continue;
220220
}
221221

222222
$includedItem = $this->getItem($keyedItems, $relatedItem);
223223
if ($includedItem !== null) {
224-
$relation->associate($includedItem);
224+
$relation->setIncluded($includedItem);
225225
}
226226
} elseif ($relation instanceof ManyRelationInterface) {
227-
/** @var \Swis\JsonApi\Client\Collection $relatedCollection */
228-
$relatedCollection = $relation->getIncluded();
229-
230-
/** @var \Swis\JsonApi\Client\Interfaces\ItemInterface $relatedItem */
231-
foreach ($relatedCollection as $key => $relatedItem) {
232-
$includedItem = $this->getItem($keyedItems, $relatedItem);
233-
if ($includedItem !== null) {
234-
$relatedCollection->put($key, $includedItem);
235-
}
227+
/** @var \Swis\JsonApi\Client\Collection|null $relatedCollection */
228+
$relatedCollection = $relation->getData();
229+
230+
if ($relatedCollection === null) {
231+
continue;
236232
}
233+
234+
$relation->setIncluded(
235+
$relatedCollection->map(function (ItemInterface $relatedItem) use ($keyedItems) {
236+
return $this->getItem($keyedItems, $relatedItem) ?? $relatedItem;
237+
})
238+
);
237239
}
238240
}
239241
}

src/Relations/AbstractManyRelation.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,37 @@
88
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
99

1010
/**
11+
* @property \Swis\JsonApi\Client\Collection|false|null $data
1112
* @property \Swis\JsonApi\Client\Collection|false|null $included
1213
*/
1314
abstract class AbstractManyRelation extends AbstractRelation implements ManyRelationInterface
1415
{
16+
/**
17+
* @param \Swis\JsonApi\Client\Collection|null $data
18+
*
19+
* @return $this
20+
*/
21+
public function setData(?Collection $data)
22+
{
23+
$this->data = $data;
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* @return \Swis\JsonApi\Client\Collection|null
30+
*/
31+
public function getData(): ?Collection
32+
{
33+
return $this->data ?: null;
34+
}
35+
1536
/**
1637
* @param \Swis\JsonApi\Client\Collection $included
1738
*
1839
* @return $this
1940
*/
20-
public function associate(Collection $included)
41+
public function setIncluded(Collection $included)
2142
{
2243
$this->included = $included;
2344

@@ -32,6 +53,33 @@ public function getIncluded(): Collection
3253
return $this->included ?: new Collection();
3354
}
3455

56+
/**
57+
* @param \Swis\JsonApi\Client\Collection $included
58+
*
59+
* @return $this
60+
*/
61+
public function associate(Collection $included)
62+
{
63+
return $this->setData($included)
64+
->setIncluded($included);
65+
}
66+
67+
/**
68+
* @return \Swis\JsonApi\Client\Collection
69+
*/
70+
public function getAssociated(): Collection
71+
{
72+
if ($this->hasIncluded()) {
73+
return $this->getIncluded();
74+
}
75+
76+
if ($this->hasData()) {
77+
return $this->getData();
78+
}
79+
80+
return new Collection();
81+
}
82+
3583
/**
3684
* Sort the included collection by the given key.
3785
* You can also pass your own callback to determine how to sort the collection values.

src/Relations/AbstractOneRelation.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,37 @@
88
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
99

1010
/**
11+
* @property \Swis\JsonApi\Client\Interfaces\ItemInterface|false|null $data
1112
* @property \Swis\JsonApi\Client\Interfaces\ItemInterface|false|null $included
1213
*/
1314
abstract class AbstractOneRelation extends AbstractRelation implements OneRelationInterface
1415
{
1516
/**
16-
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included
17+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $data
1718
*
1819
* @return $this
1920
*/
20-
public function associate(ItemInterface $included)
21+
public function setData(?ItemInterface $data)
22+
{
23+
$this->data = $data;
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
30+
*/
31+
public function getData(): ?ItemInterface
32+
{
33+
return $this->data ?: null;
34+
}
35+
36+
/**
37+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $included
38+
*
39+
* @return $this
40+
*/
41+
public function setIncluded(?ItemInterface $included)
2142
{
2243
$this->included = $included;
2344

@@ -31,4 +52,31 @@ public function getIncluded(): ?ItemInterface
3152
{
3253
return $this->included ?: null;
3354
}
55+
56+
/**
57+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included
58+
*
59+
* @return $this
60+
*/
61+
public function associate(ItemInterface $included)
62+
{
63+
return $this->setData($included)
64+
->setIncluded($included);
65+
}
66+
67+
/**
68+
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
69+
*/
70+
public function getAssociated(): ?ItemInterface
71+
{
72+
if ($this->hasIncluded()) {
73+
return $this->getIncluded();
74+
}
75+
76+
if ($this->hasData()) {
77+
return $this->getData();
78+
}
79+
80+
return null;
81+
}
3482
}

0 commit comments

Comments
 (0)