These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @title Page Class |
||
4 | * @desc Various Page methods with also the pagination methods. |
||
5 | * |
||
6 | * @author Pierre-Henry Soria <[email protected]> |
||
7 | * @copyright (c) 2012-2018, Pierre-Henry Soria. All Rights Reserved. |
||
8 | * @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
||
9 | * @package PH7 / Framework / Navigation |
||
10 | * @version 1.2 |
||
11 | */ |
||
12 | |||
13 | namespace PH7\Framework\Navigation; |
||
14 | |||
15 | defined('PH7') or exit('Restricted access'); |
||
16 | |||
17 | use PH7\Framework\Mvc\Request\Http as HttpRequest; |
||
18 | |||
19 | class Page |
||
20 | { |
||
21 | const DEFAULT_NUMBER_ITEMS = 10; |
||
22 | |||
23 | /** @var HttpRequest */ |
||
24 | private $oHttpRequest; |
||
25 | |||
26 | /** @var int */ |
||
27 | private $iTotalPages; |
||
28 | |||
29 | /** @var int */ |
||
30 | private $iTotalItems; |
||
31 | |||
32 | /** @var int */ |
||
33 | private $iNbItemsPerPage; |
||
34 | |||
35 | /** @var int */ |
||
36 | private $iCurrentPage; |
||
37 | |||
38 | /** @var int */ |
||
39 | private $iFirstItem; |
||
40 | |||
41 | public function __construct() |
||
42 | { |
||
43 | $this->oHttpRequest = new HttpRequest; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param int $iTotalItems |
||
48 | * @param int $iNbItemsPerPage |
||
49 | * |
||
50 | * @return void |
||
51 | */ |
||
52 | protected function totalPages($iTotalItems, $iNbItemsPerPage) |
||
53 | { |
||
54 | $this->iTotalItems = (int)$iTotalItems; |
||
55 | $this->iNbItemsPerPage = (int)$iNbItemsPerPage; // or intval() function, but it is slower than casting |
||
56 | $this->iCurrentPage = (int)$this->oHttpRequest->getExists('p') ? $this->oHttpRequest->get('p') : 1; |
||
57 | |||
58 | // Ternary condition to prevent division by zero |
||
59 | $this->iTotalPages = (int)($this->iTotalItems !== 0 && $this->iNbItemsPerPage !== 0) ? ceil($this->iTotalItems / $this->iNbItemsPerPage) : 0; |
||
60 | |||
61 | $this->iFirstItem = (int)($this->iCurrentPage - 1) * $this->iNbItemsPerPage; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param int $iTotalItems |
||
66 | * @param int $iNbItemsPerPage Default 10 |
||
67 | * |
||
68 | * @return int The number of pages. |
||
69 | */ |
||
70 | public function getTotalPages($iTotalItems, $iNbItemsPerPage = self::DEFAULT_NUMBER_ITEMS) |
||
71 | { |
||
72 | $this->totalPages($iTotalItems, $iNbItemsPerPage); |
||
73 | return ($this->iTotalPages < 1) ? 1 : $this->iTotalPages; |
||
74 | } |
||
75 | |||
76 | public function getTotalItems() |
||
77 | { |
||
78 | return $this->iTotalItems; |
||
79 | } |
||
80 | |||
81 | public function getFirstItem() |
||
82 | { |
||
83 | return $this->iFirstItem < 0 ? 0 : $this->iFirstItem; |
||
84 | } |
||
85 | |||
86 | public function getNbItemsPerPage() |
||
87 | { |
||
88 | return $this->iNbItemsPerPage; |
||
89 | } |
||
90 | |||
91 | public function getCurrentPage() |
||
92 | { |
||
93 | return $this->iCurrentPage; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Clean a Dynamic URL for some features CMS. |
||
98 | * |
||
99 | * @param string $sVar The Query URL (e.g. www.pierre-henry-soria.com/my-mod/?query=value). |
||
100 | * |
||
101 | * @return string $sPageUrl The new clean URL. |
||
102 | */ |
||
103 | public static function cleanDynamicUrl($sVar) |
||
104 | { |
||
105 | $sCurrentUrl = (new HttpRequest)->currentUrl(); |
||
106 | $sUrl = preg_replace('#\?.+$#', '', $sCurrentUrl); |
||
107 | |||
108 | if (preg_match('#\?(.+[^\./])=(.+[^\./])$#', $sCurrentUrl)) { |
||
109 | $sPageUrl = $sUrl . self::getUrlSlug($sCurrentUrl) . '&' . $sVar . '='; |
||
110 | } else { |
||
111 | $sPageUrl = $sUrl . static::trailingSlash($sUrl) . '?' . $sVar . '='; |
||
0 ignored issues
–
show
|
|||
112 | } |
||
113 | |||
114 | return $sPageUrl; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Returns a trailing slash if needed. |
||
119 | * |
||
120 | * @param string $sUrl |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | private static function trailingSlash($sUrl) |
||
125 | { |
||
126 | return (substr($sUrl, -1) !== PH7_SH && !strstr($sUrl, PH7_PAGE_EXT)) ? PH7_SH : ''; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $sCurrentUrl |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | private static function getUrlSlug($sCurrentUrl) |
||
135 | { |
||
136 | return strpos($sCurrentUrl, '&') !== false ? strrchr($sCurrentUrl, '?') : strrchr($sCurrentUrl, '?'); |
||
137 | } |
||
138 | } |
||
139 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: