Skip to content

Commit 3b6df54

Browse files
authored
Merge pull request #13 from swisnl/item-hydrator-nested-relations-fix
Fixes for hydrating/building deeply nested relationships
2 parents 8b0b6ec + 3699e5a commit 3b6df54

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/ItemHydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function hydrate(ItemInterface $item, array $attributes): ItemInterface
4848
*/
4949
protected function fill(ItemInterface $item, array $attributes = null)
5050
{
51-
$item->fill($attributes);
51+
$item->fill(array_diff_key($attributes, array_combine($item->getAvailableRelations(), $item->getAvailableRelations())));
5252
}
5353

5454
/**

src/Items/EloquentItem.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function getAvailableRelations(): array
119119
*
120120
* @return \Swis\JsonApi\Collection
121121
*/
122-
public function getIncluded()
122+
public function getIncluded(): Collection
123123
{
124124
$included = new Collection();
125125

@@ -128,19 +128,25 @@ public function getIncluded()
128128
if (!empty($relation->getType()) && null !== $relation->getId()) {
129129
$included->push($relation->toJsonApiArray());
130130
}
131+
$included = $included->merge($relation->getIncluded());
131132
} elseif ($relation instanceof IlluminateCollection) {
132133
$relation->each(
133-
function (ItemInterface $item) use ($included) {
134+
function (ItemInterface $item) use (&$included) {
134135
if (!empty($item->getType()) && null !== $item->getId()) {
135136
$included->push($item->toJsonApiArray());
136137
}
138+
$included = $included->merge($item->getIncluded());
137139
}
138140
);
139141
} else {
140142
throw new \Exception('Not yet implemented');
141143
}
142144
}
143145

144-
return $included;
146+
return $included->unique(
147+
function (array $item) {
148+
return $item['type'].':'.$item['id'];
149+
}
150+
);
145151
}
146152
}

src/Items/JenssegersItem.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function getRelationships(): array
183183
*
184184
* @return \Swis\JsonApi\Collection
185185
*/
186-
public function getIncluded()
186+
public function getIncluded(): Collection
187187
{
188188
$included = new Collection();
189189

@@ -192,25 +192,31 @@ public function getIncluded()
192192
continue;
193193
}
194194

195-
if ($relationship->getIncluded() instanceof ItemInterface) {
196-
$item = $relationship->getIncluded();
197-
if (!empty($item->getType()) && null !== $item->getId()) {
198-
$included->push($item->toJsonApiArray());
195+
$includedFromRelationship = $relationship->getIncluded();
196+
if ($includedFromRelationship instanceof ItemInterface) {
197+
if (!empty($includedFromRelationship->getType()) && null !== $includedFromRelationship->getId()) {
198+
$included->push($includedFromRelationship->toJsonApiArray());
199199
}
200-
} elseif ($relationship->getIncluded() instanceof Collection) {
201-
$relationship->getIncluded()->each(
202-
function (ItemInterface $item) use ($included) {
200+
$included = $included->merge($includedFromRelationship->getIncluded());
201+
} elseif ($includedFromRelationship instanceof Collection) {
202+
$includedFromRelationship->each(
203+
function (ItemInterface $item) use (&$included) {
203204
if (!empty($item->getType()) && null !== $item->getId()) {
204205
$included->push($item->toJsonApiArray());
205206
}
207+
$included = $included->merge($item->getIncluded());
206208
}
207209
);
208210
} else {
209211
throw new \Exception('Not yet implemented');
210212
}
211213
}
212214

213-
return $included;
215+
return $included->unique(
216+
function (array $item) {
217+
return $item['type'].':'.$item['id'];
218+
}
219+
);
214220
}
215221

216222
/**

0 commit comments

Comments
 (0)