2017 © Pedro Peláez
 

library collective

Simple collection implementation on top of native arrays

image

selvinortiz/collective

Simple collection implementation on top of native arrays

  • Thursday, June 16, 2016
  • by selvinortiz
  • Repository
  • 1 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Collective, (*1)

Build Status Total Downloads Latest Stable Version, (*2)

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., (*3)

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, (*4)

$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, (*5)

$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, (*6)

$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, (*7)

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, (*8)

The Versions

16/06 2016

dev-master

9999999-dev

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

16/06 2016

dev-dev

dev-dev

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

16/06 2016

0.5.0

0.5.0.0

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

16/06 2016

v0.4.0

0.4.0.0

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

15/06 2016

v0.3.0

0.3.0.0

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

14/06 2016

v0.2.0

0.2.0.0

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective

12/06 2016

v0.1.0

0.1.0.0

Simple collection implementation on top of native arrays

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection array collective