selvinortiz/collective

Simple collection implementation on top of native arrays

0.5.0 2016-06-16 05:01 UTC

This package is auto-updated.

Last update: 2024-11-20 11:05:59 UTC


README

Collective

Build Status Total Downloads Latest Stable Version

Description

Collective is a lightweight library that allows you to interact with native arrays in a more flexible and elegant way. It is inpired by Laravel Collections and focused on performance.

Requirements

Install

composer require selvinortiz/collective

Test

sh spec.sh

Usage

// 1. Create a new instance of Collective
// 2. Give it an (optional) input array
// 3. Call methods on it
$input = ['name' => 'Collective', 'release' => 'alpha'];

(new Collective($input))->get('name');
// 'Collective'

Reference

get($key, $default = null)

$input = [
    'user' => [
        'name'   => 'Brad',
        'salary' => '100000'
    ]
];

(new Collective($input))->get('users.name');
// 'Brad'

set($key, $value)

(new Collection())->set('user.name', 'Matt')->toArray();
// ['user' => ['name' => 'Matt']]

count()

$input = [0, 1, 2, 3, 4, 5];

(new Collective($input))->count();
// 6

first(callable $callback = null, $default = null)

$input      = [128, 256, 512, 'Brad', 'Brandon', 'Matt'];
$collective = new Collective($input);

$collective->first();
// 128

$collective->first(function($value)
{
    return strpos($value, 'Bra') !== false;
});
// Brad

last(callable $callback = null, $default = null)

$input      = [128, 256, 512 'Brad', 'Brandon', 'Matt'];
$collective = new Collective($input);

$collective->last();
// 'Matt'

$collective->last(function($value)
{
    return strpos($value, 'Bra') !== false;
});
// Brandon

map(callable $callback, array $default = [])

Applies your callable to each item in the collection

$input = [128, 256, 512 'Brad', 'Brandon', 'Matt'];

(new Collective($input))->map(function($value)
{
    return is_string($value) ? '- '.$value : $value;
})->toArray();
// 128, 256, 512, '- Brad', '- Brandon', '- Matt'

filter(callable $callback = null, array $default = null)

Filters each item in the collection using your own callable

$input = [128, 256, 512 'Brad', 'Brandon', 'Matt'];
(new Collective($input))->filter(function($value)
{
    return is_numeric($value);
})->toArray();
// 128, 256, 512

reduce(callable $callback, $initial = null)

Reduces a collection to a single value

$input    = [
    ['name' => 'Brad', 'salary' => 100000, 'type' => 'yearly'],
    ['name' => 'Brandon', 'salary' => 250000, 'type' => 'yearly']
];

$callback = function ($value, $carry) {
    return $carry + $value['salary'];
};

(new Collective($input))->reduce($callback);
// 350000

reverse()

(new Collective([128, 256, 512]))->reverse()->toArray();
// [512, 256, 128]

flatten($depth = INF)

$input = [
    'level1' => [
        'name'   => 'Level 1',
        'level2' => [
            'name'   => 'Level 2',
            'level3' => [
                'name' => 'Level 3'
            ]
        ]
    ]
];

(new Collective($input))->flatten()->toArray();
// ['Level 1', 'Level 2', 'Level 3']

then(callable $callback)

Chains functions not defined by the collection without breaking the pipe

function filterToStrings($collective) {
    return $collective->filter(function ($value) { return is_string($value); });
}

function fourCharsOnly($collective) {
    return $collective->filter(function ($value) { return strlen($value) == 4; });
}

$collective->then('filterToStrings')->then('filterToLength')->toArray();
// 'Brad', 'Matt'

License

Collective is open source software licensed under the MIT License