Skip to content

Commit 0880f8c

Browse files
author
Fredrick Peter
committed
Version Update and Pagination
1 parent 669890b commit 0880f8c

File tree

11 files changed

+273
-129
lines changed

11 files changed

+273
-129
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.env
22
.gitignore
33
/database
4+
/storage
45
/vendor
56
/.idea
67
/.vscode

README.md

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ was pretty tough. So i decided to create a much more easier way of communicating
5050
* [Pagination Links Config](#pagination-links-config)
5151
* [Pagination Showing](#pagination-showing)
5252
* [Pagination Showing Config](#pagination-showing-config)
53+
* [Pagination Numbers](#pagination-numbers)
54+
* [Get Pagination](#get-pagination)
5355
* [Clause](#clause)
5456
* [Raw](#raw)
5557
* [select](#select)
@@ -106,7 +108,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
106108
**Step 1** — update your `composer.json`:
107109
```composer.json
108110
"require": {
109-
"peterson/php-orm-database": "^3.1.5"
111+
"peterson/php-orm-database": "^3.1.6"
110112
}
111113
```
112114

@@ -529,19 +531,16 @@ if($users->isNotEmpty()){
529531
| class | string | Css `selector` For pagination ul tag in the browser |
530532
| span | string | Default `.pagination-highlight` Css `selector` For pagination Showing Span tags in the browser |
531533
| view | `bootstrap` \| `simple` | Default `simple` - For pagination design |
532-
| first | string | Change the letter of `First` |
533-
| last | string | Change the letter of `Last` |
534-
| next | string | Change the letter of `Next` |
535-
| prev | string | Change the letter of `Prev` |
536-
| showing | string | Change the letter of `Showing` |
537-
| of | string | Change the letter `of` |
538-
| to | string | Change the letter `to` |
534+
| first | string | Change the letter `First` |
535+
| last | string | Change the letter `Last` |
536+
| next | string | Change the letter `Next` |
537+
| prev | string | Change the letter `Prev` |
538+
| showing | string | Change the letter `Showing` |
539+
| of | string | Change the letter `of` |
539540
| results | string | Change the letter `results` |
540541

541542

542543
### Global Configuration
543-
<details><summary>Read more...</summary>
544-
545544
- 1 Setup global pagination on ENV autostart `most preferred` method
546545
```
547546
AutoloadEnv::configurePagination([
@@ -552,20 +551,21 @@ AutoloadEnv::configurePagination([
552551
'view' => 'bootstrap',
553552
'class' => 'Custom-Class-Css-Selector',
554553
]);
554+
```
555555

556-
or Helpers Function
557-
556+
- or Helpers Function
557+
```
558558
configure_pagination([
559-
559+
'allow' => true,
560560
]);
561561
```
562562

563+
<details><summary>Read more...</summary>
563564
- 2 Can also be called using the `$db->configurePagination` method
564565
```
565566
$db->configurePagination([
566567
'allow' => true,
567568
'view' => 'bootstrap',
568-
'class' => 'Custom-Class-Css-Selector',
569569
]);
570570
```
571571

@@ -580,8 +580,7 @@ $db = new DB([
580580

581581
### Pagination Query
582582
```
583-
$users = $db->table('users')
584-
->paginate(40);
583+
$users = $db->table('users')->paginate(40);
585584
586585
-- Query
587586
SELECT *
@@ -623,7 +622,7 @@ $users->showing();
623622
624623
// This will create a span html element with text
625624
<span class='pagination-highlight'>
626-
Showing 0 to 40 of 500 results
625+
Showing 0-40 of 500 results
627626
</span>
628627
```
629628

@@ -634,14 +633,44 @@ $users->showing();
634633
```
635634
$users->showing([
636635
'showing' => 'Showing',
637-
'to' => 'To',
638636
'of' => 'out of',
639637
'results' => 'Results',
640638
'span' => 'css-selector',
641639
])
642640
```
643641
</details>
644642

643+
### Pagination Numbers
644+
- Page numbering `starts counting from 1`
645+
- This will format each numbers of data according to it's possition
646+
647+
```
648+
$users = $db->table('users')->paginate(20);
649+
650+
foreach($users as $user){
651+
652+
echo $users->numbers();
653+
}
654+
```
655+
656+
### Get Pagination
657+
- Returns pagination informations
658+
659+
| key | Description |
660+
|---------------|---------------------------|
661+
| limit | Pagination limit `int` |
662+
| offset | Pagination offset `int` |
663+
| page | Pagination Current page `int` |
664+
| pageCount | Pagination Total page count `int` |
665+
| perPage | Pagination per page count `int` |
666+
| totalCount | Pagination total items count `int`|
667+
668+
```
669+
$users = $db->table('users')->paginate(20);
670+
671+
$users->getPagination();
672+
```
673+
645674
## Clause
646675
- Multiple clause
647676

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"extra": {
4040
"branch-alias": {
41-
"dev-main": "3.1.5-dev"
41+
"dev-main": "3.1.6-dev"
4242
}
4343
},
4444
"minimum-stability": "stable",

src/AutoloadEnv.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ static public function configurePagination(?array $options = [])
166166
'prev' => $options['prev'] ?? $text['prev'],
167167
'span' => $options['span'] ?? $text['span'],
168168
'showing' => $options['showing'] ?? $text['showing'],
169-
'to' => $options['to'] ?? $text['to'],
170169
'of' => $options['of'] ?? $text['of'],
171170
'results' => $options['results'] ?? $text['results'],
172171
];

src/Collections/Collection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ public function showing(?array $options = [])
147147
}
148148
}
149149

150+
/**
151+
* Get Pagination Numbers
152+
* @param mixed $key
153+
*
154+
* @return string
155+
*/
156+
public function numbers(mixed $key = 0)
157+
{
158+
$key = (int) $key + 1;
159+
if(self::$check_paginate){
160+
$pagination = $this->getPagination();
161+
return ($pagination->offset + $key);
162+
}
163+
164+
return $key;
165+
}
166+
150167
/**
151168
* Check if an item exists in the collection.
152169
*

src/Collections/CollectionMapper.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@ class CollectionMapper implements IteratorAggregate, ArrayAccess
1313
{
1414
private $attributes;
1515
private $getQuery;
16+
private $key;
17+
static private $check_paginate = false;
18+
static private $pagination;
1619

1720
/**
1821
* Create a new collection.
1922
*
2023
* @param mixed $items
2124
*/
22-
public function __construct($items = [])
25+
public function __construct($items = [], mixed $key = 0, ?bool $check_paginate = false, mixed $pagination = null)
2326
{
24-
$this->attributes = self::convertOnInit($items);
25-
$this->getQuery = get_query();
27+
$this->attributes = $this->convertOnInit($items);
28+
$this->getQuery = get_query();
29+
$this->key = ($key + 1);
30+
31+
// if pagination request is `true`
32+
if($check_paginate){
33+
self::$check_paginate = $check_paginate;
34+
self::$pagination = $pagination->pagination ?? null;
35+
}
2636
}
2737

2838
/**
@@ -91,6 +101,41 @@ public function __isset($key)
91101
return isset($this->attributes[$key]);
92102
}
93103

104+
/**
105+
* Get Pagination Object
106+
*
107+
* @return mixed
108+
*/
109+
public function getPagination()
110+
{
111+
if(self::$check_paginate){
112+
$pagination = self::$pagination;
113+
return (object) [
114+
'limit' => (int) $pagination->limit,
115+
'offset' => (int) $pagination->offset,
116+
'page' => (int) $pagination->page,
117+
'pageCount' => (int) $pagination->pageCount,
118+
'perPage' => (int) $pagination->perPage,
119+
'totalCount' => (int) $pagination->totalCount,
120+
];
121+
}
122+
}
123+
124+
/**
125+
* Get Pagination Numbers
126+
*
127+
* @return string
128+
*/
129+
public function numbers()
130+
{
131+
if(self::$check_paginate){
132+
$pagination = $this->getPagination();
133+
return ($pagination->offset + $this->key);
134+
}
135+
136+
return $this->key;
137+
}
138+
94139
/**
95140
* return items collection as an array
96141
*
@@ -128,9 +173,7 @@ public function isNotEmpty()
128173
*/
129174
public function isEmpty()
130175
{
131-
return $this->count() === 0
132-
? true
133-
: false;
176+
return empty($this->attributes);
134177
}
135178

136179
/**
@@ -191,7 +234,7 @@ public function getQuery()
191234
*
192235
* @return array
193236
*/
194-
static private function convertOnInit(mixed $items = null)
237+
private function convertOnInit(mixed $items = null)
195238
{
196239
return json_decode( json_encode($items), true);
197240
}

0 commit comments

Comments
 (0)