Projekt

Obecné

Profil

Stáhnout (3.43 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
namespace App\Utils;
4

    
5

    
6
class Paginator
7
{
8

    
9
    private $limit;
10
    private $page;
11

    
12
    private $pageCount;
13
    private $itemCount;
14

    
15
    private $startingPage;
16
    private $endingPage;
17

    
18
    const ALLOWED_LIMITS = [10, 25, 50, 100];
19

    
20
    /**
21
     * Implementation of spring Data Pageable to paginate and sort JPA queries
22
     * @param $page int query offset
23
     * @param $limit int query limit
24
     */
25
    public function __construct($page, $limit)
26
    {
27
        if ($page < 1)
28
        {
29
            $page = 1;
30
        }
31
        $this->page = $page;
32

    
33
        foreach (self::ALLOWED_LIMITS as $l)
34
        {
35
            if ($l == $limit)
36
            {
37
                $this->limit = $limit;
38
            }
39
        }
40
        if ($this->limit == 0)
41
        {
42
            $this->limit = self::ALLOWED_LIMITS[0];
43
        }
44
    }
45

    
46
    /**
47
     * @return int
48
     */
49
    public function getPageNumber()
50
    {
51
        return $this->page;
52
    }
53

    
54
    /**
55
     * @return int
56
     */
57
    public function getPageSize()
58
    {
59
        return $this->limit;
60
    }
61

    
62
    public function getOffset()
63
    {
64
        return ($this->page - 1) * $this->limit;
65
    }
66

    
67
    /**
68
     * @return Paginator
69
     */
70
    public function next()
71
    {
72
        return $this->hasNext() ? new Paginator($this->getPageNumber() + 1, $this->getPageSize()) : $this;
73
    }
74

    
75
    /**
76
     * @return Paginator
77
     */
78
    public function previous()
79
    {
80
        return $this->hasPrevious() ? new Paginator($this->getPageNumber() - 1, $this->getPageSize()) : $this;
81
    }
82

    
83
    public function previousOrFirst()
84
    {
85
        return $this->hasPrevious() ? $this->previous() : $this->first();
86
    }
87

    
88
    public function first()
89
    {
90
        return new Paginator(1, $this->getPageSize());
91
    }
92

    
93
    /**
94
     * @return bool
95
     */
96
    public function hasPrevious()
97
    {
98
        return $this->getOffset() >= $this->limit;
99
    }
100

    
101
    /**
102
     * @return bool
103
     */
104
    public function hasNext()
105
    {
106
        return $this->getPageNumber() < $this->getPageCount();
107
    }
108

    
109
    /**
110
     * @return Paginator
111
     */
112
    public function last()
113
    {
114
        return new Paginator($this->getPageCount(), $this->getPageSize());
115
    }
116

    
117
    public function getPageCount()
118
    {
119
        return $this->pageCount;
120
    }
121

    
122
    public function getItemCount()
123
    {
124
        return $this->itemCount;
125
    }
126

    
127
    /**
128
     * Sets maximum possible page to be displayed and total number of items
129
     * @param $itemCount int total count
130
     */
131
    public function setPageCount($itemCount)
132
    {
133
        $this->itemCount = $itemCount;
134
        $this->pageCount = ceil($itemCount / $this->getPageSize());
135
        $this->setPagesList();
136
    }
137

    
138
    public function getAllowedLimits()
139
    {
140
        return self::ALLOWED_LIMITS;
141
    }
142

    
143
    /**
144
     * Sets starting and ending page number to be displayed in template
145
     */
146
    private function setPagesList()
147
    {
148
        $offset = 2;
149

    
150
        if ($this->getPageNumber() + $offset >= $this->getPageCount())
151
        {
152
            $this->startingPage = max(1, $this->getPageCount() - 2 * $offset);
153
            $this->endingPage = $this->getPageCount();
154
        } else
155
        {
156
            $this->startingPage = max(1, $this->getPageNumber() - $offset);
157
            $this->endingPage = min($this->startingPage + 2 * $offset, $this->getPageCount());
158
        }
159
    }
160

    
161
    public function getStartingPage()
162
    {
163
        return $this->startingPage;
164
    }
165

    
166
    public function getEndingPage()
167
    {
168
        return $this->endingPage;
169
    }
170
}
(2-2/2)