Completed
Branch master (52775d)
by Pierre-Henry
36:45
created

Page::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 19 and the first side effect is on line 15.

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            Page Class
4
 * @desc             Various Page methods with also the pagination methods.
5
 *
6
 * @author           Pierre-Henry Soria <[email protected]>
7
 * @copyright        (c) 2012-2017, 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;
18
19
class Page
20
{
21
    const DEFAULT_NUMBER_ITEMS = 10;
22
23
    private $_oHttpRequest, $_iTotalPages, $_iTotalItems, $_iNbItemsPerPage, $_iCurrentPage, $_iFirstItem;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
24
25
    public function __construct()
26
    {
27
        $this->_oHttpRequest = new Http;
28
    }
29
30
    /**
31
     * @param integer $iTotalItems
32
     * @param integer $iNbItemsPerPage
33
     * @return void
34
     */
35
    protected function totalPages($iTotalItems, $iNbItemsPerPage)
36
    {
37
        $this->_iTotalItems = (int) $iTotalItems;
38
        $this->_iNbItemsPerPage = (int) $iNbItemsPerPage; // or intval() function, but it is slower than the cast
39
        $this->_iCurrentPage = (int) ($this->_oHttpRequest->getExists('p')) ? $this->_oHttpRequest->get('p') : 1;
40
41
        // Ternary condition to prevent division by zero
42
        $this->_iTotalPages = (int) ($this->_iTotalItems !== 0 && $this->_iNbItemsPerPage !== 0) ? ceil($this->_iTotalItems / $this->_iNbItemsPerPage) : 0;
43
44
        $this->_iFirstItem = (int) ($this->_iCurrentPage-1) * $this->_iNbItemsPerPage;
45
    }
46
47
    /**
48
     * @param integer $iTotalItems
49
     * @param integer $iNbItemsPerPage Default 10
50
     * @return integer The number of pages.
51
     */
52
    public function getTotalPages($iTotalItems, $iNbItemsPerPage = self::DEFAULT_NUMBER_ITEMS)
53
    {
54
        $this->totalPages($iTotalItems, $iNbItemsPerPage);
55
        return ($this->_iTotalPages < 1) ? 1 : $this->_iTotalPages;
56
    }
57
58
    public function getTotalItems()
59
    {
60
        return $this->_iTotalItems;
61
    }
62
63
    public function getFirstItem()
64
    {
65
        return ($this->_iFirstItem < 0) ? 0 : $this->_iFirstItem;
66
    }
67
68
    public function getNbItemsPerPage()
69
    {
70
        return $this->_iNbItemsPerPage;
71
    }
72
73
    public function getCurrentPage()
74
    {
75
        return $this->_iCurrentPage;
76
    }
77
78
    /**
79
     * Clean a Dynamic URL for some features CMS.
80
     *
81
     * @param string $sVar The Query URL (e.g. www.pierre-henry-soria.com/my-mod/?query=value).
82
     * @return string $sPageUrl The new clean URL.
83
     */
84
    public static function cleanDynamicUrl($sVar)
85
    {
86
        $sCurrentUrl = (new Http)->currentUrl();
87
        $sUrl = preg_replace('#\?.+$#', '', $sCurrentUrl);
88
89
        if (preg_match('#\?(.+[^\./])=(.+[^\./])$#', $sCurrentUrl))
90
        {
91
            $sUrlSlug = (strpos($sCurrentUrl, '&amp;') !== false) ? strrchr($sCurrentUrl, '?') : strrchr($sCurrentUrl, '?');
92
            $sPageUrl = $sUrl . $sUrlSlug . '&amp;' . $sVar . '=';
93
        }
94
        else
95
        {
96
            $sPageUrl = $sUrl . static::trailingSlash($sUrl) . '?' . $sVar . '=';
97
        }
98
99
        return $sPageUrl;
100
    }
101
102
    /**
103
     * Returns a trailing slash if needed.
104
     *
105
     * @param  string $sUrl
106
     * @return string
107
     */
108
    protected static function trailingSlash($sUrl)
109
    {
110
        return (substr($sUrl, -1) !== PH7_SH && !strstr($sUrl, PH7_PAGE_EXT)) ? PH7_SH : '';
111
    }
112
}
113