You are here

abstract class Data in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Data.php \Drupal\jsonapi\JsonApiResource\Data

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

@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.

Hierarchy

  • class \Drupal\jsonapi\JsonApiResource\Data implements \Drupal\jsonapi\JsonApiResource\IteratorAggregate, \Drupal\jsonapi\JsonApiResource\Countable

Expanded class hierarchy of Data

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

4 files declare their use of Data
DataNormalizer.php in core/modules/jsonapi/src/Normalizer/DataNormalizer.php
EntityResource.php in core/modules/jsonapi/src/Controller/EntityResource.php
EntityResourceTest.php in core/modules/jsonapi/tests/src/Kernel/Controller/EntityResourceTest.php
IncludeResolver.php in core/modules/jsonapi/src/IncludeResolver.php
2 string references to 'Data'
UserViewsData::getViewsData in core/modules/user/src/UserViewsData.php
Returns views data for the entity type.
views.view.test_user_data.yml in core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_data.yml
core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_data.yml

File

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

Namespace

Drupal\jsonapi\JsonApiResource
View source
abstract class Data implements \IteratorAggregate, \Countable {

  /**
   * Various representations of JSON:API objects.
   *
   * @var \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[]
   */
  protected $data;

  /**
   * The number of resources permitted in this collection.
   *
   * @var int
   */
  protected $cardinality;

  /**
   * Holds a boolean indicating if there is a next page.
   *
   * @var bool
   */
  protected $hasNextPage;

  /**
   * Holds the total count of entities.
   *
   * @var int
   */
  protected $count;

  /**
   * Instantiates a Data object.
   *
   * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifierInterface[] $data
   *   The resources or resource identifiers for the collection.
   * @param 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.
   */
  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;
  }

  /**
   * Returns an iterator for entities.
   *
   * @return \ArrayIterator
   *   An \ArrayIterator instance
   */
  public function getIterator() {
    return new \ArrayIterator($this->data);
  }

  /**
   * Returns the number of entities.
   *
   * @return int
   *   The number of parameters
   */
  public function count() {
    return count($this->data);
  }

  /**
   * {@inheritdoc}
   */
  public function getTotalCount() {
    return $this->count;
  }

  /**
   * {@inheritdoc}
   */
  public function setTotalCount($count) {
    $this->count = $count;
  }

  /**
   * Returns the collection as an array.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   The array of entities.
   */
  public function toArray() {
    return $this->data;
  }

  /**
   * Checks if there is a next page in the collection.
   *
   * @return bool
   *   TRUE if the collection has a next page.
   */
  public function hasNextPage() {
    return (bool) $this->hasNextPage;
  }

  /**
   * Sets the has next page flag.
   *
   * Once the collection query has been executed and we build the entity
   * collection, we now if there will be a next page with extra entities.
   *
   * @param bool $has_next_page
   *   TRUE if the collection has a next page.
   */
  public function setHasNextPage($has_next_page) {
    $this->hasNextPage = (bool) $has_next_page;
  }

  /**
   * Gets the cardinality of this collection.
   *
   * @return int
   *   The cardinality of the resource collection. -1 for unlimited cardinality.
   */
  public function getCardinality() {
    return $this->cardinality;
  }

  /**
   * Returns a new Data object containing the entities of $this and $other.
   *
   * @param \Drupal\jsonapi\JsonApiResource\Data $a
   *   A Data object object to be merged.
   * @param \Drupal\jsonapi\JsonApiResource\Data $b
   *   A Data object to be merged.
   *
   * @return static
   *   A new merged Data object.
   */
  public static function merge(Data $a, Data $b) {
    return new static(array_merge($a
      ->toArray(), $b
      ->toArray()));
  }

  /**
   * Returns a new, deduplicated Data object.
   *
   * @param \Drupal\jsonapi\JsonApiResource\Data $collection
   *   The Data object to deduplicate.
   *
   * @return static
   *   A new merged Data object.
   */
  public static function deduplicate(Data $collection) {
    $deduplicated = [];
    foreach ($collection as $resource) {
      $dedupe_key = $resource
        ->getTypeName() . ':' . $resource
        ->getId();
      if ($resource instanceof EntityAccessDeniedHttpException && ($error = $resource
        ->getError()) && !is_null($error['relationship_field'])) {
        $dedupe_key .= ':' . $error['relationship_field'];
      }
      $deduplicated[$dedupe_key] = $resource;
    }
    return new static(array_values($deduplicated));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Data::$cardinality protected property The number of resources permitted in this collection.
Data::$count protected property Holds the total count of entities.
Data::$data protected property Various representations of JSON:API objects.
Data::$hasNextPage protected property Holds a boolean indicating if there is a next page.
Data::count public function Returns the number of entities.
Data::deduplicate public static function Returns a new, deduplicated Data object.
Data::getCardinality public function Gets the cardinality of this collection.
Data::getIterator public function Returns an iterator for entities.
Data::getTotalCount public function
Data::hasNextPage public function Checks if there is a next page in the collection.
Data::merge public static function Returns a new Data object containing the entities of $this and $other.
Data::setHasNextPage public function Sets the has next page flag.
Data::setTotalCount public function
Data::toArray public function Returns the collection as an array.
Data::__construct public function Instantiates a Data object. 2