1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mav3rick177\RapidPagination; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
10
|
|
|
use mav3rick177\RapidPagination\Base\PaginationResult as BasePaginationResult; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PaginationResult |
14
|
|
|
* |
15
|
|
|
* @see BasePaginationResult |
16
|
|
|
* @mixin Collection |
17
|
|
|
*/ |
18
|
|
|
class PaginationResult extends BasePaginationResult implements \JsonSerializable, Arrayable, Jsonable |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The view factory resolver callback. |
23
|
|
|
* |
24
|
|
|
* @var \Closure |
25
|
|
|
*/ |
26
|
|
|
protected static $viewFactoryResolver; |
27
|
|
|
|
28
|
|
|
use Macroable { |
29
|
|
|
__call as macroCall; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Make dynamic calls into the collection. |
34
|
|
|
* |
35
|
|
|
* @param string $method |
36
|
|
|
* @param array $parameters |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function __call($method, array $parameters) |
40
|
|
|
{ |
41
|
|
|
if (static::hasMacro($method)) { |
42
|
|
|
return $this->macroCall($method, $parameters); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->records->$method(...$parameters); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set URL for the previous page. |
50
|
|
|
* |
51
|
|
|
* @return string|null |
52
|
|
|
*/ |
53
|
|
|
public function makePreviousUrl($state) |
54
|
|
|
{ |
55
|
|
|
$this->previousUrl = $this->url('prev', $state); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set URL for the next page. |
60
|
|
|
* |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
|
|
public function makeNextUrl($state) |
64
|
|
|
{ |
65
|
|
|
$this->nextUrl = $this->url('next', $state); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set URL for the next page. |
71
|
|
|
* |
72
|
|
|
* @return string|null |
73
|
|
|
*/ |
74
|
|
|
public function setTabID($tab) |
75
|
|
|
{ |
76
|
|
|
$this->tab = $tab; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add a set of query string values to the paginator. |
81
|
|
|
* |
82
|
|
|
* @param array|string|null $key |
83
|
|
|
* @param string|null $value |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function appends($key, $value = null) |
87
|
|
|
{ |
88
|
|
|
if (is_null($key)) { |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (is_array($key)) { |
93
|
|
|
return $this->appendArray($key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->addQuery($key, $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add an array of query string values. |
101
|
|
|
* |
102
|
|
|
* @param array $keys |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
protected function appendArray(array $keys) |
106
|
|
|
{ |
107
|
|
|
foreach ($keys as $key => $value) { |
108
|
|
|
$this->addQuery($key, $value); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add a query string value to the paginator. |
117
|
|
|
* |
118
|
|
|
* @param string $key |
119
|
|
|
* @param string $value |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
protected function addQuery($key, $value) |
123
|
|
|
{ |
124
|
|
|
if ($key !== "direction" && $key !== "state" && $key !== "tab") { |
125
|
|
|
if($value) |
126
|
|
|
$this->query[$key] = $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the URL. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function url($direction, $state) |
138
|
|
|
{ |
139
|
|
|
$uri = url()->current() . '?'. 'direction=' . $direction . '&state=' . $state . '&tab=' . $this->tab; |
140
|
|
|
// append query to the URI |
141
|
|
|
if (is_array($this->query) || is_object($this->query)) |
142
|
|
|
{ |
143
|
|
|
foreach ($this->query as $key => $value){ |
144
|
|
|
$uri = $uri . '&' . $key . '=' . $value; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $uri; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the instance as an array. |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function toArray() |
156
|
|
|
{ |
157
|
|
|
$array = []; |
158
|
|
|
foreach (get_object_vars($this) as $key => $value) { |
159
|
|
|
$array[Str::snake($key)] = $value; |
160
|
|
|
} |
161
|
|
|
return $array; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Convert the object into something JSON serializable. |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function jsonSerialize() |
170
|
|
|
{ |
171
|
|
|
return $this->toArray(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Convert the object to its JSON representation. |
176
|
|
|
* |
177
|
|
|
* @param int $options |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function toJson($options = 0) |
181
|
|
|
{ |
182
|
|
|
return json_encode($this->jsonSerialize(), $options); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|