Pagination   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A getHtmlCode() 0 4 1
F generateHtmlPaging() 0 43 21
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @title            Pagination Class
4
 *
5
 * @author           Pierre-Henry Soria <[email protected]>
6
 * @copyright        (c) 2012-2019, Pierre-Henry Soria. All Rights Reserved.
7
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
8
 * @package          PH7 / Framework / Page
9
 */
10
11
namespace PH7\Framework\Navigation;
12
13
defined('PH7') or exit('Restricted access');
14
15
class Pagination
16
{
17
    const MAX_PAGES = 4;
18
19
    /** @var string */
20
    private $sPageName;
21
22
    /** @var int */
23
    private $iTotalPages;
24
25
    /** @var int */
26
    private $iCurrentPage;
27
28
    /** @var int */
29
    private $iShowItems;
30
31
    /** @var string */
32
    private $sHtmlOutput;
33
34
    /** @var array */
35
    private static $aOptions = [
36
        'range' => self::MAX_PAGES - 1, // Number of pages to display on the pagination
37
        'text_first_page' => '&laquo;', // Button text "First Page"
38
        'text_last_page' => '&raquo;', // Button text "Last Page"
39
        'text_next_page' => '&rsaquo;', //  Button text "Next"
40
        'text_previous_page' => '&lsaquo;' // Button text "Previous"
41
    ];
42
43
    /**
44
     * @param int $iTotalPages
45
     * @param int $iCurrentPage
46
     * @param string $sPageName Default 'p'
47
     * @param array $aOptions Optional options.
48
     */
49
    public function __construct($iTotalPages, $iCurrentPage, $sPageName = 'p', array $aOptions = [])
50
    {
51
        // Set the total number of page
52
        $this->iTotalPages = $iTotalPages;
53
54
        // Retrieve the number of the current page
55
        $this->iCurrentPage = $iCurrentPage;
56
57
        // Put options update
58
        self::$aOptions += $aOptions;
59
60
        // It retrieves the address of the page
61
        $this->sPageName = Page::cleanDynamicUrl($sPageName);
62
63
64
        // Management pages to see
65
        $this->iShowItems = (self::$aOptions['range'] * 2) + 1;
0 ignored issues
show
Documentation Bug introduced by
The property $iShowItems was declared of type integer, but self::$aOptions['range'] * 2 + 1 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
66
67
        $this->generateHtmlPaging();
68
    }
69
70
    /**
71
     * Display the pagination if there is more than one page
72
     *
73
     * @return string Html code.
74
     */
75
    public function getHtmlCode()
76
    {
77
        return $this->sHtmlOutput;
78
    }
79
80
    /**
81
     * Generate the HTML pagination code.
82
     *
83
     * @return void
84
     */
85
    private function generateHtmlPaging()
86
    {
87
        // If you have more than one page, it displays the navigation
88
        if ($this->iTotalPages > 1) {
89
            $this->sHtmlOutput = '<div class="clear"></div><nav class="center" role="navigation"><ul class="pagination">';
90
91
            // Management link to go to the first page
92
            if (self::$aOptions['text_first_page']) {
93
                if ($this->iCurrentPage > 2 && $this->iCurrentPage > self::$aOptions['range'] + 1 && $this->iShowItems < $this->iTotalPages) {
94
                    $this->sHtmlOutput .= '<li><a href="' . $this->sPageName . '1"><span aria-hidden="true">' . self::$aOptions['text_first_page'] . '</span></a></li>';
95
                }
96
            }
97
98
            // Management the Previous link
99
            if (self::$aOptions['text_previous_page']) {
100
                if ($this->iCurrentPage > 2 && $this->iShowItems < $this->iTotalPages) {
101
                    $this->sHtmlOutput .= '<li class="previous"><a href="' . $this->sPageName . ($this->iCurrentPage - 1) . '" aria-label="Previous"><span aria-hidden="true">' . self::$aOptions['text_previous_page'] . '</span></a></li>';
102
                }
103
            }
104
            // Management of other paging buttons...
105
            for ($iCurrentPage = 1; $iCurrentPage <= $this->iTotalPages; $iCurrentPage++) {
106
                if (($iCurrentPage >= $this->iCurrentPage - self::$aOptions['range'] && $iCurrentPage <= $this->iCurrentPage + self::$aOptions['range']) || $this->iTotalPages <= $this->iShowItems) {
107
                    $this->sHtmlOutput .= '<li' . ($this->iCurrentPage === $iCurrentPage ? ' class="active"' : '') . '><a href="' . $this->sPageName . $iCurrentPage . '">' . $iCurrentPage . '</a></li>';
108
                }
109
            }
110
111
            //  Management the "Next" link
112
            if (self::$aOptions['text_next_page']) {
113
                if ($this->iCurrentPage < $this->iTotalPages - 1 && $this->iShowItems < $this->iTotalPages) {
114
                    $this->sHtmlOutput .= '<li class="next"><a href="' . $this->sPageName . ($this->iCurrentPage + 1) . '" aria-label="Next"><span aria-hidden="true">' . self::$aOptions['text_next_page'] . '</span></a></li>';
115
                }
116
            }
117
118
            // Management link to go to the last page
119
            if (self::$aOptions['text_last_page']) {
120
                if ($this->iCurrentPage < $this->iTotalPages - 1 && $this->iCurrentPage + self::$aOptions['range'] < $this->iTotalPages && $this->iShowItems < $this->iTotalPages) {
121
                    $this->sHtmlOutput .= '<li><a href="' . $this->sPageName . $this->iTotalPages . '"><span aria-hidden="true">' . self::$aOptions['text_last_page'] . '</span></a></li>';
122
                }
123
            }
124
125
            $this->sHtmlOutput .= '</ul></nav>';
126
        }
127
    }
128
}
129