From a9f0003e809d46093e783d2079be7a952b5f5e0d Mon Sep 17 00:00:00 2001 From: winter Date: Mon, 31 Mar 2025 21:29:22 +0100 Subject: implement backfilling for newly discovered actors --- Digitigrade/Pagination.php | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Digitigrade/Pagination.php (limited to 'Digitigrade/Pagination.php') diff --git a/Digitigrade/Pagination.php b/Digitigrade/Pagination.php new file mode 100644 index 0000000..75a876f --- /dev/null +++ b/Digitigrade/Pagination.php @@ -0,0 +1,60 @@ +body); + if (!isset($data->page)) { + throw new \RuntimeException("doesn't look like a pagination: $uri"); + } + $p = (new JsonMapper())->map($data, self::class); + return $p; + } + + private function become(self $other) { + $this->page = $other->page; + $this->totalPages = $other->totalPages; + $this->totalItems = $other->totalItems; + $this->nextPage = $other->nextPage; + $this->previousPage = $other->previousPage; + $this->items = &$other->items; + } + + /** + * Gets the next item from the pagination, going to the next page if needed. + * + * Careful! This behaves differently from PHP's built in `next`: it returns + * the first item, the first time it's called, rather than the second. + * @return mixed The next item, or `false` if at the end. + */ + public function next(): mixed { + if (current($this->items) === false) { + if (isset($this->nextPage)) { + $this->become(self::fromUri($this->nextPage)); + return $this->next(); + } else { + return false; + } + } else { + $item = current($this->items); + next($this->items); + return $item; + } + } +} \ No newline at end of file -- cgit v1.3