https://github.com/compolomus/collection
https://github.com/compolomus/collection
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/compolomus/collection
- Owner: Compolomus
- License: mit
- Created: 2019-08-23T15:08:12.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-05T13:40:47.000Z (over 6 years ago)
- Last Synced: 2025-01-19T18:51:26.529Z (over 1 year ago)
- Language: PHP
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Compolomus Collection
[](https://packagist.org/packages/compolomus/Collection)
[](https://scrutinizer-ci.com/g/Compolomus/Collection/build-status/master)
[](https://scrutinizer-ci.com/g/Compolomus/Collection/?branch=master)
[](https://scrutinizer-ci.com/g/Compolomus/Collection/?branch=master)
[](https://codeclimate.com/github/Compolomus/Collection)
[](https://packagist.org/packages/compolomus/Collection)
# Install:
composer require compolomus/Collection
# Usage:
```php
use Compolomus\Collection\Collection;
require __DIR__ . '/vendor/autoload.php';
```
## New collection
### Single add
```php
$collection = new Collection('stdClass');
for ($i = 0; $i <= 42; $i++) {
$add = new stdClass();
$add->test = $i;
$collection->addOne($add);
}
```
### Batch add
```php
$array = [];
for ($i = 0; $i <= 42; $i++) {
$add = new stdClass();
$add->test = $i;
$array[] = $add;
}
$collection->addAll($array);
```
## Limit
### Count limit
```php
$limit1 = $collection->immutable()->limit(5);
echo '
' . print_r($limit1->get(), true) . '
';
/*
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
```php
$limit2 = $collection->immutable()->limit(3, 3);
echo '
' . print_r($limit2->get(), true) . '
';
/*
Array
(
[0] => stdClass Object
(
[test] => 3
)
[1] => stdClass Object
(
[test] => 4
)
[2] => stdClass Object
(
[test] => 5
)
)
*/
```
## Count
```php
echo $collection->count(); //43
echo $limit1->count(); // 5
echo $limit2->count(); // 3
```
## Sort
```php
$sort = $limit2->immutable()->sort('test', Collection::DESC);
echo '
' . print_r($sort->get(), true) . '
';
/*
Array
(
[2] => stdClass Object
(
[test] => 5
)
[1] => stdClass Object
(
[test] => 4
)
[0] => stdClass Object
(
[test] => 3
)
)
*/
```
## LINQ
```php
$linq = $collection->where('test > 33');
echo '
' . print_r($linq->get(), true) . '
';
/*
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
)
)
*/
```