This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (v1.0.4) of this package.

A simple collection package compatible with PHP 5.4 with no dependencies

v1.0.4 2017-10-21 15:56 UTC

This package is auto-updated.

Last update: 2024-04-19 20:56:52 UTC


README

A simple collection package compatible with PHP 5.4 with no dependencies

Usage

Example

$collection = new Collection(['apple', 'orange', 'peach']);

$collection->map(function ($fruit) {
    return 'Don\'t touch my fucking ' . $fruit;
})->each(function($rule) {
    echo $rule . '<br />';
});

Creating

$collection = new Collection;
$collection2 = new Collection([1, 2, 3]);
$collection3 = new Collection([
    'hello' => 'goodbye',
    'your mother' => 'should know',
]);

Adding items

push

$collection = new Collection;
$collection->push('item');
$collection->push('item2');

$collection->toArray(); // ['item', 'item2'];

put

$collection = new Collection;
$collection->put('key', 'value');

$collection->toArray(); // ['key' => 'value'];

Filtering

filter

$collection = new Collection([1, 2, 3, 4]);
$collection->filter(function($value, $key) {
    return $value < 3;
});

$collection->toArray(); // [1, 2];

reject

$collection = new Collection([1, 2, 3, 4]);
$collection->reject(function($value, $key) {
    return $value < 3;
});

$collection->toArray(); // [3, 4];

Snooping

hasKey

$collection = new Collection(['propietario' => 1]);

$collection->hasKey('propietario'); // true
$collection->hasKey('un chorro'); // false;

hasValue

$collection = new Collection(['propietario' => 1]);

$collection->hasValue(1); // true
$collection->hasValue(2); // false;

count

Self explanatory

isEmpty

Self explanatory

isNotEmpty

Self explanatory

Getting items

all

Self explanatory

toArray

Self explanatory

first

Self explanatory

get

$collection = new Collection(['key' => 'value']);

$collection->get('key'); // 'value'

shift

$collection = new Collection(['your sister', 'your mom']);

$collection->shift(); // 'your sister'

pop

$collection = new Collection(['your sister', 'your mom']);

$collection->pop(); // 'your mom'

keys

$collection = new Collection(['key' => 'value', 'key2' => 'value2']);

$collection->keys()->toArray(); // ['key', 'key2'] 

values

$collection = new Collection(['key' => 'value', 'key2' => 'value2']);

$collection->keys()->toArray(); // ['value', 'value2'] 

reduce

$collection = new Collection([1, 1, 1]);

$collection->reduce(function ($value, $initial) {
    return $initial + $value;
}, 0); // 3

implode

$collection = new Collection([1, 2, 3]);

$collection->implode('.'); // '1.2.3'

Iterating

each

$collection = new Collection([1, 2, 3]);

$collection->each(function ($value, $key) {
    // do something
});

map

$collection = new Collection([1, 2, 3]);

$collection->map(function ($value, $key) {
    return $value * 2;
});

$collection->toArray(); // [2, 4, 6]

Moring

merge

$collection = new Collection([1, 2, 3]);

$collection->merge([4]);

$collection->toArray(); // [1, 2, 3, 4]

sortBy

$collection = new Collection(3, 1, 2]);

$collection->sortBy(function ($value, $key) {
    return $value;
});

$collection->toArray(); // [1, 2, 3]

sortByDesc

$collection = new Collection(3, 1, 2]);

$collection->sortByDesc(function ($value, $key) {
    return $value;
});

$collection->toArray(); // [3, 2, 1]