ldc-boutique-cnc-sable/shared/Pagination/PageNumber.php

54 lines
1.1 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Shared\Pagination;
use Shared\DDD\Equatable;
/**
* Value object representing a page number in pagination (1-indexed).
*/
final readonly class PageNumber implements \JsonSerializable, \Stringable, Equatable
{
public const int DEFAULT_PAGE = 1;
public function __construct(
public int $value = self::DEFAULT_PAGE,
) {
if ($this->value <= 0) {
throw new \InvalidArgumentException('Page number must be greater than 0');
}
}
public static function of(int $value): self
{
return new self($value);
}
public static function default(): self
{
return new self(self::DEFAULT_PAGE);
}
public function value(): int
{
return $this->value;
}
public function equals(mixed $other): bool
{
return $other instanceof self && $this->value === $other->value;
}
public function __toString(): string
{
return (string) $this->value;
}
public function jsonSerialize(): int
{
return $this->value;
}
}