function LinkCollection::merge

Same name and namespace in other branches
  1. 8.9.x core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::merge()
  2. 10 core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::merge()
  3. 11.x core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::merge()

Merges two LinkCollections.

Parameters

\Drupal\jsonapi\JsonApiResource\LinkCollection $a: The first link collection.

\Drupal\jsonapi\JsonApiResource\LinkCollection $b: The second link collection.

Return value

\Drupal\jsonapi\JsonApiResource\LinkCollection A new LinkCollection with the links of both inputs.

1 call to LinkCollection::merge()
Relationship::getMergedLinks in core/modules/jsonapi/src/JsonApiResource/Relationship.php
Merges the object's links with the top-level links.

File

core/modules/jsonapi/src/JsonApiResource/LinkCollection.php, line 172

Class

LinkCollection
Contains a set of JSON:API Link objects.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public static function merge(LinkCollection $a, LinkCollection $b) {
    assert($a->getContext() === $b->getContext());
    $merged = new LinkCollection([], $a->getContext());
    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 => $links) {
        $merged = array_reduce($links, function (self $merged, Link $link) use ($key) {
            return $merged->withLink($key, $link);
        }, $merged);
    }
    return $merged;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.