You are here

abstract class Data in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Data.php \Drupal\jsonapi\JsonApiResource\Data
  2. 9 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

2 files declare their use of Data
EntityResource.php in core/modules/jsonapi/src/Controller/EntityResource.php
IncludeResolver.php in core/modules/jsonapi/src/IncludeResolver.php
4 string references to 'Data'
EntityTestMapField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMapField.php
Provides base field definitions for an entity type.
MapBaseFieldTest::testUninstallMapItemBaseField in core/tests/Drupal/KernelTests/Core/Field/MapBaseFieldTest.php
Tests uninstalling map item base field.
UserViewsData::getViewsData in core/modules/user/src/UserViewsData.php
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
   */

  #[\ReturnTypeWillChange]
  public function getIterator() {
    return new \ArrayIterator($this->data);
  }

  /**
   * Returns the number of entities.
   *
   * @return int
   *   The number of parameters
   */

  #[\ReturnTypeWillChange]
  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 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