Skip to content

Commit 3c921dd

Browse files
author
Fredrick Peter
committed
Collection Updates On Global use
1 parent 3652497 commit 3c921dd

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

src/Collections/Collection.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class Collection extends CollectionProperty implements IteratorAggregate, ArrayAccess
2626
{
2727
use CollectionTrait, RelatedTrait;
28-
28+
2929
/**
3030
* Create a new collection.
3131
*
@@ -40,7 +40,6 @@ public function __construct(mixed $items = [], mixed $database = null)
4040
$this->database = $database;
4141
$this->isProxyAllowed = self::checkProxiesType();
4242
$this->items = $this->convertOnInit($items);
43-
4443
// if pagination request is `true`
4544
if($this->isPaginate){
4645
$this->pagination = $this->database;
@@ -54,11 +53,16 @@ public function __construct(mixed $items = [], mixed $database = null)
5453
*/
5554
public function getIterator() : Traversable
5655
{
57-
// On interation (foreach)
56+
// On interation of (foreach)
5857
// Wrap items into instance of CollectionMapper
59-
return new ArrayIterator(
60-
$this->wrapArrayIntoNewCollections()
61-
);
58+
if(!$this->isProxyAllowed){
59+
return new ArrayIterator(
60+
$this->wrapArrayIntoNewCollections()
61+
);
62+
} else{
63+
// disallow loop through Proxies items collections
64+
return new ArrayIterator([]);
65+
}
6266
}
6367

6468
/**

src/Collections/CollectionMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CollectionMapper extends CollectionProperty implements IteratorAggregate,
2929
* @param object\builder\Database\Collections\Collection
3030
* - Instance of Collection
3131
*/
32-
public function __construct($items = [], mixed $key = 0, object $collection)
32+
public function __construct(mixed $items = [], mixed $key = 0, object $collection = null)
3333
{
3434
$this->key = ((int) $key + 1);
3535
$this->database = $collection->database;

src/Collections/Traits/RelatedTrait.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getPagination()
4242
public function toSql()
4343
{
4444
if($this->isDBInstance){
45-
dd( $this->database->dbQuery()->stmt->queryString );
45+
dump( $this->database->dbQuery()->stmt->queryString );
4646
}
4747
}
4848

@@ -251,16 +251,37 @@ private function isArray()
251251
*/
252252
private function convertOnInit(mixed $items = null)
253253
{
254-
// For ORM Database Proxies Data
255-
if ($this->isProxyAllowed || $this->isPaginate || is_array($items)) {
254+
// For ORM Database Proxies and Paginate Data
255+
// Convert to an array
256+
if($this->isDBInstance){
256257
return json_decode(json_encode($items), true);
257258
} elseif($this->isValidJson($items)) {
258259
return json_decode($items, true);
259-
}
260+
} elseif($this->isNotValidArray($items)){
261+
return json_decode(json_encode($items), true);
262+
}
260263

261264
return $items;
262265
}
263266

267+
/**
268+
* Check if data is not a valid array
269+
*
270+
* @param mixed $data
271+
* @return bool
272+
*/
273+
private function isNotValidArray(mixed $data = null)
274+
{
275+
if (!is_array($data)) {
276+
return true;
277+
}
278+
279+
// array filter
280+
$filteredArray = array_filter($data, 'is_array');
281+
282+
return count($filteredArray) === count($data);
283+
}
284+
264285
/**
265286
* Check if a string is valid JSON.
266287
*

src/Pagination/Yidas/PaginationLoader.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ function __construct($options=[])
127127
// Page fetching
128128
if ($this->page===null) {
129129

130-
$this->page = isset($_GET[$this->pageParam]) ? $_GET[$this->pageParam] : 1;
130+
$this->page = isset($_GET[$this->pageParam]) ? (int) $_GET[$this->pageParam] : 1;
131131
}
132132

133133
// PrePage fetching
134134
if (!isset($options[$this->perPageParam]) && $this->perPageParam && isset($_GET[$this->perPageParam])) {
135135

136136
// Limit check
137137
$input = (int) $_GET[$this->perPageParam];
138-
list($min, $max) = $this->perPageLimit;
139-
$this->perPage = ($input <= $max && $input >= $min) ? $input : $this->perPage;
138+
list($min, $max) = (int) $this->perPageLimit;
139+
$this->perPage = ($input <= $max && $input >= $min) ? (int) $input : (int) $this->perPage;
140140
}
141141

142142
$this->_init();
@@ -200,14 +200,12 @@ public function createUrl($page, $perPage=null)
200200
*/
201201
protected function _init()
202202
{
203-
$this->convertToIntegers();
204-
205203
// Format
206-
$this->totalCount = ($this->totalCount > 0) ? floor($this->totalCount) : 0;
207-
$this->perPage = ($this->perPage >= 1) ? floor($this->perPage) : 20;
208-
$this->page = ($this->page >= 1) ? floor($this->page) : 1;
209-
$this->pageCount = ceil($this->totalCount / $this->perPage);
210-
$this->pageCount = ($this->pageCount > 0) ? $this->pageCount : 1;
204+
$this->totalCount = ($this->totalCount > 0) ? floor($this->totalCount) : 0;
205+
$this->perPage = ($this->perPage >= 1) ? floor($this->perPage) : 20;
206+
$this->page = ($this->page >= 1) ? floor($this->page) : 1;
207+
$this->pageCount = ceil($this->totalCount / $this->perPage);
208+
$this->pageCount = ($this->pageCount > 0) ? $this->pageCount : 1;
211209

212210
// Validate page
213211
if ($this->validatePage) {
@@ -217,6 +215,8 @@ protected function _init()
217215
$this->offset = $this->perPage * ($this->page - 1);
218216
// Limit ignores (total - offset)
219217
$this->limit = $this->perPage;
218+
219+
$this->convertToIntegers();
220220
}
221221

222222
/**
@@ -227,6 +227,8 @@ protected function _init()
227227
protected function convertToIntegers()
228228
{
229229
// Format
230+
$this->limit = (int) $this->limit;
231+
$this->offset = (int) $this->offset;
230232
$this->page = (int) $this->page;
231233
$this->perPage = (int) $this->perPage;
232234
$this->pageCount = (int) $this->pageCount;

src/Traits/ServerTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static private function getDirectRootPath()
242242
if (strpos($currentScript, $documentRoot) === 0) {
243243
$projectRootPath = substr($currentScript, strlen($documentRoot));
244244
$projectRootPath = trim($projectRootPath, '/');
245-
$projectRootPath = substr($projectRootPath, 0, strpos($projectRootPath, '/'));
245+
$projectRootPath = substr($projectRootPath, 0, (int) strpos($projectRootPath, '/'));
246246
$projectRootPath = $documentRoot . '/' . $projectRootPath;
247247

248248
// if not directory then get the directory of the path link

0 commit comments

Comments
 (0)