-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from DirectoryTree/remove-illuminate-support
Remove illuminate/support to be framework agnostic
- Loading branch information
Showing
8 changed files
with
337 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
namespace DirectoryTree\ImapEngine\Pagination; | ||
|
||
use DirectoryTree\ImapEngine\Support\ForwardsCalls; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Support\Collection; | ||
use JsonSerializable; | ||
|
||
class LengthAwarePaginator implements Arrayable, JsonSerializable | ||
{ | ||
use ForwardsCalls; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct( | ||
protected Collection $items, | ||
protected int $total, | ||
protected int $perPage, | ||
protected int $currentPage = 1, | ||
protected string $path = '', | ||
protected array $query = [], | ||
protected string $pageName = 'page', | ||
) { | ||
$this->currentPage = max($currentPage, 1); | ||
|
||
$this->path = rtrim($path, '/'); | ||
} | ||
|
||
/** | ||
* Handle dynamic method calls on the paginator. | ||
*/ | ||
public function __call(string $method, array $parameters): mixed | ||
{ | ||
return $this->forwardCallTo($this->items, $method, $parameters); | ||
} | ||
|
||
/** | ||
* Get the items being paginated. | ||
*/ | ||
public function items(): Collection | ||
{ | ||
return $this->items; | ||
} | ||
|
||
/** | ||
* Get the total number of items. | ||
*/ | ||
public function total(): int | ||
{ | ||
return $this->total; | ||
} | ||
|
||
/** | ||
* Get the number of items per page. | ||
*/ | ||
public function perPage(): int | ||
{ | ||
return $this->perPage; | ||
} | ||
|
||
/** | ||
* Get the current page number. | ||
*/ | ||
public function currentPage(): int | ||
{ | ||
return $this->currentPage; | ||
} | ||
|
||
/** | ||
* Get the last page (total pages). | ||
*/ | ||
public function lastPage(): int | ||
{ | ||
return (int) ceil($this->total / $this->perPage); | ||
} | ||
|
||
/** | ||
* Determine if there are enough items to split into multiple pages. | ||
*/ | ||
public function hasPages(): bool | ||
{ | ||
return $this->total() > $this->perPage(); | ||
} | ||
|
||
/** | ||
* Determine if there is a next page. | ||
*/ | ||
public function hasMorePages(): bool | ||
{ | ||
return $this->currentPage() < $this->lastPage(); | ||
} | ||
|
||
/** | ||
* Generate the URL for a given page. | ||
*/ | ||
public function url(int $page): string | ||
{ | ||
$params = array_merge($this->query, [$this->pageName => $page]); | ||
|
||
$queryString = http_build_query($params); | ||
|
||
return $this->path.($queryString ? '?'.$queryString : ''); | ||
} | ||
|
||
/** | ||
* Get the URL for the next page, or null if none. | ||
*/ | ||
public function nextPageUrl(): ?string | ||
{ | ||
if ($this->hasMorePages()) { | ||
return $this->url($this->currentPage() + 1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the URL for the previous page, or null if none. | ||
*/ | ||
public function previousPageUrl(): ?string | ||
{ | ||
if ($this->currentPage() > 1) { | ||
return $this->url($this->currentPage() - 1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Convert the pagination data to an array. | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'path' => $this->path, | ||
'total' => $this->total(), | ||
'to' => $this->calculateTo(), | ||
'per_page' => $this->perPage(), | ||
'last_page' => $this->lastPage(), | ||
'first_page_url' => $this->url(1), | ||
'data' => $this->items()->toArray(), | ||
'current_page' => $this->currentPage(), | ||
'next_page_url' => $this->nextPageUrl(), | ||
'prev_page_url' => $this->previousPageUrl(), | ||
'last_page_url' => $this->url($this->lastPage()), | ||
'from' => $this->total() ? ($this->currentPage() - 1) * $this->perPage() + 1 : null, | ||
]; | ||
} | ||
|
||
/** | ||
* Calculate the "to" index for the current page. | ||
*/ | ||
protected function calculateTo(): ?int | ||
{ | ||
if (! $this->total()) { | ||
return null; | ||
} | ||
|
||
$to = $this->currentPage() * $this->perPage(); | ||
|
||
return min($to, $this->total()); | ||
} | ||
|
||
/** | ||
* Convert the instance to JSON. | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return $this->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace DirectoryTree\ImapEngine\Support; | ||
|
||
use BadMethodCallException; | ||
use Error; | ||
|
||
trait ForwardsCalls | ||
{ | ||
/** | ||
* Forward a method call to the given object. | ||
*/ | ||
protected function forwardCallTo(object $object, string $method, array $parameters): mixed | ||
{ | ||
try { | ||
return $object->{$method}(...$parameters); | ||
} catch (Error|BadMethodCallException $e) { | ||
$pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~'; | ||
|
||
if (! preg_match($pattern, $e->getMessage(), $matches)) { | ||
throw $e; | ||
} | ||
|
||
if ($matches['class'] != get_class($object) || | ||
$matches['method'] != $method) { | ||
throw $e; | ||
} | ||
|
||
static::throwBadMethodCallException($method); | ||
} | ||
} | ||
|
||
/** | ||
* Throw a bad method call exception for the given method. | ||
*/ | ||
protected static function throwBadMethodCallException(string $method) | ||
{ | ||
throw new BadMethodCallException(sprintf( | ||
'Call to undefined method %s::%s()', static::class, $method | ||
)); | ||
} | ||
} |
Oops, something went wrong.