Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Link.php

function Link::compare

Compares two links.

Parameters

\Drupal\jsonapi\JsonApiResource\Link $a: The first link.

\Drupal\jsonapi\JsonApiResource\Link $b: The second link.

Return value

int 0 if the links can be considered identical, an integer greater than or less than 0 otherwise.

2 calls to Link::compare()
Link::merge in core/modules/jsonapi/src/JsonApiResource/Link.php
Merges two equivalent links into one link with the merged cacheability.
LinkCollection::withLink in core/modules/jsonapi/src/JsonApiResource/LinkCollection.php
Gets a new LinkCollection with the given link inserted.

File

core/modules/jsonapi/src/JsonApiResource/Link.php, line 140

Class

Link
Represents an RFC8288 based link.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public static function compare(Link $a, Link $b) {
    // Any string concatenation would work, but a Link header-like format makes
    // it clear what is being compared.
    $a_string = sprintf('<%s>;rel="%s"', $a->getHref(), $a->rel);
    $b_string = sprintf('<%s>;rel="%s"', $b->getHref(), $b->rel);
    $cmp = strcmp($a_string, $b_string);
    // If the `href` or `rel` of the links are not equivalent, it's not
    // necessary to compare target attributes.
    if ($cmp === 0) {
        return (int) (!empty(DiffArray::diffAssocRecursive($a->getTargetAttributes(), $b->getTargetAttributes())));
    }
    return $cmp;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal