Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Pagination;
4
 
5
use Illuminate\Contracts\Pagination\LengthAwarePaginator as PaginatorContract;
6
 
7
class UrlWindow
8
{
9
    /**
10
     * The paginator implementation.
11
     *
12
     * @var \Illuminate\Contracts\Pagination\LengthAwarePaginator
13
     */
14
    protected $paginator;
15
 
16
    /**
17
     * Create a new URL window instance.
18
     *
19
     * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator  $paginator
20
     * @return void
21
     */
22
    public function __construct(PaginatorContract $paginator)
23
    {
24
        $this->paginator = $paginator;
25
    }
26
 
27
    /**
28
     * Create a new URL window instance.
29
     *
30
     * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator  $paginator
31
     * @return array
32
     */
33
    public static function make(PaginatorContract $paginator)
34
    {
35
        return (new static($paginator))->get();
36
    }
37
 
38
    /**
39
     * Get the window of URLs to be shown.
40
     *
41
     * @return array
42
     */
43
    public function get()
44
    {
45
        $onEachSide = $this->paginator->onEachSide;
46
 
47
        if ($this->paginator->lastPage() < ($onEachSide * 2) + 8) {
48
            return $this->getSmallSlider();
49
        }
50
 
51
        return $this->getUrlSlider($onEachSide);
52
    }
53
 
54
    /**
55
     * Get the slider of URLs there are not enough pages to slide.
56
     *
57
     * @return array
58
     */
59
    protected function getSmallSlider()
60
    {
61
        return [
62
            'first' => $this->paginator->getUrlRange(1, $this->lastPage()),
63
            'slider' => null,
64
            'last' => null,
65
        ];
66
    }
67
 
68
    /**
69
     * Create a URL slider links.
70
     *
71
     * @param  int  $onEachSide
72
     * @return array
73
     */
74
    protected function getUrlSlider($onEachSide)
75
    {
76
        $window = $onEachSide + 4;
77
 
78
        if (! $this->hasPages()) {
79
            return ['first' => null, 'slider' => null, 'last' => null];
80
        }
81
 
82
        // If the current page is very close to the beginning of the page range, we will
83
        // just render the beginning of the page range, followed by the last 2 of the
84
        // links in this list, since we will not have room to create a full slider.
85
        if ($this->currentPage() <= $window) {
86
            return $this->getSliderTooCloseToBeginning($window, $onEachSide);
87
        }
88
 
89
        // If the current page is close to the ending of the page range we will just get
90
        // this first couple pages, followed by a larger window of these ending pages
91
        // since we're too close to the end of the list to create a full on slider.
92
        elseif ($this->currentPage() > ($this->lastPage() - $window)) {
93
            return $this->getSliderTooCloseToEnding($window, $onEachSide);
94
        }
95
 
96
        // If we have enough room on both sides of the current page to build a slider we
97
        // will surround it with both the beginning and ending caps, with this window
98
        // of pages in the middle providing a Google style sliding paginator setup.
99
        return $this->getFullSlider($onEachSide);
100
    }
101
 
102
    /**
103
     * Get the slider of URLs when too close to the beginning of the window.
104
     *
105
     * @param  int  $window
106
     * @param  int  $onEachSide
107
     * @return array
108
     */
109
    protected function getSliderTooCloseToBeginning($window, $onEachSide)
110
    {
111
        return [
112
            'first' => $this->paginator->getUrlRange(1, $window + $onEachSide),
113
            'slider' => null,
114
            'last' => $this->getFinish(),
115
        ];
116
    }
117
 
118
    /**
119
     * Get the slider of URLs when too close to the ending of the window.
120
     *
121
     * @param  int  $window
122
     * @param  int  $onEachSide
123
     * @return array
124
     */
125
    protected function getSliderTooCloseToEnding($window, $onEachSide)
126
    {
127
        $last = $this->paginator->getUrlRange(
128
            $this->lastPage() - ($window + ($onEachSide - 1)),
129
            $this->lastPage()
130
        );
131
 
132
        return [
133
            'first' => $this->getStart(),
134
            'slider' => null,
135
            'last' => $last,
136
        ];
137
    }
138
 
139
    /**
140
     * Get the slider of URLs when a full slider can be made.
141
     *
142
     * @param  int  $onEachSide
143
     * @return array
144
     */
145
    protected function getFullSlider($onEachSide)
146
    {
147
        return [
148
            'first' => $this->getStart(),
149
            'slider' => $this->getAdjacentUrlRange($onEachSide),
150
            'last' => $this->getFinish(),
151
        ];
152
    }
153
 
154
    /**
155
     * Get the page range for the current page window.
156
     *
157
     * @param  int  $onEachSide
158
     * @return array
159
     */
160
    public function getAdjacentUrlRange($onEachSide)
161
    {
162
        return $this->paginator->getUrlRange(
163
            $this->currentPage() - $onEachSide,
164
            $this->currentPage() + $onEachSide
165
        );
166
    }
167
 
168
    /**
169
     * Get the starting URLs of a pagination slider.
170
     *
171
     * @return array
172
     */
173
    public function getStart()
174
    {
175
        return $this->paginator->getUrlRange(1, 2);
176
    }
177
 
178
    /**
179
     * Get the ending URLs of a pagination slider.
180
     *
181
     * @return array
182
     */
183
    public function getFinish()
184
    {
185
        return $this->paginator->getUrlRange(
186
            $this->lastPage() - 1,
187
            $this->lastPage()
188
        );
189
    }
190
 
191
    /**
192
     * Determine if the underlying paginator being presented has pages to show.
193
     *
194
     * @return bool
195
     */
196
    public function hasPages()
197
    {
198
        return $this->paginator->lastPage() > 1;
199
    }
200
 
201
    /**
202
     * Get the current page from the paginator.
203
     *
204
     * @return int
205
     */
206
    protected function currentPage()
207
    {
208
        return $this->paginator->currentPage();
209
    }
210
 
211
    /**
212
     * Get the last page from the paginator.
213
     *
214
     * @return int
215
     */
216
    protected function lastPage()
217
    {
218
        return $this->paginator->lastPage();
219
    }
220
}