Skip to content

Commit 0727aae

Browse files
authored
Merge pull request #5 from Anthony-Saresoft/Anthony-Saresoft-patch-4
fix: Code cleanup, adaptation to PHP 8 and correction of pagination error
2 parents 1682312 + 1520a24 commit 0727aae

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

src/PHPPagination/Pagination.php

+26-42
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ class Pagination
171171
/**
172172
* Stores the generated pagination structure.
173173
*
174-
* @var string
174+
* @var array
175175
*/
176176
protected $pages = null;
177-
177+
178178
/**
179179
* Constructor class.
180180
*/
@@ -274,7 +274,7 @@ public function large()
274274

275275
return $this;
276276
}
277-
277+
278278
/**
279279
* Sets the Bootstrap 4 alignment class to left.
280280
*
@@ -311,20 +311,6 @@ public function alignRight()
311311
return $this;
312312
}
313313

314-
/**
315-
* Sets the show separator flag to true.
316-
*
317-
* Shows the separator blocks.
318-
*
319-
* @return PHPPagination
320-
*/
321-
public function showSeparator()
322-
{
323-
$this->show_separator = true;
324-
325-
return $this;
326-
}
327-
328314
/**
329315
* Sets the show separator flag to false.
330316
*
@@ -579,7 +565,7 @@ protected function buildURL($page)
579565
}
580566

581567
if ($this->fragment_query_string) {
582-
$url .= '#'.$this->fragment_query_string;
568+
$url .= '#' . $this->fragment_query_string;
583569
}
584570

585571
return $url;
@@ -624,71 +610,71 @@ protected function pageText($page_text)
624610
*/
625611
protected function generate()
626612
{
627-
$this->total_pages = $this->items_per_page != 0 ? ceil($this->total_items / $this->items_per_page) : 0;
613+
$total_pages = $this->items_per_page != 0 ? ceil($this->total_items / $this->items_per_page) : 0;
628614

629-
if ($this->total_pages <= 1) {
615+
if ($total_pages <= 1) {
630616
$this->pages = [];
631617

632618
return $this->pages;
633619
}
634620

635621
$pages = [];
636622
$this->pages = [];
637-
$this->prev_page = $this->page - 1;
638-
$this->next_page = $this->page + 1;
623+
$prev_page = $this->page - 1;
624+
$next_page = $this->page + 1;
639625

640-
$this->prev_page = $this->prev_page > 0 ? $this->prev_page : 0;
641-
$this->next_page = $this->next_page <= $this->total_pages ? $this->next_page : 0;
642-
$this->first_page = 1;
643-
$this->last_page = $this->total_pages;
626+
$prev_page = $prev_page > 0 ? $prev_page : 0;
627+
$next_page = $next_page <= $total_pages ? $next_page : 0;
628+
$first_page = 1;
629+
$last_page = $total_pages;
644630

645-
$this->start_offset = ($this->page - $this->pages_around_active) > 0 ? $this->page - $this->pages_around_active : $this->first_page;
646-
$this->end_offset = ($this->page + $this->pages_around_active) < $this->total_pages ? $this->page + $this->pages_around_active : $this->last_page;
631+
$start_offset = ($this->page - $this->pages_around_active) > 0 ? $this->page - $this->pages_around_active : $first_page;
632+
$end_offset = ($this->page + $this->pages_around_active) < $total_pages ? $this->page + $this->pages_around_active : $last_page;
647633

648-
if ((($this->pages_before_separator * 2) + $this->pages_before_separator) >= $this->last_page) {
634+
if ((($this->pages_before_separator * 2) + $this->pages_before_separator) >= $last_page) {
649635
$this->hide_separator = true;
650636
}
651637

652638
if (!$this->hide_previous) {
653-
if ($this->prev_page) {
654-
$this->pages[] = $this->pageArray($this->prev_page, $this->previous_text, 'prev');
639+
if ($prev_page) {
640+
$this->pages[] = $this->pageArray($prev_page, $this->previous_text, 'prev');
655641
}
656642
}
657643

658-
if ($this->start_offset >= $this->pages_before_separator) {
644+
if ($start_offset >= $this->pages_before_separator) {
659645
for ($i = 1; $i <= $this->pages_before_separator; $i++) {
660646
$this->pages[] = $this->pageArray($i, $this->pageText($i));
661647
$pages[] = $i;
662648
}
663649

664-
if (!$this->hide_separator) {
650+
if (!$this->hide_separator && $start_offset != $this->pages_before_separator && $start_offset != ($this->pages_before_separator + 1)) {
665651
$this->pages[] = $this->pageArray(null, $this->separator, 'separator');
666652
}
667653
}
668654

669-
for ($i = $this->start_offset; $i <= $this->end_offset; $i++) {
655+
for ($i = $start_offset; $i <= $end_offset; $i++) {
670656
if (!in_array($i, $pages)) {
671657
$this->pages[] = $this->pageArray($i, $this->pageText($i));
672658
}
673659
}
674660

675-
if ($this->end_offset <= ($this->last_page - $this->pages_before_separator)) {
661+
if ($end_offset <= ($last_page - $this->pages_before_separator)) {
676662
if (!$this->hide_separator) {
677663
$this->pages[] = $this->pageArray(null, $this->separator, 'separator');
678664
}
679665

680-
for ($i = ($this->last_page - ($this->pages_before_separator - 1)); $i <= $this->last_page; $i++) {
666+
for ($i = ($last_page - ($this->pages_before_separator - 1)); $i <= $last_page; $i++) {
681667
$this->pages[] = $this->pageArray($i, $this->pageText($i));
682668
}
683669
}
684670

685-
if ($i == $this->last_page) {
671+
if ($i == $last_page) {
686672
$this->pages[] = $this->pageArray($i, $this->pageText($i));
687673
}
688674

689675
if (!$this->hide_next) {
690-
if ($this->next_page) {
691-
$this->pages[] = $this->pageArray($this->next_page, $this->next_text, 'next');
676+
if ($next_page) {
677+
$this->pages[] = $this->pageArray($next_page, $this->next_text, 'next');
692678
}
693679
}
694680

@@ -766,9 +752,7 @@ public function get()
766752
$output .= '</nav>';
767753
$output .= "\r\n";
768754

769-
$this->pagination = $output;
770-
771-
return $this->pagination;
755+
return $output;
772756
}
773757

774758
/**

0 commit comments

Comments
 (0)