class Relationship implements TopLevelDataInterface (View source)

internal  JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.
 

Represents references from one resource object to other resource object(s).

Properties

protected ResourceObject $context

The context resource object of the relationship.

protected RelationshipData $data

The data of the relationship object.

protected string $fieldName

The relationship's public field name.

$links

The relationship object's links.

protected array $meta

The relationship object's meta member.

Methods

__construct(string $public_field_name, RelationshipData $data, LinkCollection $links, array $meta, ResourceObject $context)

Relationship constructor.

static Relationship
createFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links = NULL, array $meta = [])

Creates a new Relationship from an entity reference field.

getContext()

Gets context resource object of the relationship.

string
getFieldName()

Gets the relationship object's public field name.

getData()

Gets the relationship object's data.

getLinks()

Gets the relationship object's links.

array
getMeta()

Gets the relationship object's metadata.

getOmissions()

Returns the data that was omitted from the JSON:API document.

getMergedLinks(LinkCollection $top_level_links)

Merges the object's links with the top-level links.

array
getMergedMeta(array $top_level_meta)

Merges the object's meta member with the top-level meta member.

static LinkCollection
buildLinkCollectionFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links)

Builds a LinkCollection for the given entity reference field.

Details

protected __construct(string $public_field_name, RelationshipData $data, LinkCollection $links, array $meta, ResourceObject $context)

Relationship constructor.

This constructor is protected by design. To create a new relationship, use static::createFromEntityReferenceField().

Parameters

string $public_field_name

The public field name of the relationship field.

RelationshipData $data

The relationship data.

LinkCollection $links

Any links for the resource object, if a self link is not provided, one will be automatically added if the resource is locatable and is not internal.

array $meta

Any relationship metadata.

ResourceObject $context

The relationship's context resource object. Use the self::withContext() method to establish a context.

See also

Relationship::createFromEntityReferenceField

static Relationship createFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links = NULL, array $meta = [])

Creates a new Relationship from an entity reference field.

Parameters

ResourceObject $context

The context resource object of the relationship to be created.

EntityReferenceFieldItemListInterface $field

The entity reference field from which to create the relationship.

LinkCollection $links

(optional) Any extra links for the Relationship, if a self link is not provided, one will be automatically added if the context resource is locatable and is not internal.

array $meta

(optional) Any relationship metadata.

Return Value

Relationship

An instantiated relationship object.

ResourceObject getContext()

Gets context resource object of the relationship.

Return Value

ResourceObject

The context ResourceObject.

See also

Relationship::$context

string getFieldName()

Gets the relationship object's public field name.

Return Value

string

The relationship's field name.

Data getData()

Gets the relationship object's data.

Return Value

Data

The top-level data.

Gets the relationship object's links.

Return Value

LinkCollection

The relationship object's links.

array getMeta()

Gets the relationship object's metadata.

Return Value

array

The relationship object's metadata.

OmittedData getOmissions()

Returns the data that was omitted from the JSON:API document.

Return Value

OmittedData

The omitted data.

Merges the object's links with the top-level links.

Parameters

LinkCollection $top_level_links

The top-level links to merge.

Return Value

LinkCollection

The merged links.

array getMergedMeta(array $top_level_meta)

Merges the object's meta member with the top-level meta member.

Parameters

array $top_level_meta

The top-level links to merge.

Return Value

array

The merged meta member.

static protected LinkCollection buildLinkCollectionFromEntityReferenceField(ResourceObject $context, EntityReferenceFieldItemListInterface $field, LinkCollection $links)

Builds a LinkCollection for the given entity reference field.

Parameters

ResourceObject $context

The context resource object of the relationship object.

EntityReferenceFieldItemListInterface $field

The entity reference field from which to create the links.

LinkCollection $links

Any extra links for the Relationship, if a self link is not provided, one will be automatically added if the context resource is locatable and is not internal.

Return Value

LinkCollection

The built links.