compolomus / collection
Collection
dev-master
2019-10-05 13:40 UTC
Requires
- php: ^7.3
Requires (Dev)
Suggests
- ext-xdebug: ^2.6.0
This package is auto-updated.
Last update: 2024-11-06 00:57:57 UTC
README
Install:
composer require compolomus/Collection
Usage:
use Compolomus\Collection\Collection; require __DIR__ . '/vendor/autoload.php';
New collection
Single add
$collection = new Collection('stdClass'); for ($i = 0; $i <= 42; $i++) { $add = new stdClass(); $add->test = $i; $collection->addOne($add); }
Batch add
$array = []; for ($i = 0; $i <= 42; $i++) { $add = new stdClass(); $add->test = $i; $array[] = $add; } $collection->addAll($array);
Limit
Count limit
$limit1 = $collection->immutable()->limit(5); echo '<pre>' . print_r($limit1->get(), true) . '</pre>'; /* Array ( [0] => stdClass Object ( [test] => 0 ) [1] => stdClass Object ( [test] => 1 ) [2] => stdClass Object ( [test] => 2 ) [3] => stdClass Object ( [test] => 3 ) [4] => stdClass Object ( [test] => 4 ) ) */
Limit with offset
$limit2 = $collection->immutable()->limit(3, 3); echo '<pre>' . print_r($limit2->get(), true) . '</pre>'; /* Array ( [0] => stdClass Object ( [test] => 3 ) [1] => stdClass Object ( [test] => 4 ) [2] => stdClass Object ( [test] => 5 ) ) */
Count
echo $collection->count(); //43 echo $limit1->count(); // 5 echo $limit2->count(); // 3
Sort
$sort = $limit2->immutable()->sort('test', Collection::DESC); echo '<pre>' . print_r($sort->get(), true) . '</pre>'; /* Array ( [2] => stdClass Object ( [test] => 5 ) [1] => stdClass Object ( [test] => 4 ) [0] => stdClass Object ( [test] => 3 ) ) */
LINQ
$linq = $collection->where('test > 33'); echo '<pre>' . print_r($linq->get(), true) . '</pre>'; /* Array ( [0] => stdClass Object ( [test] => 34 ) [1] => stdClass Object ( [test] => 35 ) [2] => stdClass Object ( [test] => 36 ) [3] => stdClass Object ( [test] => 37 ) [4] => stdClass Object ( [test] => 38 ) [5] => stdClass Object ( [test] => 39 ) [6] => stdClass Object ( [test] => 40 ) [7] => stdClass Object ( [test] => 41 ) [8] => stdClass Object ( [test] => 42 ) ) */