Commit cca55e29 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add a pagination interface to the laravel paginator

parent 7e6efe87
<?php
namespace Source\Sanitize;
use Illuminate\Pagination\LengthAwarePaginator;
trait Paginates
{
protected int $perPage = 50;
protected int $page = 1;
protected array $appends = [];
protected ?LengthAwarePaginator $paginator;
/**
* @param int $perPage
*/
public function setPerPage(int $perPage): void
{
$this->perPage = $perPage;
}
/**
* @param int $page
*/
public function setPage(int $page): void
{
$this->page = $page;
}
/**
* @return array
*/
public function getItems(): array
{
if (!$this->paginator) {
return [];
}
return $this->paginator->toArray();
}
/**
* @param array $items
*/
public function setItems(array $items): void
{
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$currentItems = array_slice($items, $this->perPage * ($currentPage - 1), $this->perPage);
$this->paginator = new LengthAwarePaginator($currentItems, count($items), $this->perPage, $currentPage);
$this->paginator->appends($this->appends);
}
/**
* @param array $appends
*/
public function setAppends(array $appends): void
{
$this->appends = $appends;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment