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