Completed
Push — master ( a931c7...ace7d8 )
by Dominik
04:09
created

src/Pagination/SlidingPagination.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Saxulum\PaginationProvider\Pagination;
4
5
use Knp\Component\Pager\Pagination\AbstractPagination;
6
7
class SlidingPagination extends AbstractPagination
8
{
9
    private $route;
10
    private $params;
11
    private $pageRange = 5;
12
    private $template;
13
    private $sortableTemplate;
14
    private $filtrationTemplate;
15
16
    public function __construct(array $params)
17
    {
18
        $this->params = $params;
19
    }
20
21
    public function setUsedRoute($route)
22
    {
23
        $this->route = $route;
24
    }
25
26
    public function getRoute()
27
    {
28
        return $this->route;
29
    }
30
31
    public function setSortableTemplate($template)
32
    {
33
        $this->sortableTemplate = $template;
34
    }
35
36
    public function getSortableTemplate()
37
    {
38
        return $this->sortableTemplate;
39
    }
40
41
    public function setFiltrationTemplate($template)
42
    {
43
        $this->filtrationTemplate = $template;
44
    }
45
46
    public function getFiltrationTemplate()
47
    {
48
        return $this->filtrationTemplate;
49
    }
50
51
    public function setParam($name, $value)
52
    {
53
        $this->params[$name] = $value;
54
    }
55
56
    public function getParams()
57
    {
58
        return $this->params;
59
    }
60
61
    public function setTemplate($template)
62
    {
63
        $this->template = $template;
64
    }
65
66
    public function getTemplate()
67
    {
68
        return $this->template;
69
    }
70
71
    public function setPageRange($range)
72
    {
73
        $this->pageRange = abs(intval($range));
0 ignored issues
show
Documentation Bug introduced by
It seems like abs(intval($range)) can also be of type double. However, the property $pageRange is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74
    }
75
76
    /**
77
     * Get url query with all parameters
78
     *
79
     * @param  array $additionalQueryParams
80
     * @return array - list of query parameters
81
     */
82
    public function getQuery(array $additionalQueryParams = array())
83
    {
84
        return array_merge($this->params, $additionalQueryParams);
85
    }
86
87
    public function isSorted($key, array $params = array())
88
    {
89
        $params = array_merge($this->params, $params);
90
91
        return isset($params[$this->getPaginatorOption('sortFieldParameterName')]) && $params[$this->getPaginatorOption('sortFieldParameterName')] === $key;
92
    }
93
94
    public function getDirection()
95
    {
96
        return $this->params[$this->getPaginatorOption('sortDirectionParameterName')];
97
    }
98
99
    public function getPaginationData()
100
    {
101
        $pageCount = intval(ceil($this->totalCount / $this->numItemsPerPage));
102
        $current = $this->currentPageNumber;
103
104
        if ($pageCount < $current) {
105
            $this->currentPageNumber = $current = $pageCount;
106
        }
107
108
        if ($this->pageRange > $pageCount) {
109
            $this->pageRange = $pageCount;
110
        }
111
112
        $delta = ceil($this->pageRange / 2);
113
114
        if ($current - $delta > $pageCount - $this->pageRange) {
115
            $pages = range($pageCount - $this->pageRange + 1, $pageCount);
116
        } else {
117
            if ($current - $delta < 0) {
118
                $delta = $current;
119
            }
120
121
            $offset = $current - $delta;
122
            $pages = range($offset + 1, $offset + $this->pageRange);
123
        }
124
125
        $proximity = floor($this->pageRange / 2);
126
127
        $startPage  = $current - $proximity;
128
        $endPage    = $current + $proximity;
129
130
        if ($startPage < 1) {
131
            $endPage = min($endPage + (1 - $startPage), $pageCount);
132
            $startPage = 1;
133
        }
134
135
        if ($endPage > $pageCount) {
136
            $startPage = max($startPage - ($endPage - $pageCount), 1);
137
            $endPage = $pageCount;
138
        }
139
140
        $viewData = array(
141
            'last'              => $pageCount,
142
            'current'           => $current,
143
            'numItemsPerPage'   => $this->numItemsPerPage,
144
            'first'             => 1,
145
            'pageCount'         => $pageCount,
146
            'totalCount'        => $this->totalCount,
147
            'pageRange'         => $this->pageRange,
148
            'startPage'         => $startPage,
149
            'endPage'           => $endPage
150
        );
151
152
        if ($current - 1 > 0) {
153
            $viewData['previous'] = $current - 1;
154
        }
155
156
        if ($current + 1 <= $pageCount) {
157
            $viewData['next'] = $current + 1;
158
        }
159
160
        $viewData['pagesInRange'] = $pages;
161
        $viewData['firstPageInRange'] = min($pages);
162
        $viewData['lastPageInRange']  = max($pages);
163
164
        if ($this->getItems() !== null) {
165
            $viewData['currentItemCount'] = $this->count();
166
            $viewData['firstItemNumber'] = (($current - 1) * $this->numItemsPerPage) + 1;
167
            $viewData['lastItemNumber'] = $viewData['firstItemNumber'] + $viewData['currentItemCount'] - 1;
168
        }
169
170
        return $viewData;
171
    }
172
173
    public function getPaginatorOptions()
174
    {
175
        return $this->paginatorOptions;
176
    }
177
178
    public function getCustomParameters()
179
    {
180
        return $this->customParameters;
181
    }
182
}
183