class ResourceIdentifier implements ResourceIdentifierInterface (View source)

internal  JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.
 

Represents a JSON:API resource identifier object.

The official JSON:API JSON-Schema document requires that no two resource identifier objects are duplicates, however Drupal allows multiple entity reference items to the same entity. Here, these are termed "parallel" relationships (as in "parallel edges" of a graph).

This class adds a concept of an @code arity @endcode member under each its

Constants

ARITY_KEY

Properties

protected string $resourceTypeName

The JSON:API resource type name.

protected ResourceType $resourceType

The JSON:API resource type.

protected string $id

The resource ID.

protected array $meta

The relationship's metadata.

Methods

__construct(ResourceType|string $resource_type, string $id, array $meta = [])

ResourceIdentifier constructor.

string
getTypeName()

Gets the ResourceIdentifier's JSON:API resource type name.

getResourceType()

Gets the resource identifier's JSON:API resource type.

string
getId()

Gets the ResourceIdentifier's ID.

int
hasArity()

Whether this ResourceIdentifier has an arity.

int
getArity()

Gets the ResourceIdentifier's arity.

withArity(int $arity)

Returns a copy of the given ResourceIdentifier with the given arity.

array
getMeta()

Gets the resource identifier objects metadata.

static bool
isDuplicate(ResourceIdentifier $a, ResourceIdentifier $b)

Determines if two ResourceIdentifiers are the same.

static bool
isParallel(ResourceIdentifier $a, ResourceIdentifier $b)

Determines if two ResourceIdentifiers identify the same resource object.

static int
compare(ResourceIdentifier $a, ResourceIdentifier $b)

Compares ResourceIdentifier objects.

static ResourceIdentifier[]
deduplicate(array $resource_identifiers)

Deduplicates an array of ResourceIdentifier objects.

static bool
areResourceIdentifiersUnique(array $resource_identifiers)

Determines if an array of ResourceIdentifier objects is duplicate free.

toResourceIdentifier(EntityReferenceItem $item, int $arity = NULL)

Creates a ResourceIdentifier object.

static ResourceIdentifier[]
toResourceIdentifiers(EntityReferenceFieldItemListInterface $items)

Creates an array of ResourceIdentifier objects.

static ResourceIdentifier[]
toResourceIdentifiersWithArityRequired(EntityReferenceFieldItemListInterface $items)

Creates an array of ResourceIdentifier objects with arity on every value.

fromEntity(EntityInterface $entity)

Creates a ResourceIdentifier object.

static string
getDataReferencePropertyName(EntityReferenceItem $item)

Helper method to determine which field item property contains an entity.

getVirtualOrMissingResourceIdentifier(EntityReferenceItem $item)

Creates a ResourceIdentifier for a NULL or FALSE entity reference item.

Details

__construct(ResourceType|string $resource_type, string $id, array $meta = [])

ResourceIdentifier constructor.

Parameters

ResourceType|string $resource_type

The JSON:API resource type or a JSON:API resource type name.

string $id

The resource ID.

array $meta

Any metadata for the ResourceIdentifier.

string getTypeName()

Gets the ResourceIdentifier's JSON:API resource type name.

Return Value

string

The JSON:API resource type name.

ResourceType getResourceType()

Gets the resource identifier's JSON:API resource type.

Return Value

ResourceType

The JSON:API resource type.

string getId()

Gets the ResourceIdentifier's ID.

Return Value

string

A resource ID.

int hasArity()

Whether this ResourceIdentifier has an arity.

Return Value

int

TRUE if the ResourceIdentifier has an arity, FALSE otherwise.

int getArity()

Gets the ResourceIdentifier's arity.

One must check self::hasArity() before calling this method.

Return Value

int

The arity.

ResourceIdentifier withArity(int $arity)

Returns a copy of the given ResourceIdentifier with the given arity.

Parameters

int $arity

The new arity; must be a non-negative integer.

Return Value

ResourceIdentifier

A newly created ResourceIdentifier with the given arity, otherwise the same.

array getMeta()

Gets the resource identifier objects metadata.

Return Value

array

The metadata.

static bool isDuplicate(ResourceIdentifier $a, ResourceIdentifier $b)

Determines if two ResourceIdentifiers are the same.

This method does not consider parallel relationships with different arity values to be duplicates. For that, use the isParallel() method.

Parameters

ResourceIdentifier $a

The first ResourceIdentifier object.

ResourceIdentifier $b

The second ResourceIdentifier object.

Return Value

bool

TRUE if both relationships reference the same resource and do not have two distinct arity's, FALSE otherwise.

For example, if $a and $b both reference the same resource identifier, they can only be distinct if they both have an arity and those values are not the same. If $a or $b does not have an arity, they will be considered duplicates.

static bool isParallel(ResourceIdentifier $a, ResourceIdentifier $b)

Determines if two ResourceIdentifiers identify the same resource object.

This method does not consider arity.

Parameters

ResourceIdentifier $a

The first ResourceIdentifier object.

ResourceIdentifier $b

The second ResourceIdentifier object.

Return Value

bool

TRUE if both relationships reference the same resource, even when they have differing arity values, FALSE otherwise.

static int compare(ResourceIdentifier $a, ResourceIdentifier $b)

Compares ResourceIdentifier objects.

Parameters

ResourceIdentifier $a

The first ResourceIdentifier object.

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.

static ResourceIdentifier[] deduplicate(array $resource_identifiers)

Deduplicates an array of ResourceIdentifier objects.

Parameters

array $resource_identifiers

The list of ResourceIdentifiers to deduplicate.

Return Value

ResourceIdentifier[]

A deduplicated array of ResourceIdentifier objects.

See also

\Drupal\jsonapi\JsonApiResource\self::isDuplicate()

static bool areResourceIdentifiersUnique(array $resource_identifiers)

Determines if an array of ResourceIdentifier objects is duplicate free.

Parameters

array $resource_identifiers

The list of ResourceIdentifiers to assess.

Return Value

bool

Whether all the given resource identifiers are unique.

static ResourceIdentifier toResourceIdentifier(EntityReferenceItem $item, int $arity = NULL)

Creates a ResourceIdentifier object.

Parameters

EntityReferenceItem $item

The entity reference field item from which to create the relationship.

int $arity

(optional) The arity of the relationship.

Return Value

ResourceIdentifier

A new ResourceIdentifier object.

static ResourceIdentifier[] toResourceIdentifiers(EntityReferenceFieldItemListInterface $items)

Creates an array of ResourceIdentifier objects.

Parameters

EntityReferenceFieldItemListInterface $items

The entity reference field items from which to create the relationship array.

Return Value

ResourceIdentifier[]

An array of new ResourceIdentifier objects with appropriate arity values.

static ResourceIdentifier[] toResourceIdentifiersWithArityRequired(EntityReferenceFieldItemListInterface $items)

Creates an array of ResourceIdentifier objects with arity on every value.

Parameters

EntityReferenceFieldItemListInterface $items

The entity reference field items from which to create the relationship array.

Return Value

ResourceIdentifier[]

An array of new ResourceIdentifier objects with appropriate arity values. Unlike self::toResourceIdentifiers(), this method does not omit arity when an identifier is not parallel to any other identifier.

static ResourceIdentifier fromEntity(EntityInterface $entity)

Creates a ResourceIdentifier object.

Parameters

EntityInterface $entity

The entity from which to create the resource identifier.

Return Value

ResourceIdentifier

A new ResourceIdentifier object.

static protected string getDataReferencePropertyName(EntityReferenceItem $item)

Helper method to determine which field item property contains an entity.

Parameters

EntityReferenceItem $item

The entity reference item for which to determine the entity property name.

Return Value

string

The property name which has an entity as its value.

static protected ResourceIdentifier getVirtualOrMissingResourceIdentifier(EntityReferenceItem $item)

Creates a ResourceIdentifier for a NULL or FALSE entity reference item.

Parameters

EntityReferenceItem $item

The entity reference field item.

Return Value

ResourceIdentifier

A new ResourceIdentifier object.