function LinkCollection::__construct

Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::__construct()
  2. 10 core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::__construct()
  3. 11.x core/modules/jsonapi/src/JsonApiResource/LinkCollection.php \Drupal\jsonapi\JsonApiResource\LinkCollection::__construct()

LinkCollection constructor.

Parameters

\Drupal\jsonapi\JsonApiResource\Link[] $links: An associated array of key names and JSON:API Link objects.

\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel|\Drupal\jsonapi\JsonApiResource\ResourceObject|\Drupal\jsonapi\JsonApiResource\Relationship $context: (internal use only) The context object. Use the self::withContext() method to establish a context. This should be done automatically when a LinkCollection is passed into a context object.

File

core/modules/jsonapi/src/JsonApiResource/LinkCollection.php, line 47

Class

LinkCollection
Contains a set of JSON:API Link objects.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public function __construct(array $links, $context = NULL) {
    assert(Inspector::assertAll(function ($key) {
        return static::validKey($key);
    }, array_keys($links)));
    assert(Inspector::assertAll(function ($link) {
        return $link instanceof Link || is_array($link) && Inspector::assertAllObjects($link, Link::class);
    }, $links));
    assert(is_null($context) || Inspector::assertAllObjects([
        $context,
    ], JsonApiDocumentTopLevel::class, ResourceObject::class, Relationship::class));
    ksort($links);
    $this->links = array_map(function ($link) {
        return is_array($link) ? $link : [
            $link,
        ];
    }, $links);
    $this->context = $context;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.