Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Form
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
6 / 6
28
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setInputSeparator
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getInputSeparator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFormString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 createForm
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
19
 __toString
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Paginator;
16
17/**
18 * Paginator form class
19 *
20 * @category   Pop
21 * @package    Pop\Paginator
22 * @author     Nick Sagona, III <dev@noladev.com>
23 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    4.1.0
26 */
27class Form extends AbstractPaginator
28{
29
30    /**
31     * Input separator
32     * @var string
33     */
34    protected string $inputSeparator = 'of';
35
36    /**
37     * Page form property
38     * @var ?string
39     */
40    protected ?string $form = null;
41
42    /**
43     * Constructor
44     *
45     * Instantiate the paginator object
46     *
47     * @param  int $total
48     * @param  int $perPage
49     */
50    public function __construct(int $total, int $perPage = 10)
51    {
52        parent::__construct($total, $perPage, 1);
53    }
54
55    /**
56     * Set input separator
57     *
58     * @param  string $separator
59     * @return Form
60     */
61    public function setInputSeparator(string $separator): Form
62    {
63        $this->inputSeparator = $separator;
64        return $this;
65    }
66
67    /**
68     * Get input separator
69     *
70     * @return string
71     */
72    public function getInputSeparator(): string
73    {
74        return $this->inputSeparator;
75    }
76
77    /**
78     * Get the page form string
79     *
80     * @param  ?int $page
81     * @return string
82     */
83    public function getFormString(?int $page = null): string
84    {
85        if ($page === null) {
86            $page = (isset($_GET[$this->queryKey]) && ((int)$_GET[$this->queryKey] > 0)) ? (int)$_GET[$this->queryKey] : 1;
87        }
88        $this->createForm($page);
89
90        return $this->form;
91    }
92
93    /**
94     * Create links
95     *
96     * @param  int  $page
97     * @return void
98     */
99    protected function createForm(int $page = 1): void
100    {
101        $this->currentPage = $page;
102
103        // Generate the page links.
104        $form = '';
105
106        // Preserve any passed GET parameters.
107        $query  = null;
108        $hidden = null;
109        $uri    = null;
110
111        if (isset($_SERVER['REQUEST_URI'])) {
112            $uri = (!empty($_SERVER['QUERY_STRING'])) ?
113                str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']) :
114                $_SERVER['REQUEST_URI'];
115            $uri = htmlspecialchars($uri, ENT_QUOTES);
116
117            if (count($_GET) > 0) {
118                $get = $_GET;
119                if (isset($get[$this->queryKey])) {
120                    unset($get[$this->queryKey]);
121                }
122                $query = '&' . http_build_query($get);
123                foreach ($get as $key => $value) {
124                    $key = htmlspecialchars((string)$key, ENT_QUOTES);
125                    if (is_array($value)) {
126                        foreach ($value as $k => $v) {
127                            $k = htmlspecialchars((string)$k, ENT_QUOTES);
128                            $v = htmlspecialchars((string)$v, ENT_QUOTES);
129                            $hidden .= '<input type="hidden" name="' . $key . '[' . $k . ']" value="' . $v . '" />';
130                        }
131                    } else {
132                        $value = htmlspecialchars((string)$value, ENT_QUOTES);
133                        $hidden .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
134                    }
135                }
136            }
137        }
138
139        // Calculate page range
140        $pageRange = $this->calculateRange($page);
141
142        $form .= '<form class="pop-paginator-form" action="' . $uri . (($query !== null) ? '?' . substr($query, 1)  : null) .
143            '" method="get"><div><input type="text" name="' . $this->queryKey . '" size="2" value="' .
144            $this->currentPage . '" /> ' . $this->inputSeparator . ' ' . $this->numberOfPages . '</div>';
145
146        if ($hidden !== null) {
147            $form .= '<div>' . $hidden . '</div>';
148        }
149
150        $form .= '</form>';
151
152        $startLinks  = null;
153        $endLinks    = null;
154
155        for ($i = $pageRange['start']; $i <= $pageRange['end']; $i++) {
156            if (($i == $pageRange['start']) && ($pageRange['prev'])) {
157                if ($this->bookends['start'] !== null) {
158                    $startLinks .= "<a href=\"" . $uri . "?" . $this->queryKey . "=1" . "{$query}\">" .
159                        $this->bookends['start'] . "</a>";
160                }
161                if ($this->bookends['previous'] !== null) {
162                    $startLinks .= "<a href=\"" . $uri . "?" . $this->queryKey . "=" . ($i - 1) . "{$query}\">" .
163                        $this->bookends['previous'] . "</a>";
164                }
165            }
166
167            if (($i == $pageRange['end']) && ($pageRange['next'])) {
168                if ($this->bookends['next'] !== null) {
169                    $endLinks .= "<a href=\"" . $uri . "?" . $this->queryKey . "=" . ($i + 1) . "{$query}\">" .
170                        $this->bookends['next'] . "</a>";
171                }
172                if ($this->bookends['end'] !== null) {
173                    $endLinks .= "<a href=\"" . $uri . "?" . $this->queryKey . "=" . $this->numberOfPages .
174                        "{$query}\">" . $this->bookends['end'] . "</a>";
175                }
176            }
177        }
178
179        $this->form = $startLinks . $form . $endLinks;
180    }
181
182    /**
183     * Output the rendered page links
184     *
185     * @return string
186     */
187    public function __toString(): string
188    {
189        if (empty($this->form)) {
190            $this->getFormString();
191        }
192        return $this->form;
193    }
194
195}