public static function Link::merge in JSON:API 8.2
Merges two link objects' relation types and target attributes.
The links must share the same URI.
Parameters
\Drupal\jsonapi\JsonApiResource\Link $a: The first link.
\Drupal\jsonapi\JsonApiResource\Link $b: The second link.
Return value
static A new JSON:API Link object with the link relation type and target attributes merged.
1 call to Link::merge()
- LinkCollection::withLink in src/
JsonApiResource/ LinkCollection.php - Gets a new LinkCollection with the given link inserted.
File
- src/
JsonApiResource/ Link.php, line 160
Class
- Link
- Represents an RFC8288 based link.
Namespace
Drupal\jsonapi\JsonApiResourceCode
public static function merge(Link $a, Link $b) {
assert(static::compare($a, $b) === 0);
$merged_rels = array_unique(array_merge($a
->getLinkRelationTypes(), $b
->getLinkRelationTypes()));
$merged_attributes = $a
->getTargetAttributes();
foreach ($b
->getTargetAttributes() as $key => $value) {
if (isset($merged_attributes[$key])) {
// The attribute values can be either a string or an array of strings.
$value = array_unique(array_merge(is_string($merged_attributes[$key]) ? [
$merged_attributes[$key],
] : $merged_attributes[$key], is_string($value) ? [
$value,
] : $value));
}
$merged_attributes[$key] = count($value) === 1 ? reset($value) : $value;
}
$merged_cacheability = (new CacheableMetadata())
->addCacheableDependency($a)
->addCacheableDependency($b);
return new static($merged_cacheability, $a
->getUri(), $merged_rels, $merged_attributes);
}