dev-master
9999999-devJust an another collections library
MIT
The Requires
- php >=5.4.0
The Development Requires
Just an another collections library
Provide a fluent collection library., (*2)
Get all items of the collection, (*3)
$collection = new Collection([0, 1, 2, 3]); $collection->all(); // [0, 1, 2, 3]
Determine if an item exists in the collection, (*4)
$collection = new Collection([0, 1, 2, 3]); $collection->contains(1); // true $collection->contains(5); // false
Count the number of items in the collection, (*5)
$collection = new Collection(); $collection->count(); // 0 $collection = new Collection([0, 1, 2, 3]); $collection->count(); // 4
Computes the difference of items in the collection, (*6)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']); $collection->diff('foo'); // Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']) $collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']); $collection->diff('doe'); // Collection(['foo' => 'bar'])
Computes the difference of keys in the collection, (*7)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']); $collection->diffKeys('foo'); // Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']) $collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']); $collection->diffKeys('foo' => 'bar'); // Collection(['john' => 'doe', 'jane' => 'doe'])
Filter the collection, (*8)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']); $collection->filter(function($item) { return $item === 'bar'; }); // Collection(['foo' => 'bar', 'test' => 'bar'])
Search first element, (*9)
$collection = new Collection([ ['a' => '1', 'foo' => 'a'], ['a' => '2', 'foo' => 'b'], ['a' => '3', 'foo' => 'c'], ['a' => '4', 'foo' => 'b'], ]); $collection->first(); // ['a' => '1', 'foo' => 'a'] $collection->first(function($item) { return $item['foo'] === 'b'; }); // ['a' => '2', 'foo' => 'b'] $collection->first(/* ... */, 'default value if not found')
Exchanges all keys with their associated values in an array, (*10)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']); $collection->flip(); // Collection(['bar' => 'foo', 'doe' => 'john'])
Get an item from the collection, (*11)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']); $collection->get('foo'); // 'bar' $collection->get('bar'); // null $collection->get('bar', 'myDefaultValue'); // 'myDefaultValue'
Determine if a key exists in the collection, (*12)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']); $collection->has('foo'); // true $collection->has('bar'); // false
Check if the collection is empty, (*13)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']); $collection->isEmpty(); // false
Get all keys of the collection, (*14)
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']); $collection->keys(); // Collection(['foo', 'john'])
Search last element, (*15)
$collection = new Collection([ ['a' => '1', 'foo' => 'a'], ['a' => '2', 'foo' => 'b'], ['a' => '4', 'foo' => 'b'], ['a' => '3', 'foo' => 'c'], ]); $collection->last(); // ['a' => '3', 'foo' => 'c'] $collection->last(function($item) { return $item['foo'] === 'b'; }); // ['a' => '4', 'foo' => 'b'] $collection->last(/* ... */, 'default value if not found')
Applies the callback to the elements of the given arrays, (*16)
$collection = new Collection([0, 1, 2, 3]); $collection->map(function($item) { return $item + 10; }); // Collection([10, 11, 12, 13])
Reduce the array to a single value, (*17)
function sum($carray, $item) { return $carray + $item; } $collection = new Collection([0, 1, 2, 3]); $collection->reduce('sum'); // 6 $collection = new Collection([ [ 'note' => 5, 'coeff' => 1, ], [ 'note' => 8, 'coeff' => 2, ], ]); $collection->reduce(function ($carry, $item) { return $carry + $item['note']; }); // 13
Create a collection without elements, (*18)
$collection = new Collection([0, 1, 2, 3]); $collection->reject(function($item) { return $item === 1; }); // Collection([0, 2, 3]) $collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']); $collection->reject(function($item) { return $item === 'bar'; }); // Collection(['john' => 'doe'])
Sort the items, (*19)
$collection = new Collection([3, 0, 2, 1]); $collection->sort(); // Collection([0, 1, 2, 3]) $collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']); $collection->reject(function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? 1 : -1; }); // Collection([3, 2, 1, 0])
Just an another collections library
MIT