-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
263 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Sparors\Ussd\Attributes; | ||
|
||
use Attribute; | ||
use Sparors\Ussd\Contracts\Decision; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
final class Paginate | ||
{ | ||
public function __construct( | ||
public string|array|Decision $next, | ||
public null|string|array|Decision $previous = null, | ||
public null|string|array $callback = null | ||
) { } | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace Sparors\Ussd\Support; | ||
|
||
class Paginator | ||
{ | ||
public function __construct( | ||
public array $items | ||
) { } | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace Sparors\Ussd\Traits; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Facades\App; | ||
use Sparors\Ussd\Record; | ||
|
||
trait WithPagination | ||
{ | ||
private static $currentPage; | ||
|
||
private function lastPage(): int | ||
{ | ||
return ceil(count($this->getItems()) / $this->perPage()); | ||
} | ||
|
||
public function currentPage(): int | ||
{ | ||
if (isset(self::$currentPage)) { | ||
return self::$currentPage; | ||
} | ||
|
||
/** @var Record */ $record = App::make(Record::class); | ||
$pageId = Str::of(get_called_class())->replace('\\', '')->snake()->append('_page')->value(); | ||
$page = $record->get($pageId, 1); | ||
|
||
self::$currentPage = $page; | ||
|
||
return $page; | ||
} | ||
|
||
public function isFirstPage(): int | ||
{ | ||
return 1 === $this->currentPage(); | ||
} | ||
|
||
public function isLastPage(): int | ||
{ | ||
return $this->currentPage() === $this->lastPage(); | ||
} | ||
|
||
public function hasNextPage(): bool | ||
{ | ||
return $this->currentPage() < $this->lastPage(); | ||
} | ||
|
||
public function hasPreviousPage(): bool | ||
{ | ||
return $this->currentPage() > 1; | ||
} | ||
|
||
abstract public function getItems(): array; | ||
|
||
abstract public function perPage(): int; | ||
|
||
public function __destruct() | ||
{ | ||
self::$currentPage = null; | ||
} | ||
} |
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
Oops, something went wrong.