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\CursorPaginator 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 CursorPaginator extends AbstractCursorPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
15
{
16
    /**
17
     * Indicates whether 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  \Illuminate\Pagination\Cursor|null  $cursor
29
     * @param  array  $options  (path, query, fragment, pageName)
30
     * @return void
31
     */
32
    public function __construct($items, $perPage, $cursor = null, array $options = [])
33
    {
34
        $this->options = $options;
35
 
36
        foreach ($options as $key => $value) {
37
            $this->{$key} = $value;
38
        }
39
 
40
        $this->perPage = (int) $perPage;
41
        $this->cursor = $cursor;
42
        $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
43
 
44
        $this->setItems($items);
45
    }
46
 
47
    /**
48
     * Set the items for the paginator.
49
     *
50
     * @param  mixed  $items
51
     * @return void
52
     */
53
    protected function setItems($items)
54
    {
55
        $this->items = $items instanceof Collection ? $items : Collection::make($items);
56
 
57
        $this->hasMore = $this->items->count() > $this->perPage;
58
 
59
        $this->items = $this->items->slice(0, $this->perPage);
60
 
61
        if (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()) {
62
            $this->items = $this->items->reverse()->values();
63
        }
64
    }
65
 
66
    /**
67
     * Render the paginator using the given view.
68
     *
69
     * @param  string|null  $view
70
     * @param  array  $data
71
     * @return \Illuminate\Contracts\Support\Htmlable
72
     */
73
    public function links($view = null, $data = [])
74
    {
75
        return $this->render($view, $data);
76
    }
77
 
78
    /**
79
     * Render the paginator using the given view.
80
     *
81
     * @param  string|null  $view
82
     * @param  array  $data
83
     * @return \Illuminate\Contracts\Support\Htmlable
84
     */
85
    public function render($view = null, $data = [])
86
    {
87
        return static::viewFactory()->make($view ?: Paginator::$defaultSimpleView, array_merge($data, [
88
            'paginator' => $this,
89
        ]));
90
    }
91
 
92
    /**
93
     * Determine if there are more items in the data source.
94
     *
95
     * @return bool
96
     */
97
    public function hasMorePages()
98
    {
99
        return (is_null($this->cursor) && $this->hasMore) ||
100
            (! is_null($this->cursor) && $this->cursor->pointsToNextItems() && $this->hasMore) ||
101
            (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems());
102
    }
103
 
104
    /**
105
     * Determine if there are enough items to split into multiple pages.
106
     *
107
     * @return bool
108
     */
109
    public function hasPages()
110
    {
111
        return ! $this->onFirstPage() || $this->hasMorePages();
112
    }
113
 
114
    /**
115
     * Determine if the paginator is on the first page.
116
     *
117
     * @return bool
118
     */
119
    public function onFirstPage()
120
    {
121
        return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore);
122
    }
123
 
124
    /**
125
     * Determine if the paginator is on the last page.
126
     *
127
     * @return bool
128
     */
129
    public function onLastPage()
130
    {
131
        return ! $this->hasMorePages();
132
    }
133
 
134
    /**
135
     * Get the instance as an array.
136
     *
137
     * @return array
138
     */
139
    public function toArray()
140
    {
141
        return [
142
            'data' => $this->items->toArray(),
143
            'path' => $this->path(),
144
            'per_page' => $this->perPage(),
145
            'next_cursor' => $this->nextCursor()?->encode(),
146
            'next_page_url' => $this->nextPageUrl(),
147
            'prev_cursor' => $this->previousCursor()?->encode(),
148
            'prev_page_url' => $this->previousPageUrl(),
149
        ];
150
    }
151
 
152
    /**
153
     * Convert the object into something JSON serializable.
154
     *
155
     * @return array
156
     */
157
    public function jsonSerialize(): array
158
    {
159
        return $this->toArray();
160
    }
161
 
162
    /**
163
     * Convert the object to its JSON representation.
164
     *
165
     * @param  int  $options
166
     * @return string
167
     */
168
    public function toJson($options = 0)
169
    {
170
        return json_encode($this->jsonSerialize(), $options);
171
    }
172
}