139 lines
3.1 KiB
PHP
139 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\SearchRequest;
|
|
|
|
use Ulmus\Repository;
|
|
use Ulmus\SearchRequest\Attribute\SearchManual;
|
|
|
|
trait SearchRequestPaginationTrait {
|
|
use DeprecatedSearchRequestFormatTrait;
|
|
|
|
public int $count = 0;
|
|
|
|
#[SearchManual]
|
|
public int $page = 1;
|
|
|
|
public int $pageCount = 0;
|
|
|
|
#[SearchManual]
|
|
public int $limit = 25;
|
|
|
|
public bool $skipCount = false;
|
|
|
|
public ? array $columns = null;
|
|
|
|
public function count(): int
|
|
{
|
|
return $this->count;
|
|
}
|
|
|
|
public function page(): int
|
|
{
|
|
return $this->page;
|
|
}
|
|
|
|
public function limit(): int
|
|
{
|
|
return $this->limit;
|
|
}
|
|
|
|
public function offset(): int
|
|
{
|
|
return (int) ( abs( ( $this->page() - 1 ) * $this->limit() ) );
|
|
}
|
|
|
|
public function pagination(int $page, int $itemCount) : void
|
|
{
|
|
$this->count = $itemCount;
|
|
$this->page = $page;
|
|
}
|
|
|
|
public function pageCount() : int
|
|
{
|
|
return $this->limit() ? $this->pageCount = ceil($this->count() / $this->limit()) : 1;
|
|
}
|
|
|
|
public function hasPagination() : int
|
|
{
|
|
return $this->pageCount() > 1;
|
|
}
|
|
|
|
public function skipCount(bool $value) : self
|
|
{
|
|
$this->skipCount = $value;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function resultShown() : int
|
|
{
|
|
$total = $this->page() * $this->limit();
|
|
|
|
return $total <= $this->count() ? $total : $this->count();
|
|
}
|
|
|
|
public function jsonSerialize(): mixed
|
|
{
|
|
$this->pageCount();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function wheresConditions() : iterable
|
|
{
|
|
return array_filter(($this->wheresConditions ?? []) + [
|
|
|
|
], fn($i) => ! is_null($i) ) + [ ];
|
|
}
|
|
|
|
public function wheresOperators() : iterable
|
|
{
|
|
return array_filter(($this->wheresOperators ?? []) + [
|
|
|
|
], fn($i) => ! is_null($i) ) + [ ];
|
|
}
|
|
|
|
public function likesConditions() : iterable
|
|
{
|
|
return array_filter(($this->likesConditions ?? []) + [
|
|
|
|
], fn($i) => ! is_null($i) ) + [ ];
|
|
}
|
|
|
|
public function likesOperators() : iterable
|
|
{
|
|
return array_filter(($this->likesOperators ?? []) + [
|
|
|
|
], fn($i) => ! is_null($i) ) + [ ];
|
|
}
|
|
|
|
public function filter(Repository $repository) : Repository
|
|
{
|
|
return $repository;
|
|
}
|
|
|
|
public function applyCount(Repository\RepositoryInterface $repository): SearchRequestInterface
|
|
{
|
|
$this->count = $this->filter($repository)
|
|
->wheres($this->wheres(), $this->wheresOperators(), $this->wheresConditions())
|
|
->likes($this->likes(), $this->likesConditions())
|
|
->groups($this->groups())
|
|
->count();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function apply(Repository\RepositoryInterface $repository): SearchRequestInterface
|
|
{
|
|
$this->filter($repository)
|
|
->wheres($this->wheres(), $this->wheresOperators(), $this->wheresConditions())
|
|
->likes($this->likes(), $this->likesConditions())
|
|
->orders($this->orders())
|
|
->groups($this->groups())
|
|
->offset($this->offset())
|
|
->limit($this->limit());
|
|
|
|
return $this;
|
|
}
|
|
}
|