You are here

public static function Link::compare in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::compare()
  2. 9 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\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()
LinkCollection::withLink in core/modules/jsonapi/src/JsonApiResource/LinkCollection.php
Gets a new LinkCollection with the given link inserted.
LinkTest::testLinkComparison in core/modules/jsonapi/tests/src/Unit/JsonApiResource/LinkTest.php
@covers ::compare @dataProvider linkComparisonProvider

File

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

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;
}