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\Paginator as PaginatorContract;
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 Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
15
{
16
    /**
17
     * Determine if there are more items in the data source.
18
     *
19
     * @return bool
20
     */
21
    protected $hasMore;
22
 
23
    /**
24
     * Create a new paginator instance.
25
     *
26
     * @param  mixed  $items
27
     * @param  int  $perPage
28
     * @param  int|null  $currentPage
29
     * @param  array  $options  (path, query, fragment, pageName)
30
     * @return void
31
     */
32
    public function __construct($items, $perPage, $currentPage = null, array $options = [])
33
    {
34
        $this->options = $options;
35
 
36
        foreach ($options as $key => $value) {
37
            $this->{$key} = $value;
38
        }
39
 
40
        $this->perPage = $perPage;
41
        $this->currentPage = $this->setCurrentPage($currentPage);
42
        $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
43
 
44
        $this->setItems($items);
45
    }
46
 
47
    /**
48
     * Get the current page for the request.
49
     *
50
     * @param  int  $currentPage
51
     * @return int
52
     */
53
    protected function setCurrentPage($currentPage)
54
    {
55
        $currentPage = $currentPage ?: static::resolveCurrentPage();
56
 
57
        return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
58
    }
59
 
60
    /**
61
     * Set the items for the paginator.
62
     *
63
     * @param  mixed  $items
64
     * @return void
65
     */
66
    protected function setItems($items)
67
    {
68
        $this->items = $items instanceof Collection ? $items : Collection::make($items);
69
 
70
        $this->hasMore = $this->items->count() > $this->perPage;
71
 
72
        $this->items = $this->items->slice(0, $this->perPage);
73
    }
74
 
75
    /**
76
     * Get the URL for the next page.
77
     *
78
     * @return string|null
79
     */
80
    public function nextPageUrl()
81
    {
82
        if ($this->hasMorePages()) {
83
            return $this->url($this->currentPage() + 1);
84
        }
85
    }
86
 
87
    /**
88
     * Render the paginator using the given view.
89
     *
90
     * @param  string|null  $view
91
     * @param  array  $data
92
     * @return string
93
     */
94
    public function links($view = null, $data = [])
95
    {
96
        return $this->render($view, $data);
97
    }
98
 
99
    /**
100
     * Render the paginator using the given view.
101
     *
102
     * @param  string|null  $view
103
     * @param  array  $data
104
     * @return \Illuminate\Contracts\Support\Htmlable
105
     */
106
    public function render($view = null, $data = [])
107
    {
108
        return static::viewFactory()->make($view ?: static::$defaultSimpleView, array_merge($data, [
109
            'paginator' => $this,
110
        ]));
111
    }
112
 
113
    /**
114
     * Manually indicate that the paginator does have more pages.
115
     *
116
     * @param  bool  $hasMore
117
     * @return $this
118
     */
119
    public function hasMorePagesWhen($hasMore = true)
120
    {
121
        $this->hasMore = $hasMore;
122
 
123
        return $this;
124
    }
125
 
126
    /**
127
     * Determine if there are more items in the data source.
128
     *
129
     * @return bool
130
     */
131
    public function hasMorePages()
132
    {
133
        return $this->hasMore;
134
    }
135
 
136
    /**
137
     * Get the instance as an array.
138
     *
139
     * @return array
140
     */
141
    public function toArray()
142
    {
143
        return [
144
            'current_page' => $this->currentPage(),
145
            'data' => $this->items->toArray(),
146
            'first_page_url' => $this->url(1),
147
            'from' => $this->firstItem(),
148
            'next_page_url' => $this->nextPageUrl(),
149
            'path' => $this->path(),
150
            'per_page' => $this->perPage(),
151
            'prev_page_url' => $this->previousPageUrl(),
152
            'to' => $this->lastItem(),
153
        ];
154
    }
155
 
156
    /**
157
     * Convert the object into something JSON serializable.
158
     *
159
     * @return array
160
     */
161
    public function jsonSerialize(): array
162
    {
163
        return $this->toArray();
164
    }
165
 
166
    /**
167
     * Convert the object to its JSON representation.
168
     *
169
     * @param  int  $options
170
     * @return string
171
     */
172
    public function toJson($options = 0)
173
    {
174
        return json_encode($this->jsonSerialize(), $options);
175
    }
176
}