Pckg/collection provides a ways handle collection of items / arrays and strings differently. It works really well with pckg/skeleton, pckg/framework and pckg/database.
For standalone usage simply require pckg/collection in composer.
$ composer require pckg/collection
For advanced usage check pckg/skeleton.
$ composer install pckg/skeleton .
Package does not depend on any other package.
Test can be run with codeception
$ cp ./codeception.sample.yml ./codeception.yml
$ codecept run
// create a new Collection
$collection = new Collection();
// push items to the last position of the collection
// push some single items
$collection->push('foo');
$collection->push('bar');
// push a whole array
$collection->pushArray(['first', 'second']);
// pop last item and remove it from collection
$item = $collection->pop();
// add an item at the first position
$collection->prepend('prepended');
// retrieve and remove first item
$item = $collection->shift();
// retrieve first and last items keeping then in colection
$collection->first();
$collection->last();
Testing if collection has a set of values:
$collection = new Collection([
'foo' => [
'id' => 1,
'title' => 'baz',
],
'bar' => [
'id' => 2,
'title' => 'unknown',
],
]);
$collection->has(['id' => 1, 'title' => 'baz'])); // return true
$collection->has(['id' => 2, 'title' => 'baz'])); // return false
Return all itens that has a true
return from annonymous function.
$filtered = $collection->filter(function($item) {
return $item['id'] == 1;
});
var_dump($filtered->all()); // ['foo' => ['id' => 1, 'title' => 'baz']]
Set entry keys by some array item value. At following sample all keys are setted by title
inner array entry.
$keyed = $collection->keyBy('title');
var_dump($keyed->all());
/**
* [
* 'baz' => [
* 'id' => 1,
* 'title' => 'baz',
* ],
* 'unknown' => [
* 'id' => 2,
* 'title' => 'unknown',
* ],
* ]
**/
Return the firt item that satisfies a logical test.
$first = $collection->first(function($item) {
return $item['id'] > 1;
});
var_dump($first); //['id' => 2, 'title' => 'unknown']
Map the key of each entry to a inner value based on inner key.
$mapped = $collection->map('title');
var_dump($mapped->all()); // ['foo' => 'baz', 'bar' => 'unknown']
$collection = new Collection(['foo' => 'bar', 'baz' => 'test', 'john' => 'doe', 'jane' => 'name']);
// get a collection with a removed entry based on it key
$removedOne = $collection->removeKeys('baz'); // ['foo' => 'bar', 'john' => 'doe', 'jane' => 'name']
// get a collection with several entries removed based on they keys
$removedMultiple = $collection->removeKeys(['baz', 'john']); // ['foo' => 'bar', 'jane' => 'name']
// get a collection with several entries removed based on they values
$removedValues = $collection->removeValues(['bar', 'test']); // ['john' => 'doe', 'jane' => 'name']
// get all keys of a collection
$keys = $collection->keys(); // ['foo', 'baz', 'john', 'jane']
// get all values of a collection
$values = $collection->values(); // ['bar', 'test', 'doe', 'name']
// test if a key exist
$collection->hasKey('baz'); // true
$collection->hasKey('bz'); // false
// retrieve the value of a key
$collection->getKey('baz'); // 'test'
$collection = new Collection(['foo', 'bar', 'baz', '', ' untrimmed ']);
$sliced = $collection->slice(1, 2); // ['bar', 'baz']
Chunk by pieces.
$chunked = $collection->chunk(2);
/**
* [
* ['foo', 'bar'],
* ['baz', ''],
* [' untrimmed ']
*
**/
$flatten = $chunked->flat(); // ['foo', 'bar', 'baz', '', ' untrimmed ']
$trimmed = $collection->trim(); // ['foo', 'bar', 'baz', '', 'untrimmed']
Duplicate the items by the number passed.
$multiplied = $collection->multiply(2);
/**
* [
* 'foo',
* 'bar',
* 'baz',
* '',
* ' untrimmed ',
* 'foo',
* 'bar',
* 'baz',
* '',
* ' untrimmed ',
* ]
**/
Return a collection with no duplicated values
$unique = $multiplied->unique(); // ['foo', 'bar', 'baz', '', 'untrimmed']
Get a string with all collection values separated by imploded char.
$imploded = $collection->implode(' ', ' - '); // 'foo bar baz - untrimmed '
$nonEmpty = $collection->removeEmpty(); // ['foo', 'bar', 'baz', ' untrimmed ']
$collection = new Collection([2, 1, 13, 3, 1, 5, 21, 8]);
$sum = $collection->sum(); // 54
$avg = $collection->avg(); // 6.75
$min = $collection->min(); // 1
$max = $collection->max(); // 21