diff --git a/src/Entity/ArrayAccessTrait.php b/src/Entity/ArrayAccessTrait.php deleted file mode 100644 index 7a8e898d..00000000 --- a/src/Entity/ArrayAccessTrait.php +++ /dev/null @@ -1,12 +0,0 @@ -collection = $collection; - } - - public function getCollection(): CollectionInterface - { - if (!isset($this->collection)) { - throw new RuntimeException('missing collection'); - } - - return $this->collection; - } -} diff --git a/src/Entity/CollectionInterface.php b/src/Entity/CollectionInterface.php deleted file mode 100644 index b8371539..00000000 --- a/src/Entity/CollectionInterface.php +++ /dev/null @@ -1,22 +0,0 @@ -hydrateEntity($this->page['_embedded'][static::getCollectionName()][$this->current], $this->key()); - } - - /** - * No checks here, just advance the index. - */ - public function next(): void - { - $this->current++; - } - - /** - * Return the ID of the resource, in some cases this is `id`, in others `uuid`. - * - * @return string|int - */ - public function key() - { - return - $this->page['_embedded'][static::getCollectionName()][$this->current]['id'] ?? - $this->page['_embedded'][static::getCollectionName()][$this->current]['uuid'] ?? - $this->current; - } - - /** - * Handle pagination automatically (unless configured not to). - */ - public function valid(): bool - { - //can't be valid if there's not a page (rewind sets this) - if (!isset($this->page)) { - return false; - } - - //all hal collections have an `_embedded` object, we expect there to be a property matching the collection name - if (!isset($this->page['_embedded'][static::getCollectionName()])) { - return false; - } - - //if we have a page with no items, we've gone beyond the end of the collection - if (!count($this->page['_embedded'][static::getCollectionName()])) { - return false; - } - - //index the start of a page at 0 - if (is_null($this->current)) { - $this->current = 0; - } - - //if our current index is past the current page, fetch the next page if possible and reset the index - if (!isset($this->page['_embedded'][static::getCollectionName()][$this->current])) { - if (isset($this->page['_links']['next'])) { - $this->fetchPage($this->page['_links']['next']['href']); - $this->current = 0; - - return true; - } - - return false; - } - - return true; - } - - /** - * Fetch the initial page - */ - public function rewind(): void - { - $this->fetchPage(static::getCollectionPath()); - } - - /** - * Count of total items - */ - public function count(): ?int - { - if (isset($this->page)) { - return (int)$this->page['count']; - } - - return null; - } - - /** - * @param $index - * - * @return $this - */ - public function setPage($index): CollectionTrait - { - $this->index = (int)$index; - - return $this; - } - - /** - * @return int|mixed - */ - public function getPage() - { - if (isset($this->page)) { - return $this->page['page_index']; - } - - if (isset($this->index)) { - return $this->index; - } - - throw new RuntimeException('page not set'); - } - - /** - * @return int|mixed - */ - public function getSize() - { - if (isset($this->page)) { - return $this->page['page_size']; - } - - if (isset($this->size)) { - return $this->size; - } - - throw new RuntimeException('size not set'); - } - - /** - * @param $size - * - * @return $this - */ - public function setSize($size): CollectionTrait - { - $this->size = (int)$size; - - return $this; - } - - /** - * Filters reduce to query params and include paging settings. - * - * @return $this - */ - public function setFilter(FilterInterface $filter): self - { - $this->filter = $filter; - - return $this; - } - - public function getFilter(): FilterInterface - { - if (!isset($this->filter)) { - $this->setFilter(new EmptyFilter()); - } - - return $this->filter; - } - - /** - * Fetch a page using the current filter if no query is provided. - * - * @param $absoluteUri - */ - protected function fetchPage($absoluteUri): void - { - //use filter if no query provided - if (false === strpos($absoluteUri, '?')) { - $query = []; - - if (isset($this->size)) { - $query['page_size'] = $this->size; - } - - if (isset($this->index)) { - $query['page_index'] = $this->index; - } - - if (isset($this->filter)) { - $query = array_merge($this->filter->getQuery(), $query); - } - - $absoluteUri .= '?' . http_build_query($query); - } - - $request = new Request( - $this->getClient()->getApiUrl() . $absoluteUri, - 'GET' - ); - - $response = $this->client->send($request); - - if ((int)$response->getStatusCode() !== 200) { - throw $this->getException($response); - } - - $this->response = $response; - $this->page = json_decode($this->response->getBody()->getContents(), true); - } -} diff --git a/src/Entity/ModernCollectionTrait.php b/src/Entity/ModernCollectionTrait.php deleted file mode 100644 index 06b314ce..00000000 --- a/src/Entity/ModernCollectionTrait.php +++ /dev/null @@ -1,43 +0,0 @@ -page)) { - return (int)$this->page['total_items']; - } - - return 0; - } - - /** - * @return int|mixed - */ - public function getPage() - { - if (isset($this->page)) { - return $this->page['page']; - } - - if (isset($this->index)) { - return $this->index; - } - - throw new RuntimeException('page not set'); - } -}