2017 © Pedro Peláez
 

library collection

Just an another collections library

image

jdecool/collection

Just an another collections library

  • Saturday, October 8, 2016
  • by jdecool
  • Repository
  • 1 Watchers
  • 1 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Collection

Build Status Latest Stable Version, (*1)

Provide a fluent collection library., (*2)

Available methods

all

Get all items of the collection, (*3)

$collection = new Collection([0, 1, 2, 3]);
$collection->all(); // [0, 1, 2, 3]

contains

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

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

diff

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'])

diffKeys

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

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'])

first

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')

flip

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

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'

has

Determine if a key exists in the collection, (*12)

$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->has('foo'); // true
$collection->has('bar'); // false

isEmpty

Check if the collection is empty, (*13)

$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->isEmpty(); // false

keys

Get all keys of the collection, (*14)

$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->keys(); // Collection(['foo', 'john'])

last

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')

map

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

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

reject

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

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])

The Versions

08/10 2016

dev-master

9999999-dev

Just an another collections library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires