Skip to content

Commit 223e012

Browse files
author
Fredrick Peter
committed
Pagination Error Update
1 parent b2ce979 commit 223e012

File tree

3 files changed

+22
-50
lines changed

3 files changed

+22
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ $users = $db->table('users')->paginate(20);
648648
649649
foreach($users as $user){
650650
651-
echo $users->numbers();
651+
echo $user->numbers();
652652
}
653653
```
654654

src/Collections/Collection.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function __construct($items = [])
4242
$this->items = $this->getArrayItems($items);
4343

4444
// if pagination request is `true`
45-
if(self::$check_paginate){
46-
self::$pagination_data = $this->items['data'] ?? [];
47-
self::$pagination = $this->items['pagination'] ?? false;
45+
if(self::$is_paginate){
46+
$this->items = $this->items['data'] ?? [];
47+
self::$pagination = $this->items['pagination'] ?? false;
4848
}
4949
}
5050

@@ -151,7 +151,7 @@ public function showing(?array $options = [])
151151
*/
152152
public function numbers(mixed $key = 0)
153153
{
154-
if(self::$check_paginate){
154+
if(self::$is_paginate){
155155
$key = (int) $key + 1;
156156
$pagination = $this->getPagination();
157157
return ($pagination->offset + $key);
@@ -181,15 +181,9 @@ public function __get($key)
181181
{
182182
// Convert data to array
183183
$unasignedItems = $this->toArray();
184-
185-
// check if `unescapeIsObjectWithoutArray` is true - Then return the data or null
186-
if($this->unescapeIsObjectWithoutArray){
187-
return $unasignedItems[$key] ?? null;
188-
}
189-
// Then check if data count is === 1, Then return its's data or null
190-
elseif($this->count() === 1){
191-
return $unasignedItems[0][$key] ?? null;
192-
}
184+
return $unasignedItems[$key]
185+
?? $unasignedItems[0][$key]
186+
?? null;
193187
}
194188

195189
/**

src/Collections/Traits/CollectionTrait.php

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@ trait CollectionTrait{
2525
static protected $pagination;
2626

2727
/**
28-
* Get pagination data
28+
* Instance of Database Paginate Method
2929
*
3030
* @var mixed
3131
*/
32-
static protected $pagination_data = [];
33-
34-
/**
35-
* Instance of Database Paginate request method
36-
*
37-
* @var mixed
38-
*/
39-
static protected $check_paginate;
32+
static protected $is_paginate;
4033

4134
/**
4235
* The methods that can be proxied.
@@ -115,7 +108,7 @@ static protected function getTrace()
115108
self::$instance = $interest[0] ?? null;
116109

117110
// instance of DB Paginate request
118-
self::$check_paginate = in_array(self::$instance, self::$proxies['paginate']);
111+
self::$is_paginate = in_array(self::$instance, self::$proxies['paginate']);
119112
}
120113

121114
/**
@@ -127,15 +120,10 @@ static protected function getTrace()
127120
*/
128121
protected function wrapArrayIntoCollectionMappers(mixed $items)
129122
{
130-
// if pagination request is true\ The collect the Pagination `data`
131-
// Otherwise, get the `items` passed as param
132-
$items = self::$check_paginate
133-
? self::$pagination_data
134-
: $items;
135-
123+
// check if valid array data
136124
if (is_array($items) && count($items) > 0) {
137125
return array_map(function ($item, $key){
138-
return new CollectionMapper($item, $key, self::$check_paginate, self::$pagination);
126+
return new CollectionMapper($item, $key, self::$is_paginate, self::$pagination);
139127
}, $items, array_keys($items));
140128
}
141129

@@ -166,7 +154,7 @@ protected function getArrayItems($items)
166154
*/
167155
public function getPagination()
168156
{
169-
if(self::$check_paginate){
157+
if(self::$is_paginate){
170158
$pagination = self::$pagination->pagination;
171159
return (object) [
172160
'limit' => (int) $pagination->limit,
@@ -228,13 +216,13 @@ public function isEmpty()
228216
*/
229217
public function count(): int
230218
{
231-
if(self::$check_paginate){
232-
return count(self::$pagination_data);
233-
} elseif($this->unescapeIsObjectWithoutArray){
234-
return 1;
219+
if($this->unescapeIsObjectWithoutArray){
220+
return 0;
235221
}
236222

237-
return $this->isArray() ? count($this->items): 0;
223+
return $this->isArray()
224+
? count($this->items)
225+
: 0;
238226
}
239227

240228
/**
@@ -244,7 +232,7 @@ public function count(): int
244232
*/
245233
public function toArray()
246234
{
247-
return json_decode( json_encode($this->getItemsData()), true);
235+
return json_decode( json_encode($this->items), true);
248236
}
249237

250238
/**
@@ -254,7 +242,7 @@ public function toArray()
254242
*/
255243
public function toObject()
256244
{
257-
return json_decode( json_encode($this->getItemsData()), false);
245+
return json_decode( json_encode($this->items), false);
258246
}
259247

260248
/**
@@ -264,7 +252,7 @@ public function toObject()
264252
*/
265253
public function toJson()
266254
{
267-
return json_encode($this->getItemsData());
255+
return json_encode($this->items);
268256
}
269257

270258
/**
@@ -277,16 +265,6 @@ private function isArray()
277265
return is_array($this->items) ? true : false;
278266
}
279267

280-
/**
281-
* Determine if pagination Data is to be returned or Items Data
282-
*
283-
* @return mixed
284-
*/
285-
private function getItemsData()
286-
{
287-
return self::$check_paginate ? self::$pagination_data : $this->items;
288-
}
289-
290268
/**
291269
* Convert data to an array on Initializaiton
292270
* @param mixed $items

0 commit comments

Comments
 (0)