You are here

public function Link::__construct in JSON:API 8.2

JSON:API Link constructor.

Parameters

\Drupal\Core\Cache\CacheableMetadata $cacheability: Any cacheability metadata associated with the link. For example, a 'call-to-action' link might reference a registration resource if an event has vacancies or a wait-list resource otherwise. Therefore, the link's cacheability might be depend on a certain entity's values other than the entity on which the link will appear.

\Drupal\Core\Url $url: The Url object for the link.

string[] $link_relation_types: An array of registered or extension RFC8288 link relation types.

array $target_attributes: An associative array of target attributes for the link.

See also

https://tools.ietf.org/html/rfc8288#section-2.1

File

src/JsonApiResource/Link.php, line 74

Class

Link
Represents an RFC8288 based link.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public function __construct(CacheableMetadata $cacheability, Url $url, array $link_relation_types, array $target_attributes = []) {

  // @todo: uncomment the extra assertion below when JSON:API begins to use its own extension relation types.
  assert(Inspector::assertAllStrings($link_relation_types));
  assert(Inspector::assertAllStrings(array_keys($target_attributes)));
  assert(Inspector::assertAll(function ($target_attribute_value) {
    return is_string($target_attribute_value) || is_array($target_attribute_value) && Inspector::assertAllStrings($target_attribute_value);
  }, array_values($target_attributes)));
  $generated_url = $url
    ->setAbsolute()
    ->toString(TRUE);
  $this->href = $generated_url
    ->getGeneratedUrl();
  $this->uri = $url;
  $this->rel = $link_relation_types;
  $this->attributes = $target_attributes;
  $this
    ->setCacheability($cacheability
    ->addCacheableDependency($generated_url));
}