You are here

public static function ResourceIdentifier::compare in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php \Drupal\jsonapi\JsonApiResource\ResourceIdentifier::compare()

Compares ResourceIdentifier objects.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceIdentifier $a: The first ResourceIdentifier object.

\Drupal\jsonapi\JsonApiResource\ResourceIdentifier $b: The second ResourceIdentifier object.

Return value

int Returns 0 if $a and $b are duplicate ResourceIdentifiers. If $a and $b identify the same resource but have distinct arity values, then the return value will be arity $a minus arity $b. -1 otherwise.

2 calls to ResourceIdentifier::compare()
ResourceIdentifier::isDuplicate in core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php
Determines if two ResourceIdentifiers are the same.
ResourceIdentifier::isParallel in core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php
Determines if two ResourceIdentifiers identify the same resource object.

File

core/modules/jsonapi/src/JsonApiResource/ResourceIdentifier.php, line 225

Class

ResourceIdentifier
Represents a JSON:API resource identifier object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public static function compare(ResourceIdentifier $a, ResourceIdentifier $b) {
  $result = strcmp(sprintf('%s:%s', $a
    ->getTypeName(), $a
    ->getId()), sprintf('%s:%s', $b
    ->getTypeName(), $b
    ->getId()));

  // If type and ID do not match, return their ordering.
  if ($result !== 0) {
    return $result;
  }

  // If both $a and $b have an arity, then return the order by arity.
  // Otherwise, they are considered equal.
  return $a
    ->hasArity() && $b
    ->hasArity() ? $a
    ->getArity() - $b
    ->getArity() : 0;
}