function Data::__construct

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

Instantiates a Data object.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[] $data: The resources or resource identifiers for the collection.

int $cardinality: The number of resources that this collection may contain. Related resource collections may handle both to-one or to-many relationships. A to-one relationship should have a cardinality of 1. Use -1 for unlimited cardinality.

2 methods override Data::__construct()
RelationshipData::__construct in core/modules/jsonapi/src/JsonApiResource/RelationshipData.php
RelationshipData constructor.
ResourceObjectData::__construct in core/modules/jsonapi/src/JsonApiResource/ResourceObjectData.php
ResourceObjectData constructor.

File

core/modules/jsonapi/src/JsonApiResource/Data.php, line 58

Class

Data
Represents the `data` and `included` objects of a top-level object.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public function __construct(array $data, $cardinality = -1) {
    assert(Inspector::assertAllObjects($data, ResourceIdentifierInterface::class));
    assert($cardinality >= -1 && $cardinality !== 0, 'Cardinality must be -1 for unlimited cardinality or a positive integer.');
    assert($cardinality === -1 || count($data) <= $cardinality, 'If cardinality is not unlimited, the number of given resources must not exceed the cardinality of the collection.');
    $this->data = array_values($data);
    $this->cardinality = $cardinality;
}

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