@@ -46,15 +46,15 @@ |
||
| 46 | 46 | return $result; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param int $perPage |
|
| 51 | - * @param int $currentPage |
|
| 52 | - * @return PaginatorInterface |
|
| 53 | - */ |
|
| 54 | - public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 55 | - { |
|
| 56 | - return new Paginator($this, $perPage, $currentPage); |
|
| 57 | - } |
|
| 49 | + /** |
|
| 50 | + * @param int $perPage |
|
| 51 | + * @param int $currentPage |
|
| 52 | + * @return PaginatorInterface |
|
| 53 | + */ |
|
| 54 | + public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 55 | + { |
|
| 56 | + return new Paginator($this, $perPage, $currentPage); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * @inheritDoc |
@@ -182,20 +182,20 @@ |
||
| 182 | 182 | self::$connection = $config; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public function setData(array $data) |
|
| 186 | - { |
|
| 187 | - $this->data = $data; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - public function setModifiedFields($modifiedFields) |
|
| 191 | - { |
|
| 192 | - $this->modifiedFields = $modifiedFields; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - public function setIsNew($isNew) |
|
| 196 | - { |
|
| 197 | - $this->isNew = $isNew; |
|
| 198 | - } |
|
| 185 | + public function setData(array $data) |
|
| 186 | + { |
|
| 187 | + $this->data = $data; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + public function setModifiedFields($modifiedFields) |
|
| 191 | + { |
|
| 192 | + $this->modifiedFields = $modifiedFields; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + public function setIsNew($isNew) |
|
| 196 | + { |
|
| 197 | + $this->isNew = $isNew; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | /** |
| 201 | 201 | * @inheritDoc |
@@ -35,15 +35,15 @@ |
||
| 35 | 35 | return $this->getOrmModel()->find_many(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param int $perPage |
|
| 40 | - * @param int $currentPage |
|
| 41 | - * @return PaginatorInterface |
|
| 42 | - */ |
|
| 43 | - public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 44 | - { |
|
| 45 | - return new Paginator($this, $perPage, $currentPage); |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * @param int $perPage |
|
| 40 | + * @param int $currentPage |
|
| 41 | + * @return PaginatorInterface |
|
| 42 | + */ |
|
| 43 | + public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface |
|
| 44 | + { |
|
| 45 | + return new Paginator($this, $perPage, $currentPage); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * @inheritDoc |
@@ -144,12 +144,12 @@ |
||
| 144 | 144 | */ |
| 145 | 145 | public function get(); |
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * @param int $perPage |
|
| 149 | - * @param int $currentPage |
|
| 150 | - * @return PaginatorInterface |
|
| 151 | - */ |
|
| 152 | - public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface; |
|
| 147 | + /** |
|
| 148 | + * @param int $perPage |
|
| 149 | + * @param int $currentPage |
|
| 150 | + * @return PaginatorInterface |
|
| 151 | + */ |
|
| 152 | + public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface; |
|
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * Returns the count |
@@ -4,35 +4,35 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface PaginatorInterface |
| 6 | 6 | { |
| 7 | - public function getPagination(bool $withBaseUrl): ?string; |
|
| 7 | + public function getPagination(bool $withBaseUrl): ?string; |
|
| 8 | 8 | |
| 9 | - public function currentPageNumber(): int; |
|
| 9 | + public function currentPageNumber(): int; |
|
| 10 | 10 | |
| 11 | - public function currentPageLink(bool $withBaseUrl): ?string; |
|
| 11 | + public function currentPageLink(bool $withBaseUrl): ?string; |
|
| 12 | 12 | |
| 13 | - public function previousPageLink(bool $withBaseUrl): ?string; |
|
| 13 | + public function previousPageLink(bool $withBaseUrl): ?string; |
|
| 14 | 14 | |
| 15 | - public function previousPageNumber(): ?int; |
|
| 15 | + public function previousPageNumber(): ?int; |
|
| 16 | 16 | |
| 17 | - public function firstPageLink(bool $withBaseUrl): ?string; |
|
| 17 | + public function firstPageLink(bool $withBaseUrl): ?string; |
|
| 18 | 18 | |
| 19 | - public function nextPageLink(bool $withBaseUrl): ?string; |
|
| 19 | + public function nextPageLink(bool $withBaseUrl): ?string; |
|
| 20 | 20 | |
| 21 | - public function nextPageNumber(): ?int; |
|
| 21 | + public function nextPageNumber(): ?int; |
|
| 22 | 22 | |
| 23 | - public function lastPageLink(bool $withBaseUrl): ?string; |
|
| 23 | + public function lastPageLink(bool $withBaseUrl): ?string; |
|
| 24 | 24 | |
| 25 | - public function lastPageNumber(); |
|
| 25 | + public function lastPageNumber(); |
|
| 26 | 26 | |
| 27 | - public function firstItem(); |
|
| 27 | + public function firstItem(); |
|
| 28 | 28 | |
| 29 | - public function lastItem(); |
|
| 29 | + public function lastItem(); |
|
| 30 | 30 | |
| 31 | - public function perPage(); |
|
| 31 | + public function perPage(); |
|
| 32 | 32 | |
| 33 | - public function total(); |
|
| 33 | + public function total(); |
|
| 34 | 34 | |
| 35 | - public function links(bool $withBaseUrl); |
|
| 35 | + public function links(bool $withBaseUrl); |
|
| 36 | 36 | |
| 37 | - public function data(); |
|
| 37 | + public function data(); |
|
| 38 | 38 | } |
@@ -11,67 +11,67 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Paginator extends BasePaginator |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var SleekDbal |
|
| 16 | - */ |
|
| 17 | - private $dbal; |
|
| 14 | + /** |
|
| 15 | + * @var SleekDbal |
|
| 16 | + */ |
|
| 17 | + private $dbal; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - public $data; |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + public $data; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param $sleekDbal |
|
| 26 | - * @param int $perPage |
|
| 27 | - * @param int $page |
|
| 28 | - * @throws DatabaseException |
|
| 29 | - * @throws ModelException |
|
| 30 | - * @throws IOException |
|
| 31 | - * @throws InvalidArgumentException |
|
| 32 | - * @throws InvalidConfigurationException |
|
| 33 | - */ |
|
| 34 | - public function __construct($sleekDbal, int $perPage, int $page = 1) |
|
| 35 | - { |
|
| 36 | - /** @var SleekDbal $sleekDbal */ |
|
| 37 | - $this->total = count($sleekDbal->getBuilder()->getQuery()->fetch()); |
|
| 38 | - $this->dbal = $sleekDbal; |
|
| 39 | - $this->dbal->limit($perPage)->offset($perPage * ($page - 1)); |
|
| 40 | - $this->data = $this->dbal->getBuilder()->getQuery()->fetch(); |
|
| 41 | - $this->perPage = $perPage; |
|
| 42 | - $this->page = $page; |
|
| 43 | - $this->baseUrl = base_url(); |
|
| 44 | - } |
|
| 24 | + /** |
|
| 25 | + * @param $sleekDbal |
|
| 26 | + * @param int $perPage |
|
| 27 | + * @param int $page |
|
| 28 | + * @throws DatabaseException |
|
| 29 | + * @throws ModelException |
|
| 30 | + * @throws IOException |
|
| 31 | + * @throws InvalidArgumentException |
|
| 32 | + * @throws InvalidConfigurationException |
|
| 33 | + */ |
|
| 34 | + public function __construct($sleekDbal, int $perPage, int $page = 1) |
|
| 35 | + { |
|
| 36 | + /** @var SleekDbal $sleekDbal */ |
|
| 37 | + $this->total = count($sleekDbal->getBuilder()->getQuery()->fetch()); |
|
| 38 | + $this->dbal = $sleekDbal; |
|
| 39 | + $this->dbal->limit($perPage)->offset($perPage * ($page - 1)); |
|
| 40 | + $this->data = $this->dbal->getBuilder()->getQuery()->fetch(); |
|
| 41 | + $this->perPage = $perPage; |
|
| 42 | + $this->page = $page; |
|
| 43 | + $this->baseUrl = base_url(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @return mixed |
|
| 48 | - */ |
|
| 49 | - public function firstItem() |
|
| 50 | - { |
|
| 51 | - return $this->data[array_key_first($this->data)]; |
|
| 52 | - } |
|
| 46 | + /** |
|
| 47 | + * @return mixed |
|
| 48 | + */ |
|
| 49 | + public function firstItem() |
|
| 50 | + { |
|
| 51 | + return $this->data[array_key_first($this->data)]; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - public function lastItem() |
|
| 58 | - { |
|
| 59 | - return $this->data[array_key_last($this->data)]; |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + public function lastItem() |
|
| 58 | + { |
|
| 59 | + return $this->data[array_key_last($this->data)]; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return array|SleekDbal[] |
|
| 64 | - */ |
|
| 65 | - public function data(): array |
|
| 66 | - { |
|
| 67 | - $result = array_map(function ($element) { |
|
| 68 | - $item = clone $this->dbal; |
|
| 69 | - $item->setData($element); |
|
| 70 | - $item->setModifiedFields($element); |
|
| 71 | - $item->setIsNew(false); |
|
| 72 | - return $item; |
|
| 73 | - }, $this->data); |
|
| 62 | + /** |
|
| 63 | + * @return array|SleekDbal[] |
|
| 64 | + */ |
|
| 65 | + public function data(): array |
|
| 66 | + { |
|
| 67 | + $result = array_map(function ($element) { |
|
| 68 | + $item = clone $this->dbal; |
|
| 69 | + $item->setData($element); |
|
| 70 | + $item->setModifiedFields($element); |
|
| 71 | + $item->setIsNew(false); |
|
| 72 | + return $item; |
|
| 73 | + }, $this->data); |
|
| 74 | 74 | |
| 75 | - return $result ?? []; |
|
| 76 | - } |
|
| 75 | + return $result ?? []; |
|
| 76 | + } |
|
| 77 | 77 | } |
| 78 | 78 | \ No newline at end of file |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | */ |
| 65 | 65 | public function data(): array |
| 66 | 66 | { |
| 67 | - $result = array_map(function ($element) { |
|
| 67 | + $result = array_map(function($element) { |
|
| 68 | 68 | $item = clone $this->dbal; |
| 69 | 69 | $item->setData($element); |
| 70 | 70 | $item->setModifiedFields($element); |
@@ -8,61 +8,61 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Paginator extends BasePaginator |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var IdiormDbal |
|
| 13 | - */ |
|
| 14 | - private $dbal; |
|
| 11 | + /** |
|
| 12 | + * @var IdiormDbal |
|
| 13 | + */ |
|
| 14 | + private $dbal; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var array|IdiormResultSet |
|
| 18 | - */ |
|
| 19 | - public $data; |
|
| 16 | + /** |
|
| 17 | + * @var array|IdiormResultSet |
|
| 18 | + */ |
|
| 19 | + public $data; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param $idiormDbal |
|
| 23 | - * @param int $perPage |
|
| 24 | - * @param int $page |
|
| 25 | - * @throws DatabaseException |
|
| 26 | - */ |
|
| 27 | - public function __construct($idiormDbal, int $perPage, int $page = 1) |
|
| 28 | - { |
|
| 29 | - /** @var IdiormDbal $idiormDbal */ |
|
| 30 | - $this->total = $idiormDbal->getOrmModel()->count(); |
|
| 31 | - $this->dbal = $idiormDbal; |
|
| 32 | - $this->dbal->limit($perPage)->offset($perPage * ($page - 1)); |
|
| 33 | - $this->data = $this->dbal->getOrmModel()->find_many(); |
|
| 34 | - $this->perPage = $perPage; |
|
| 35 | - $this->page = $page; |
|
| 36 | - $this->baseUrl = base_dir(); |
|
| 37 | - } |
|
| 21 | + /** |
|
| 22 | + * @param $idiormDbal |
|
| 23 | + * @param int $perPage |
|
| 24 | + * @param int $page |
|
| 25 | + * @throws DatabaseException |
|
| 26 | + */ |
|
| 27 | + public function __construct($idiormDbal, int $perPage, int $page = 1) |
|
| 28 | + { |
|
| 29 | + /** @var IdiormDbal $idiormDbal */ |
|
| 30 | + $this->total = $idiormDbal->getOrmModel()->count(); |
|
| 31 | + $this->dbal = $idiormDbal; |
|
| 32 | + $this->dbal->limit($perPage)->offset($perPage * ($page - 1)); |
|
| 33 | + $this->data = $this->dbal->getOrmModel()->find_many(); |
|
| 34 | + $this->perPage = $perPage; |
|
| 35 | + $this->page = $page; |
|
| 36 | + $this->baseUrl = base_dir(); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return mixed |
|
| 41 | - */ |
|
| 42 | - public function firstItem() |
|
| 43 | - { |
|
| 44 | - if (!is_array($this->data)){ |
|
| 45 | - $this->data = $this->data->as_array(); |
|
| 46 | - } |
|
| 47 | - return $this->data[array_key_first($this->data)]; |
|
| 48 | - } |
|
| 39 | + /** |
|
| 40 | + * @return mixed |
|
| 41 | + */ |
|
| 42 | + public function firstItem() |
|
| 43 | + { |
|
| 44 | + if (!is_array($this->data)){ |
|
| 45 | + $this->data = $this->data->as_array(); |
|
| 46 | + } |
|
| 47 | + return $this->data[array_key_first($this->data)]; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return mixed |
|
| 52 | - */ |
|
| 53 | - public function lastItem() |
|
| 54 | - { |
|
| 55 | - if (!is_array($this->data)){ |
|
| 56 | - $this->data = $this->data->as_array(); |
|
| 57 | - } |
|
| 58 | - return $this->data[array_key_last($this->data)]; |
|
| 59 | - } |
|
| 50 | + /** |
|
| 51 | + * @return mixed |
|
| 52 | + */ |
|
| 53 | + public function lastItem() |
|
| 54 | + { |
|
| 55 | + if (!is_array($this->data)){ |
|
| 56 | + $this->data = $this->data->as_array(); |
|
| 57 | + } |
|
| 58 | + return $this->data[array_key_last($this->data)]; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @return array|IdiormResultSet |
|
| 63 | - */ |
|
| 64 | - public function data() |
|
| 65 | - { |
|
| 66 | - return $this->data ?? []; |
|
| 67 | - } |
|
| 61 | + /** |
|
| 62 | + * @return array|IdiormResultSet |
|
| 63 | + */ |
|
| 64 | + public function data() |
|
| 65 | + { |
|
| 66 | + return $this->data ?? []; |
|
| 67 | + } |
|
| 68 | 68 | } |
| 69 | 69 | \ No newline at end of file |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | public function firstItem() |
| 43 | 43 | { |
| 44 | - if (!is_array($this->data)){ |
|
| 44 | + if (!is_array($this->data)) { |
|
| 45 | 45 | $this->data = $this->data->as_array(); |
| 46 | 46 | } |
| 47 | 47 | return $this->data[array_key_first($this->data)]; |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function lastItem() |
| 54 | 54 | { |
| 55 | - if (!is_array($this->data)){ |
|
| 55 | + if (!is_array($this->data)) { |
|
| 56 | 56 | $this->data = $this->data->as_array(); |
| 57 | 57 | } |
| 58 | 58 | return $this->data[array_key_last($this->data)]; |
@@ -4,300 +4,300 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class BasePaginator implements PaginatorInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var string |
|
| 9 | - */ |
|
| 10 | - protected const PAGINATION_CLASS = 'pagination'; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected const PAGINATION_CLASS_ACTIVE = 'active'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected const PER_PAGE = 'per_page'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected const PAGE = 'page'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected const FIRST_PAGE_NUMBER = 1; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var int |
|
| 34 | - */ |
|
| 35 | - protected $total; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var string |
|
| 39 | - */ |
|
| 40 | - protected $baseUrl; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - protected $perPage; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var int |
|
| 49 | - */ |
|
| 50 | - protected $page; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return int |
|
| 54 | - */ |
|
| 55 | - public function currentPageNumber(): int |
|
| 56 | - { |
|
| 57 | - return $this->page; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param bool $withBaseUrl |
|
| 62 | - * @return string|null |
|
| 63 | - */ |
|
| 64 | - public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 65 | - { |
|
| 66 | - return $this->getPageLink($this->page, $withBaseUrl); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return int|null |
|
| 71 | - */ |
|
| 72 | - public function previousPageNumber(): ?int |
|
| 73 | - { |
|
| 74 | - $previous = null; |
|
| 75 | - if ($this->page > 1) { |
|
| 76 | - $previous = $this->page - 1; |
|
| 77 | - } elseif ($this->page == 1) { |
|
| 78 | - $previous = $this->page; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $previous; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param bool $withBaseUrl |
|
| 86 | - * @return string|null |
|
| 87 | - */ |
|
| 88 | - public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 89 | - { |
|
| 90 | - return $this->getPageLink($this->previousPageNumber(), $withBaseUrl); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return int|null |
|
| 95 | - */ |
|
| 96 | - public function nextPageNumber(): ?int |
|
| 97 | - { |
|
| 98 | - $next = null; |
|
| 99 | - if ($this->page < $this->lastPageNumber()) { |
|
| 100 | - $next = $this->page + 1; |
|
| 101 | - } elseif ($this->page == $this->lastPageNumber()) { |
|
| 102 | - $next = $this->page; |
|
| 103 | - } |
|
| 104 | - return $next; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param bool $withBaseUrl |
|
| 109 | - * @return string|null |
|
| 110 | - */ |
|
| 111 | - public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 112 | - { |
|
| 113 | - return $this->getPageLink($this->nextPageNumber(), $withBaseUrl); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param bool $withBaseUrl |
|
| 118 | - * @return string|null |
|
| 119 | - */ |
|
| 120 | - public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 121 | - { |
|
| 122 | - return $this->getPageLink(self::FIRST_PAGE_NUMBER, $withBaseUrl); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return int |
|
| 127 | - */ |
|
| 128 | - public function lastPageNumber(): int |
|
| 129 | - { |
|
| 130 | - return (int)ceil($this->total() / $this->perPage); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param bool $withBaseUrl |
|
| 135 | - * @return string|null |
|
| 136 | - */ |
|
| 137 | - public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 138 | - { |
|
| 139 | - return $this->getPageLink($this->lastPageNumber(), $withBaseUrl); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return int |
|
| 144 | - */ |
|
| 145 | - public function perPage(): int |
|
| 146 | - { |
|
| 147 | - return $this->perPage; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @return int |
|
| 152 | - */ |
|
| 153 | - public function total(): int |
|
| 154 | - { |
|
| 155 | - return $this->total; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @param bool $withBaseUrl |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function links(bool $withBaseUrl = false): array |
|
| 163 | - { |
|
| 164 | - $links = []; |
|
| 165 | - for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 166 | - $links[] = $this->getPageLink($i, $withBaseUrl); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return $links; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param bool $withBaseUrl |
|
| 174 | - * @return string |
|
| 175 | - */ |
|
| 176 | - protected function getUri(bool $withBaseUrl = false): string |
|
| 177 | - { |
|
| 178 | - $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 179 | - $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 180 | - $url = $routeUrl; |
|
| 181 | - |
|
| 182 | - if ($withBaseUrl) { |
|
| 183 | - $url = $this->baseUrl . $routeUrl; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $delimiter = strpos($url, '?') ? '&' : '?'; |
|
| 187 | - return $url . $delimiter; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - protected function getPageLink($pageNumber, $withBaseUrl = false): ?string |
|
| 191 | - { |
|
| 192 | - if (!empty($pageNumber)){ |
|
| 193 | - return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber; |
|
| 194 | - } |
|
| 195 | - return null; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @param bool $withBaseUrl |
|
| 200 | - * @param $pageItemsCount |
|
| 201 | - * @return string|null |
|
| 202 | - */ |
|
| 203 | - public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 204 | - { |
|
| 205 | - if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 206 | - $pageItemsCount = 3; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - $currentPage = $this->currentPageNumber(); |
|
| 210 | - $totalPages = $this->lastPageNumber(); |
|
| 211 | - |
|
| 212 | - if ($totalPages <= 1) { |
|
| 213 | - return null; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $pagination = '<ul class="'. self::PAGINATION_CLASS .'">'; |
|
| 217 | - |
|
| 218 | - if ($currentPage > 1) { |
|
| 219 | - $pagination .= $this->getPreviousPageItem($this->previousPageLink()); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - if ($pageItemsCount) { |
|
| 223 | - $pagination = $this->getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - if ($currentPage < $totalPages) { |
|
| 227 | - $pagination .= $this->getNextPageItem($this->nextPageLink()); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $pagination .= '</ul>'; |
|
| 231 | - |
|
| 232 | - return $pagination; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param $pagination |
|
| 237 | - * @param $currentPage |
|
| 238 | - * @param $totalPages |
|
| 239 | - * @param $pageItemsCount |
|
| 240 | - * @param $withBaseUrl |
|
| 241 | - * @return mixed|string |
|
| 242 | - */ |
|
| 243 | - protected function getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl = false) |
|
| 244 | - { |
|
| 245 | - $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 246 | - $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 247 | - $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 248 | - |
|
| 249 | - if ($startPage > 1) { |
|
| 250 | - $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 251 | - if ($startPage > 2) { |
|
| 252 | - $pagination .= '<li><span>...</span></li>'; |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $links = $this->links($withBaseUrl); |
|
| 257 | - $pagination = $this->getItemsLinks($pagination, $startPage, $endPage, $currentPage, $links, $withBaseUrl); |
|
| 258 | - |
|
| 259 | - if ($endPage < $totalPages) { |
|
| 260 | - if ($endPage < $totalPages - 1) { |
|
| 261 | - $pagination .= '<li><span>...</span></li>'; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 265 | - } |
|
| 266 | - return $pagination; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * @param string|null $nextPageLink |
|
| 271 | - * @return string |
|
| 272 | - */ |
|
| 273 | - protected function getNextPageItem(?string $nextPageLink): string |
|
| 274 | - { |
|
| 275 | - $link = ''; |
|
| 276 | - if (!empty($nextPageLink)){ |
|
| 277 | - $link = '<li><a href="' . $nextPageLink . '">Next »</a></li>'; |
|
| 278 | - } |
|
| 279 | - return $link; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @param string|null $previousPageLink |
|
| 284 | - * @return string |
|
| 285 | - */ |
|
| 286 | - protected function getPreviousPageItem(?string $previousPageLink): string |
|
| 287 | - { |
|
| 288 | - $link = ''; |
|
| 289 | - if (!empty($previousPageLink)){ |
|
| 290 | - $link = '<li><a href="' . $previousPageLink . '">« Previous</a></li>'; |
|
| 291 | - } |
|
| 292 | - return $link; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links) |
|
| 296 | - { |
|
| 297 | - for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 298 | - $active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : ''; |
|
| 299 | - $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 300 | - } |
|
| 301 | - return $pagination; |
|
| 302 | - } |
|
| 7 | + /** |
|
| 8 | + * @var string |
|
| 9 | + */ |
|
| 10 | + protected const PAGINATION_CLASS = 'pagination'; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected const PAGINATION_CLASS_ACTIVE = 'active'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected const PER_PAGE = 'per_page'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected const PAGE = 'page'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected const FIRST_PAGE_NUMBER = 1; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var int |
|
| 34 | + */ |
|
| 35 | + protected $total; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var string |
|
| 39 | + */ |
|
| 40 | + protected $baseUrl; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + protected $perPage; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var int |
|
| 49 | + */ |
|
| 50 | + protected $page; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return int |
|
| 54 | + */ |
|
| 55 | + public function currentPageNumber(): int |
|
| 56 | + { |
|
| 57 | + return $this->page; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param bool $withBaseUrl |
|
| 62 | + * @return string|null |
|
| 63 | + */ |
|
| 64 | + public function currentPageLink(bool $withBaseUrl = false): ?string |
|
| 65 | + { |
|
| 66 | + return $this->getPageLink($this->page, $withBaseUrl); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return int|null |
|
| 71 | + */ |
|
| 72 | + public function previousPageNumber(): ?int |
|
| 73 | + { |
|
| 74 | + $previous = null; |
|
| 75 | + if ($this->page > 1) { |
|
| 76 | + $previous = $this->page - 1; |
|
| 77 | + } elseif ($this->page == 1) { |
|
| 78 | + $previous = $this->page; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $previous; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param bool $withBaseUrl |
|
| 86 | + * @return string|null |
|
| 87 | + */ |
|
| 88 | + public function previousPageLink(bool $withBaseUrl = false): ?string |
|
| 89 | + { |
|
| 90 | + return $this->getPageLink($this->previousPageNumber(), $withBaseUrl); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return int|null |
|
| 95 | + */ |
|
| 96 | + public function nextPageNumber(): ?int |
|
| 97 | + { |
|
| 98 | + $next = null; |
|
| 99 | + if ($this->page < $this->lastPageNumber()) { |
|
| 100 | + $next = $this->page + 1; |
|
| 101 | + } elseif ($this->page == $this->lastPageNumber()) { |
|
| 102 | + $next = $this->page; |
|
| 103 | + } |
|
| 104 | + return $next; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param bool $withBaseUrl |
|
| 109 | + * @return string|null |
|
| 110 | + */ |
|
| 111 | + public function nextPageLink(bool $withBaseUrl = false): ?string |
|
| 112 | + { |
|
| 113 | + return $this->getPageLink($this->nextPageNumber(), $withBaseUrl); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param bool $withBaseUrl |
|
| 118 | + * @return string|null |
|
| 119 | + */ |
|
| 120 | + public function firstPageLink(bool $withBaseUrl = false): ?string |
|
| 121 | + { |
|
| 122 | + return $this->getPageLink(self::FIRST_PAGE_NUMBER, $withBaseUrl); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return int |
|
| 127 | + */ |
|
| 128 | + public function lastPageNumber(): int |
|
| 129 | + { |
|
| 130 | + return (int)ceil($this->total() / $this->perPage); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param bool $withBaseUrl |
|
| 135 | + * @return string|null |
|
| 136 | + */ |
|
| 137 | + public function lastPageLink(bool $withBaseUrl = false): ?string |
|
| 138 | + { |
|
| 139 | + return $this->getPageLink($this->lastPageNumber(), $withBaseUrl); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return int |
|
| 144 | + */ |
|
| 145 | + public function perPage(): int |
|
| 146 | + { |
|
| 147 | + return $this->perPage; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @return int |
|
| 152 | + */ |
|
| 153 | + public function total(): int |
|
| 154 | + { |
|
| 155 | + return $this->total; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @param bool $withBaseUrl |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function links(bool $withBaseUrl = false): array |
|
| 163 | + { |
|
| 164 | + $links = []; |
|
| 165 | + for ($i = 1; $i <= $this->lastPageNumber(); $i++) { |
|
| 166 | + $links[] = $this->getPageLink($i, $withBaseUrl); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return $links; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param bool $withBaseUrl |
|
| 174 | + * @return string |
|
| 175 | + */ |
|
| 176 | + protected function getUri(bool $withBaseUrl = false): string |
|
| 177 | + { |
|
| 178 | + $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri()); |
|
| 179 | + $routeUrl = preg_replace('/&/', '?', $routeUrl, 1); |
|
| 180 | + $url = $routeUrl; |
|
| 181 | + |
|
| 182 | + if ($withBaseUrl) { |
|
| 183 | + $url = $this->baseUrl . $routeUrl; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $delimiter = strpos($url, '?') ? '&' : '?'; |
|
| 187 | + return $url . $delimiter; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + protected function getPageLink($pageNumber, $withBaseUrl = false): ?string |
|
| 191 | + { |
|
| 192 | + if (!empty($pageNumber)){ |
|
| 193 | + return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber; |
|
| 194 | + } |
|
| 195 | + return null; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @param bool $withBaseUrl |
|
| 200 | + * @param $pageItemsCount |
|
| 201 | + * @return string|null |
|
| 202 | + */ |
|
| 203 | + public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string |
|
| 204 | + { |
|
| 205 | + if (!is_null($pageItemsCount) && $pageItemsCount < 3) { |
|
| 206 | + $pageItemsCount = 3; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + $currentPage = $this->currentPageNumber(); |
|
| 210 | + $totalPages = $this->lastPageNumber(); |
|
| 211 | + |
|
| 212 | + if ($totalPages <= 1) { |
|
| 213 | + return null; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $pagination = '<ul class="'. self::PAGINATION_CLASS .'">'; |
|
| 217 | + |
|
| 218 | + if ($currentPage > 1) { |
|
| 219 | + $pagination .= $this->getPreviousPageItem($this->previousPageLink()); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + if ($pageItemsCount) { |
|
| 223 | + $pagination = $this->getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + if ($currentPage < $totalPages) { |
|
| 227 | + $pagination .= $this->getNextPageItem($this->nextPageLink()); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $pagination .= '</ul>'; |
|
| 231 | + |
|
| 232 | + return $pagination; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param $pagination |
|
| 237 | + * @param $currentPage |
|
| 238 | + * @param $totalPages |
|
| 239 | + * @param $pageItemsCount |
|
| 240 | + * @param $withBaseUrl |
|
| 241 | + * @return mixed|string |
|
| 242 | + */ |
|
| 243 | + protected function getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl = false) |
|
| 244 | + { |
|
| 245 | + $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2)); |
|
| 246 | + $endPage = min($totalPages, $startPage + $pageItemsCount - 3); |
|
| 247 | + $startPage = max(1, $endPage - $pageItemsCount + 3); |
|
| 248 | + |
|
| 249 | + if ($startPage > 1) { |
|
| 250 | + $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>'; |
|
| 251 | + if ($startPage > 2) { |
|
| 252 | + $pagination .= '<li><span>...</span></li>'; |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $links = $this->links($withBaseUrl); |
|
| 257 | + $pagination = $this->getItemsLinks($pagination, $startPage, $endPage, $currentPage, $links, $withBaseUrl); |
|
| 258 | + |
|
| 259 | + if ($endPage < $totalPages) { |
|
| 260 | + if ($endPage < $totalPages - 1) { |
|
| 261 | + $pagination .= '<li><span>...</span></li>'; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>'; |
|
| 265 | + } |
|
| 266 | + return $pagination; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * @param string|null $nextPageLink |
|
| 271 | + * @return string |
|
| 272 | + */ |
|
| 273 | + protected function getNextPageItem(?string $nextPageLink): string |
|
| 274 | + { |
|
| 275 | + $link = ''; |
|
| 276 | + if (!empty($nextPageLink)){ |
|
| 277 | + $link = '<li><a href="' . $nextPageLink . '">Next »</a></li>'; |
|
| 278 | + } |
|
| 279 | + return $link; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @param string|null $previousPageLink |
|
| 284 | + * @return string |
|
| 285 | + */ |
|
| 286 | + protected function getPreviousPageItem(?string $previousPageLink): string |
|
| 287 | + { |
|
| 288 | + $link = ''; |
|
| 289 | + if (!empty($previousPageLink)){ |
|
| 290 | + $link = '<li><a href="' . $previousPageLink . '">« Previous</a></li>'; |
|
| 291 | + } |
|
| 292 | + return $link; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links) |
|
| 296 | + { |
|
| 297 | + for ($i = $startPage; $i <= $endPage; $i++) { |
|
| 298 | + $active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : ''; |
|
| 299 | + $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
|
| 300 | + } |
|
| 301 | + return $pagination; |
|
| 302 | + } |
|
| 303 | 303 | } |
| 304 | 304 | \ No newline at end of file |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | public function lastPageNumber(): int |
| 129 | 129 | { |
| 130 | - return (int)ceil($this->total() / $this->perPage); |
|
| 130 | + return (int) ceil($this->total() / $this->perPage); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -189,8 +189,8 @@ discard block |
||
| 189 | 189 | |
| 190 | 190 | protected function getPageLink($pageNumber, $withBaseUrl = false): ?string |
| 191 | 191 | { |
| 192 | - if (!empty($pageNumber)){ |
|
| 193 | - return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber; |
|
| 192 | + if (!empty($pageNumber)) { |
|
| 193 | + return $this->getUri($withBaseUrl) . self::PER_PAGE . '=' . $this->perPage . '&' . self::PAGE . '=' . $pageNumber; |
|
| 194 | 194 | } |
| 195 | 195 | return null; |
| 196 | 196 | } |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | return null; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - $pagination = '<ul class="'. self::PAGINATION_CLASS .'">'; |
|
| 216 | + $pagination = '<ul class="' . self::PAGINATION_CLASS . '">'; |
|
| 217 | 217 | |
| 218 | 218 | if ($currentPage > 1) { |
| 219 | 219 | $pagination .= $this->getPreviousPageItem($this->previousPageLink()); |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | protected function getNextPageItem(?string $nextPageLink): string |
| 274 | 274 | { |
| 275 | 275 | $link = ''; |
| 276 | - if (!empty($nextPageLink)){ |
|
| 276 | + if (!empty($nextPageLink)) { |
|
| 277 | 277 | $link = '<li><a href="' . $nextPageLink . '">Next »</a></li>'; |
| 278 | 278 | } |
| 279 | 279 | return $link; |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | protected function getPreviousPageItem(?string $previousPageLink): string |
| 287 | 287 | { |
| 288 | 288 | $link = ''; |
| 289 | - if (!empty($previousPageLink)){ |
|
| 289 | + if (!empty($previousPageLink)) { |
|
| 290 | 290 | $link = '<li><a href="' . $previousPageLink . '">« Previous</a></li>'; |
| 291 | 291 | } |
| 292 | 292 | return $link; |
@@ -295,7 +295,7 @@ discard block |
||
| 295 | 295 | protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links) |
| 296 | 296 | { |
| 297 | 297 | for ($i = $startPage; $i <= $endPage; $i++) { |
| 298 | - $active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : ''; |
|
| 298 | + $active = $i == $currentPage ? 'class="' . self::PAGINATION_CLASS_ACTIVE . '"' : ''; |
|
| 299 | 299 | $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>'; |
| 300 | 300 | } |
| 301 | 301 | return $pagination; |