Pagination.php
- Pfad:
src/Domain/ValueObject/Pagination.php - Namespace: Domain\ValueObject
- Zeilen: 126 | Größe: 3,038 Bytes
- Geändert: 2025-12-28 01:07:23 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 1
- use Domain\Constants
Klassen 1
-
Paginationclass Zeile 11
Funktionen 10
-
__construct()public Zeile 13 -
create()public Zeile 28 -
withTotal()public Zeile 39 -
totalPages()public Zeile 47 -
hasNextPage()public Zeile 59 -
hasPrevPage()public Zeile 67 -
nextPage()public Zeile 75 -
prevPage()public Zeile 83 -
getPageRange()public Zeile 94 -
toArray()public Zeile 113
Verwendet von 1
Versionen 6
-
v6
2025-12-28 01:07 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v5
2025-12-28 01:06 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v4
2025-12-23 08:08 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v3
2025-12-23 04:47 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-23 04:46 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-23 04:45 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Domain\ValueObject;
// @responsibility: Pagination Value Object (immutable)
use Domain\Constants;
final class Pagination
{
public function __construct(
public readonly int $page,
public readonly int $limit,
public readonly int $offset,
public readonly int $totalCount = 0
) {
}
/**
* Create pagination from page and limit values.
*
* @param int $page Current page number (1-based)
* @param int $limit Items per page
* @param int $maxLimit Maximum allowed limit
*/
public static function create(int $page = 1, int $limit = 50, int $maxLimit = Constants::DEFAULT_LIMIT): self
{
$page = max(1, $page);
$limit = min($maxLimit, max(1, $limit));
return new self($page, $limit, ($page - 1) * $limit);
}
/**
* Create new instance with total count set.
*/
public function withTotal(int $count): self
{
return new self($this->page, $this->limit, $this->offset, $count);
}
/**
* Calculate total number of pages.
*/
public function totalPages(): int
{
if ($this->totalCount === 0) {
return 0;
}
return (int) ceil($this->totalCount / $this->limit);
}
/**
* Check if there is a next page.
*/
public function hasNextPage(): bool
{
return $this->page < $this->totalPages();
}
/**
* Check if there is a previous page.
*/
public function hasPrevPage(): bool
{
return $this->page > 1;
}
/**
* Get next page number (or current if at last page).
*/
public function nextPage(): int
{
return $this->hasNextPage() ? $this->page + 1 : $this->page;
}
/**
* Get previous page number (or 1 if at first page).
*/
public function prevPage(): int
{
return $this->hasPrevPage() ? $this->page - 1 : 1;
}
/**
* Get range of visible page numbers for pagination UI.
*
* @param int $range Number of pages to show around current page
* @return array<int>
*/
public function getPageRange(int $range = 2): array
{
$totalPages = $this->totalPages();
if ($totalPages === 0) {
return [];
}
$start = max(1, $this->page - $range);
$end = min($totalPages, $this->page + $range);
return range($start, $end);
}
/**
* Convert to array for template usage.
*
* @return array{page: int, limit: int, offset: int, totalCount: int, totalPages: int, hasNext: bool, hasPrev: bool}
*/
public function toArray(): array
{
return [
'page' => $this->page,
'limit' => $this->limit,
'offset' => $this->offset,
'totalCount' => $this->totalCount,
'totalPages' => $this->totalPages(),
'hasNext' => $this->hasNextPage(),
'hasPrev' => $this->hasPrevPage(),
];
}
}