Skip to content

Commit 18d81aa

Browse files
author
Fredrick Peter
committed
Collections Upgrade... Most stable of all versions
1 parent 03a4a2c commit 18d81aa

File tree

15 files changed

+299
-315
lines changed

15 files changed

+299
-315
lines changed

README.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ was pretty tough. So i decided to create a much more easier way of communicating
5454
* [Pagination Links Config](#pagination-links-config)
5555
* [Pagination Showing](#pagination-showing)
5656
* [Pagination Showing Config](#pagination-showing-config)
57-
* [Pagination Numbers](#pagination-numbers)
57+
* [Pagination Foreach Numbers](#pagination-foreach-numbers)
5858
* [Get Pagination](#get-pagination)
5959
* [Clause](#clause)
6060
* [Raw](#raw)
@@ -116,7 +116,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
116116
**Step 1** — update your `composer.json`:
117117
```composer.json
118118
"require": {
119-
"peterson/php-orm-database": "^4.2.0"
119+
"peterson/php-orm-database": "^4.2.1"
120120
}
121121
```
122122

@@ -390,15 +390,16 @@ $db->table('post')
390390

391391
## Fetching Data
392392

393-
| object name | Returns |
394-
|---------------|--------------------|
395-
| get() | array of objects |
396-
| first() | object |
397-
| firstOrFail() | object or exit with 404 status |
398-
| count() | int |
399-
| paginate() | array of objects |
400-
| exists() | boolean `true` \| `false` |
401-
| tableExists() | boolean `true` \| `false` |
393+
| object name | Returns |
394+
|-------------------|-----------------------------------|
395+
| get() | array of objects |
396+
| first() | object |
397+
| FirstOrCreate() | object or exit with 404 status |
398+
| firstOrFail() | object or exit with 404 status |
399+
| count() | int |
400+
| paginate() | array of objects |
401+
| exists() | boolean `true` \| `false` |
402+
| tableExists() | boolean `true` \| `false` |
402403

403404
### GET
404405
```
@@ -563,17 +564,18 @@ http://domain.com/storage/[asset_file]?v=111111111
563564

564565

565566
### Collection Methods
566-
| Methods | Description |
567-
|-------------------|-------------------------------------------|
568-
| getAttributes() | `array` Returns an array of data |
569-
| getOriginal() | `object` Returns an object of data |
570-
| isEmpty() | `boolean` `true \| false` If data is empty |
571-
| isNotEmpty() | `opposite` of `->isEmpty()` |
572-
| count() | `int` count data in items collection |
573-
| toArray() | `array` Convert items to array |
574-
| toObject() | `object` Convert items to object |
575-
| toJson() | `string` Convert items to json |
576-
| toSql() | `string` Sql Query String without execution |
567+
| Methods | Description |
568+
|-------------------|-----------------------------------------------|
569+
| getAttributes() | `array` Returns an array of data |
570+
| getOriginal() | `object` Returns an object of data |
571+
| isEmpty() | `boolean` `true \| false` If data is empty |
572+
| isNotEmpty() | `opposite` of `->isEmpty()` |
573+
| count() | `int` count data in items collection |
574+
| toArray() | `array` Convert items to array |
575+
| toObject() | `object` Convert items to object |
576+
| toJson() | `string` Convert items to json |
577+
| toSql() | `string` Sql Query String without execution |
578+
| dd() | `object` Returns dbQuery and exit the script |
577579

578580

579581
### Collection Usage
@@ -731,9 +733,10 @@ $users->showing([
731733
```
732734
</details>
733735

734-
### Pagination Numbers
736+
### Pagination Foreach Numbers
735737
- Page numbering `starts counting from 1`
736-
- This will format each numbers of data according to it's possition
738+
- This will format all pagination items collections
739+
- On each page, it starts counting from last pagination item number
737740

738741
```
739742
$users = $db->table('users')->paginate(20);

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
"php": ">=7.2.5",
2020
"vlucas/phpdotenv": "^5.3",
2121
"ezyang/htmlpurifier": "^4.16.0",
22-
"symfony/var-dumper": "^4.4",
22+
"symfony/var-dumper": "4.4.*",
2323
"filp/whoops": "^2.15",
24-
"psr/log": "^1.1.4"
24+
"psr/log": "^1.0"
25+
},
26+
"conflict": {
27+
"symfony/var-dumper": "^6.0"
2528
},
2629
"autoload": {
2730
"files": [
@@ -39,7 +42,7 @@
3942
},
4043
"extra": {
4144
"branch-alias": {
42-
"dev-main": "4.2.0-dev"
45+
"dev-main": "4.2.1-dev"
4346
}
4447
},
4548
"minimum-stability": "stable",

src/Collections/Collection.php

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,25 @@
2525
class Collection extends CollectionProperty implements IteratorAggregate, ArrayAccess
2626
{
2727
use CollectionTrait, RelatedTrait;
28-
29-
/**
30-
* The items contained in the collection.
31-
*
32-
* @var array
33-
*/
34-
protected $items = [];
35-
3628

3729
/**
3830
* Create a new collection.
3931
*
40-
* @param array $items
32+
* @param mixed $items
33+
* @param mixed $database Instance of ORM Database \builder\Database\DB
34+
* - [optional] Used on ORM Database Only
35+
* Meant for easy manupulation of collection instance
36+
* This doesn't have affect on using this the Collection class on other projects
4137
*/
42-
public function __construct($items = [])
38+
public function __construct(mixed $items = [], mixed $database = null)
4339
{
44-
$this->unescapeIsObjectWithoutArray = self::checkProxiesType();
45-
$this->items = $this->convertOnInit($items);
46-
40+
$this->database = $database;
41+
$this->isProxyAllowed = self::checkProxiesType();
42+
$this->items = $this->convertOnInit($items);
43+
4744
// if pagination request is `true`
48-
if(self::$is_paginate){
49-
$tempItems = $this->items;
50-
$this->items = $tempItems['data'] ?? [];
51-
self::$pagination = $tempItems['pagination'] ?? false;
45+
if($this->isPaginate){
46+
$this->pagination = $this->database;
5247
}
5348
}
5449

@@ -62,7 +57,7 @@ public function getIterator() : Traversable
6257
// On interation (foreach)
6358
// Wrap items into instance of CollectionMapper
6459
return new ArrayIterator(
65-
$this->wrapArrayIntoCollectionMappers($this->items)
60+
$this->wrapArrayIntoNewCollections()
6661
);
6762
}
6863

@@ -74,8 +69,8 @@ public function getIterator() : Traversable
7469
*/
7570
public function links(?array $options = [])
7671
{
77-
if(self::$pagination){
78-
self::$pagination->links($options);
72+
if($this->pagination){
73+
$this->pagination->links($options);
7974
}
8075
}
8176

@@ -87,26 +82,9 @@ public function links(?array $options = [])
8782
*/
8883
public function showing(?array $options = [])
8984
{
90-
if(self::$pagination){
91-
self::$pagination->showing($options);
92-
}
93-
}
94-
95-
/**
96-
* Get Pagination Numbers
97-
* @param mixed $key
98-
*
99-
* @return string
100-
*/
101-
public function numbers(mixed $key = 0)
102-
{
103-
if(self::$is_paginate){
104-
$key = (int) $key + 1;
105-
$pagination = $this->getPagination();
106-
return ($pagination->offset + $key);
85+
if($this->pagination){
86+
$this->pagination->showing($options);
10787
}
108-
109-
return $key;
11088
}
11189

11290
}

src/Collections/CollectionMapper.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
class CollectionMapper extends CollectionProperty implements IteratorAggregate, ArrayAccess
1515
{
1616
use RelatedTrait;
17-
18-
/**
19-
* The items contained in the collection.
20-
*
21-
* @var array
22-
*/
23-
protected $items = [];
2417

2518
/**
2619
* Array index key
@@ -33,11 +26,18 @@ class CollectionMapper extends CollectionProperty implements IteratorAggregate,
3326
*
3427
* @param mixed $items
3528
* @param mixed $key
29+
* @param object\builder\Database\Collections\Collection
30+
* - Instance of Collection
3631
*/
37-
public function __construct($items = [], mixed $key = 0)
32+
public function __construct($items = [], mixed $key = 0, object $collection)
3833
{
39-
$this->key = ((int) $key + 1);
40-
$this->items = $this->convertMapperOnInit($items);
34+
$this->key = ((int) $key + 1);
35+
$this->database = $collection->database;
36+
$this->isDBInstance = $collection->isDBInstance;
37+
$this->isPaginate = $collection->isPaginate;
38+
$this->pagination = $collection->pagination;
39+
$this->isProxyAllowed = $collection->isProxyAllowed;
40+
$this->items = $this->convertOnInit($items, true);
4141
}
4242

4343
/**
@@ -57,7 +57,7 @@ public function getIterator() : Traversable
5757
*/
5858
public function numbers()
5959
{
60-
if(self::$is_paginate){
60+
if($this->isPaginate){
6161
$pagination = $this->getPagination();
6262
return ($pagination->offset + $this->key);
6363
}

src/Collections/CollectionProperty.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,56 @@
1616
class CollectionProperty
1717
{
1818
/**
19-
* Instance of Database fetch request method
19+
* The items contained in the collection.
2020
*
2121
* @var mixed
2222
*/
23-
static protected $instance;
23+
protected $items = [];
2424

2525
/**
26-
* Get pagination items
26+
* Check if is object without array
2727
*
28-
* @var mixed\builder\Database\DB
28+
* @var bool
2929
*/
30-
static protected $pagination;
30+
protected $isProxyAllowed = false;
3131

3232
/**
3333
* If Instance of Database Pagination Method is true
3434
* @var mixed
3535
*/
36-
static protected $is_paginate = false;
36+
protected $isPaginate = false;
3737

3838
/**
39-
* The methods that can be proxied.
39+
* If Instance of \builder\Database\DB is true
40+
* @var bool
41+
*/
42+
protected $isDBInstance = false;
43+
44+
/**
45+
* Get pagination items
4046
*
41-
* @var array
47+
* @var mixed\builder\Database\DB
4248
*/
43-
static protected $proxies = [
44-
'get' => ['get'],
45-
'first' => ['first', 'firstorcreate', 'firstorfail'],
46-
'insert' => ['insert', 'insertorignore'],
47-
'paginate' => ['paginate'],
48-
];
49+
protected $pagination;
50+
51+
/**
52+
* Instance of ORM Database Class
53+
*
54+
* @var mixed\builder\Database\DB
55+
*/
56+
protected $database;
4957

5058
/**
5159
* The methods that can be proxied.
5260
*
5361
* @var array
5462
*/
55-
static protected $proxies_compact = [
56-
'get',
63+
static protected $proxies = [
5764
'first',
5865
'firstorcreate',
5966
'firstorfail',
6067
'insert',
6168
'insertorignore',
62-
'paginate',
6369
];
6470

6571
}

0 commit comments

Comments
 (0)