diff --git a/src/JsonApiResource/LinkCollection.php b/src/JsonApiResource/LinkCollection.php index bc5fb24..25288fd 100644 --- a/src/JsonApiResource/LinkCollection.php +++ b/src/JsonApiResource/LinkCollection.php @@ -120,7 +120,7 @@ final class LinkCollection implements \IteratorAggregate { /** * Gets the LinkCollection's context object. * - * @return \Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel + * @return \Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel|\Drupal\jsonapi\JsonApiResource\ResourceObject * The LinkCollection's context. */ public function getContext() { @@ -166,11 +166,15 @@ final class LinkCollection implements \IteratorAggregate { public static function merge(LinkCollection $a, LinkCollection $b) { assert($a->getContext() === $b->getContext()); $merged = new LinkCollection([], $a->getContext()); - foreach ($a as $key => $link) { - $merged = $merged->withLink($key, $link); + foreach ($a as $key => $links) { + $merged = array_reduce($links, function (self $merged, Link $link) use ($key) { + return $merged->withLink($key, $link); + }, $merged); } - foreach ($b as $key => $link) { - $merged = $merged->withLink($key, $link); + foreach ($b as $key => $links) { + $merged = array_reduce($links, function (self $merged, Link $link) use ($key) { + return $merged->withLink($key, $link); + }, $merged); } return $merged; }