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 ArrayAccess;
6
use Countable;
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Illuminate\Support\Collection;
11
use IteratorAggregate;
12
use JsonSerializable;
13
 
14
class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, LengthAwarePaginatorContract
15
{
16
    /**
17
     * The total number of items before slicing.
18
     *
19
     * @var int
20
     */
21
    protected $total;
22
 
23
    /**
24
     * The last available page.
25
     *
26
     * @var int
27
     */
28
    protected $lastPage;
29
 
30
    /**
31
     * Create a new paginator instance.
32
     *
33
     * @param  mixed  $items
34
     * @param  int  $total
35
     * @param  int  $perPage
36
     * @param  int|null  $currentPage
37
     * @param  array  $options  (path, query, fragment, pageName)
38
     * @return void
39
     */
40
    public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
41
    {
42
        $this->options = $options;
43
 
44
        foreach ($options as $key => $value) {
45
            $this->{$key} = $value;
46
        }
47
 
48
        $this->total = $total;
49
        $this->perPage = (int) $perPage;
50
        $this->lastPage = max((int) ceil($total / $perPage), 1);
51
        $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
52
        $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
53
        $this->items = $items instanceof Collection ? $items : Collection::make($items);
54
    }
55
 
56
    /**
57
     * Get the current page for the request.
58
     *
59
     * @param  int  $currentPage
60
     * @param  string  $pageName
61
     * @return int
62
     */
63
    protected function setCurrentPage($currentPage, $pageName)
64
    {
65
        $currentPage = $currentPage ?: static::resolveCurrentPage($pageName);
66
 
67
        return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
68
    }
69
 
70
    /**
71
     * Render the paginator using the given view.
72
     *
73
     * @param  string|null  $view
74
     * @param  array  $data
75
     * @return \Illuminate\Contracts\Support\Htmlable
76
     */
77
    public function links($view = null, $data = [])
78
    {
79
        return $this->render($view, $data);
80
    }
81
 
82
    /**
83
     * Render the paginator using the given view.
84
     *
85
     * @param  string|null  $view
86
     * @param  array  $data
87
     * @return \Illuminate\Contracts\Support\Htmlable
88
     */
89
    public function render($view = null, $data = [])
90
    {
91
        return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
92
            'paginator' => $this,
93
            'elements' => $this->elements(),
94
        ]));
95
    }
96
 
97
    /**
98
     * Get the paginator links as a collection (for JSON responses).
99
     *
100
     * @return \Illuminate\Support\Collection
101
     */
102
    public function linkCollection()
103
    {
104
        return collect($this->elements())->flatMap(function ($item) {
105
            if (! is_array($item)) {
106
                return [['url' => null, 'label' => '...', 'active' => false]];
107
            }
108
 
109
            return collect($item)->map(function ($url, $page) {
110
                return [
111
                    'url' => $url,
112
                    'label' => (string) $page,
113
                    'active' => $this->currentPage() === $page,
114
                ];
115
            });
116
        })->prepend([
117
            'url' => $this->previousPageUrl(),
118
            'label' => function_exists('__') ? __('pagination.previous') : 'Previous',
119
            'active' => false,
120
        ])->push([
121
            'url' => $this->nextPageUrl(),
122
            'label' => function_exists('__') ? __('pagination.next') : 'Next',
123
            'active' => false,
124
        ]);
125
    }
126
 
127
    /**
128
     * Get the array of elements to pass to the view.
129
     *
130
     * @return array
131
     */
132
    protected function elements()
133
    {
134
        $window = UrlWindow::make($this);
135
 
136
        return array_filter([
137
            $window['first'],
138
            is_array($window['slider']) ? '...' : null,
139
            $window['slider'],
140
            is_array($window['last']) ? '...' : null,
141
            $window['last'],
142
        ]);
143
    }
144
 
145
    /**
146
     * Get the total number of items being paginated.
147
     *
148
     * @return int
149
     */
150
    public function total()
151
    {
152
        return $this->total;
153
    }
154
 
155
    /**
156
     * Determine if there are more items in the data source.
157
     *
158
     * @return bool
159
     */
160
    public function hasMorePages()
161
    {
162
        return $this->currentPage() < $this->lastPage();
163
    }
164
 
165
    /**
166
     * Get the URL for the next page.
167
     *
168
     * @return string|null
169
     */
170
    public function nextPageUrl()
171
    {
172
        if ($this->hasMorePages()) {
173
            return $this->url($this->currentPage() + 1);
174
        }
175
    }
176
 
177
    /**
178
     * Get the last page.
179
     *
180
     * @return int
181
     */
182
    public function lastPage()
183
    {
184
        return $this->lastPage;
185
    }
186
 
187
    /**
188
     * Get the instance as an array.
189
     *
190
     * @return array
191
     */
192
    public function toArray()
193
    {
194
        return [
195
            'current_page' => $this->currentPage(),
196
            'data' => $this->items->toArray(),
197
            'first_page_url' => $this->url(1),
198
            'from' => $this->firstItem(),
199
            'last_page' => $this->lastPage(),
200
            'last_page_url' => $this->url($this->lastPage()),
201
            'links' => $this->linkCollection()->toArray(),
202
            'next_page_url' => $this->nextPageUrl(),
203
            'path' => $this->path(),
204
            'per_page' => $this->perPage(),
205
            'prev_page_url' => $this->previousPageUrl(),
206
            'to' => $this->lastItem(),
207
            'total' => $this->total(),
208
        ];
209
    }
210
 
211
    /**
212
     * Convert the object into something JSON serializable.
213
     *
214
     * @return array
215
     */
216
    public function jsonSerialize(): array
217
    {
218
        return $this->toArray();
219
    }
220
 
221
    /**
222
     * Convert the object to its JSON representation.
223
     *
224
     * @param  int  $options
225
     * @return string
226
     */
227
    public function toJson($options = 0)
228
    {
229
        return json_encode($this->jsonSerialize(), $options);
230
    }
231
}