mirror of
https://github.com/anikeen-com/id.git
synced 2026-03-14 14:16:31 +00:00
refactored code
Signed-off-by: Maurice Preuß (envoyr) <hello@envoyr.com>
This commit is contained in:
@@ -7,70 +7,120 @@ use stdClass;
|
||||
|
||||
class Paginator
|
||||
{
|
||||
|
||||
/**
|
||||
* Next desired action (first, after, before).
|
||||
* Next desired action: 'first', 'after', 'before'.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public ?string $action = null;
|
||||
|
||||
/**
|
||||
* AnikeenId response pagination cursor.
|
||||
* Raw pagination links from API ('first','last','prev','next').
|
||||
*
|
||||
* @var stdClass|null
|
||||
*/
|
||||
private ?stdClass $pagination;
|
||||
private ?stdClass $links;
|
||||
|
||||
/**
|
||||
* Raw pagination meta from API (current_page, last_page, etc.).
|
||||
*
|
||||
* @var stdClass|null
|
||||
*/
|
||||
private ?stdClass $meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param null|stdClass $pagination AnikeenId response pagination cursor
|
||||
* @param stdClass|null $links Pagination links object
|
||||
* @param stdClass|null $meta Pagination meta object
|
||||
*/
|
||||
public function __construct(?stdClass $pagination = null)
|
||||
public function __construct(?stdClass $links = null, ?stdClass $meta = null)
|
||||
{
|
||||
$this->pagination = $pagination;
|
||||
$this->links = $links;
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Paginator from Result object.
|
||||
* Create Paginator from a Result instance.
|
||||
*/
|
||||
public static function from(Result $result): self
|
||||
{
|
||||
return new self($result->pagination);
|
||||
return new self($result->links, $result->meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current active cursor.
|
||||
* Return the cursor value (page number) based on the last set action.
|
||||
*/
|
||||
public function cursor(): string
|
||||
{
|
||||
return $this->pagination->cursor;
|
||||
switch ($this->action) {
|
||||
case 'first':
|
||||
return '1';
|
||||
|
||||
case 'after':
|
||||
// Try parsing from 'next' link
|
||||
if ($this->links && !empty($this->links->next)) {
|
||||
return $this->parsePageFromUrl($this->links->next);
|
||||
}
|
||||
// Fallback to current_page + 1
|
||||
return isset($this->meta->current_page)
|
||||
? (string)($this->meta->current_page + 1)
|
||||
: '1';
|
||||
|
||||
case 'before':
|
||||
if ($this->links && !empty($this->links->prev)) {
|
||||
return $this->parsePageFromUrl($this->links->prev);
|
||||
}
|
||||
// Fallback to current_page - 1
|
||||
return isset($this->meta->current_page)
|
||||
? (string)($this->meta->current_page - 1)
|
||||
: '1';
|
||||
|
||||
default:
|
||||
// Default to current page
|
||||
return isset($this->meta->current_page)
|
||||
? (string)$this->meta->current_page
|
||||
: '1';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the next set of results.
|
||||
* Parse the 'page' query parameter from a URL.
|
||||
*/
|
||||
private function parsePageFromUrl(string $url): string
|
||||
{
|
||||
$parts = parse_url($url);
|
||||
if (empty($parts['query'])) {
|
||||
return '1';
|
||||
}
|
||||
parse_str($parts['query'], $vars);
|
||||
return $vars['page'] ?? '1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the first page.
|
||||
*/
|
||||
public function first(): self
|
||||
{
|
||||
$this->action = 'first';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the first set of results.
|
||||
* Fetch the next page (after).
|
||||
*/
|
||||
public function next(): self
|
||||
{
|
||||
$this->action = 'after';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Paginator to fetch the last set of results.
|
||||
* Fetch the previous page (before).
|
||||
*/
|
||||
public function back(): self
|
||||
{
|
||||
$this->action = 'before';
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user