You are here

class EntityCollection in JSON:API 8

Wrapper to normalize collections with multiple entities.

@internal

Hierarchy

  • class \Drupal\jsonapi\Resource\EntityCollection implements \Drupal\jsonapi\Resource\IteratorAggregate, \Drupal\jsonapi\Resource\Countable

Expanded class hierarchy of EntityCollection

5 files declare their use of EntityCollection
EntityReferenceFieldNormalizer.php in src/Normalizer/EntityReferenceFieldNormalizer.php
EntityResource.php in src/Controller/EntityResource.php
EntityResourceTest.php in tests/src/Kernel/Controller/EntityResourceTest.php
JsonApiDocumentTopLevelNormalizer.php in src/Normalizer/JsonApiDocumentTopLevelNormalizer.php
Relationship.php in src/Normalizer/Relationship.php

File

src/Resource/EntityCollection.php, line 14

Namespace

Drupal\jsonapi\Resource
View source
class EntityCollection implements \IteratorAggregate, \Countable {

  /**
   * Entity storage.
   *
   * @var \Drupal\Core\Entity\EntityInterface[]
   */
  protected $entities;

  /**
   * 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 EntityCollection object.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null[] $entities
   *   The entities for the collection.
   */
  public function __construct(array $entities) {
    assert(Inspector::assertAll(function ($entity) {
      return $entity === NULL || $entity instanceof EntityInterface || $entity instanceof EntityAccessDeniedHttpException;
    }, $entities));
    $this->entities = $entities;
  }

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

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

  /**
   * {@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->entities;
  }

  /**
   * 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityCollection::$count protected property Holds the total count of entities.
EntityCollection::$entities protected property Entity storage.
EntityCollection::$hasNextPage protected property Holds a boolean indicating if there is a next page.
EntityCollection::count public function Returns the number of entities.
EntityCollection::getIterator public function Returns an iterator for entities.
EntityCollection::getTotalCount public function
EntityCollection::hasNextPage public function Checks if there is a next page in the collection.
EntityCollection::setHasNextPage public function Sets the has next page flag.
EntityCollection::setTotalCount public function
EntityCollection::toArray public function Returns the collection as an array.
EntityCollection::__construct public function Instantiates a EntityCollection object.