2017 © Pedro Peláez
 

library collection

Implementation for more human-like dealing with collections

image

pckg/collection

Implementation for more human-like dealing with collections

  • Saturday, June 23, 2018
  • by schtr4jh
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2,646 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 2 % Grown

The README.md

pckg/collection

Codacy Badge, (*1)

Build status, (*2)

Pckg/collection provides a ways handle collection of items / arrays and strings differently. It works really well with pckg/skeleton, pckg/framework and pckg/database., (*3)

Installation

For standalone usage simply require pckg/collection in composer., (*4)

$ composer require pckg/collection

For advanced usage check pckg/skeleton., (*5)

$ composer install pckg/skeleton .

Dependencies

Package does not depend on any other package., (*6)

Tests

Test can be run with codeception, (*7)

$ cp ./codeception.sample.yml ./codeception.yml
$ codecept run

Simple usage

// create a new Collection

$collection = new Collection();

// push items to the last position of the collection

// push some single items

$collection->push('foo');
$collection->push('bar');

// push a whole array

$collection->pushArray(['first', 'second']);

// pop last item and remove it from collection

$item = $collection->pop();

// add an item at the first position

$collection->prepend('prepended');

// retrieve and remove first item

$item = $collection->shift();

// retrieve first and last items keeping then in colection

$collection->first();
$collection->last();

Callbacks

has

Testing if collection has a set of values:, (*8)

$collection = new Collection([
                                 'foo' => [
                                     'id'    => 1,
                                     'title' => 'baz',
                                 ],
                                 'bar' => [
                                     'id'    => 2,
                                     'title' => 'unknown',
                                 ],
                             ]);

$collection->has(['id' => 1, 'title' => 'baz'])); // return true

$collection->has(['id' => 2, 'title' => 'baz'])); // return false

Filtering entries

Return all itens that has a true return from annonymous function., (*9)

$filtered = $collection->filter(function($item) {
    return $item['id'] == 1;
});

var_dump($filtered->all()); // ['foo' => ['id' => 1, 'title' => 'baz']]

Seting keys

Set entry keys by some array item value. At following sample all keys are setted by title inner array entry., (*10)

$keyed = $collection->keyBy('title');

var_dump($keyed->all()); 

/**
* [
*     'baz'     => [
*         'id'    => 1,
*         'title' => 'baz',
*     ],
*     'unknown' => [
*         'id'    => 2,
*         'title' => 'unknown',
*     ],
* ]
**/

Get first item by satisfied check

Return the firt item that satisfies a logical test., (*11)

$first = $collection->first(function($item) { 
   return $item['id'] > 1; 
});

var_dump($first); //['id' => 2, 'title' => 'unknown']

Map key by inner value

Map the key of each entry to a inner value based on inner key., (*12)

$mapped = $collection->map('title');

var_dump($mapped->all()); // ['foo' => 'baz', 'bar' => 'unknown']

Keys and Values

$collection = new Collection(['foo' => 'bar', 'baz' => 'test', 'john' => 'doe', 'jane' => 'name']);

// get a collection with a removed entry based on it key

$removedOne = $collection->removeKeys('baz');  // ['foo' => 'bar', 'john' => 'doe', 'jane' => 'name']

// get a collection with several entries removed based on they keys

$removedMultiple = $collection->removeKeys(['baz', 'john']); // ['foo' => 'bar', 'jane' => 'name']

// get a collection with several entries removed based on they values

$removedValues = $collection->removeValues(['bar', 'test']); // ['john' => 'doe', 'jane' => 'name']

// get all keys of a collection

$keys = $collection->keys(); // ['foo', 'baz', 'john', 'jane']

// get all values of a collection

$values = $collection->values(); // ['bar', 'test', 'doe', 'name']

// test if a key exist

$collection->hasKey('baz'); // true
$collection->hasKey('bz'); // false

// retrieve the value of a key

$collection->getKey('baz'); // 'test'

Manipulations

Slice a collection

$collection = new Collection(['foo', 'bar', 'baz', '', ' untrimmed ']);

$sliced = $collection->slice(1, 2); // ['bar', 'baz']

Chunk

Chunk by pieces., (*13)


$chunked = $collection->chunk(2); /** * [ * ['foo', 'bar'], * ['baz', ''], * [' untrimmed '] * **/

Flat


$flatten = $chunked->flat(); // ['foo', 'bar', 'baz', '', ' untrimmed ']

Trim


$trimmed = $collection->trim(); // ['foo', 'bar', 'baz', '', 'untrimmed']

Multiply by x

Duplicate the items by the number passed., (*14)


$multiplied = $collection->multiply(2); /** * [ * 'foo', * 'bar', * 'baz', * '', * ' untrimmed ', * 'foo', * 'bar', * 'baz', * '', * ' untrimmed ', * ] **/

unique

Return a collection with no duplicated values, (*15)


$unique = $multiplied->unique(); // ['foo', 'bar', 'baz', '', 'untrimmed']

Implode

Get a string with all collection values separated by imploded char., (*16)


$imploded = $collection->implode(' ', ' - '); // 'foo bar baz - untrimmed '

Remove empty values


$nonEmpty = $collection->removeEmpty(); // ['foo', 'bar', 'baz', ' untrimmed ']

Math

$collection = new Collection([2, 1, 13, 3, 1, 5, 21, 8]);

$sum = $collection->sum(); // 54

$avg = $collection->avg(); // 6.75

$min = $collection->min(); // 1

$max = $collection->max(); // 21

The Versions

23/06 2018

dev-master

9999999-dev https://github.com/pckg/collection

Implementation for more human-like dealing with collections

  Sources   Download

MIT

by Bojan Rajh

14/10 2017

dev-BR-codeception-tests

dev-BR-codeception-tests https://github.com/pckg/collection

Implementation for more human-like dealing with collections

  Sources   Download

MIT

by Bojan Rajh

12/10 2017

dev-BR-comments

dev-BR-comments https://github.com/pckg/collection

Implementation for more human-like dealing with collections

  Sources   Download

MIT

by Bojan Rajh

12/10 2017

dev-BR-docblock

dev-BR-docblock https://github.com/pckg/collection

Implementation for more human-like dealing with collections

  Sources   Download

MIT

by Bojan Rajh

06/03 2017

v0.1.0

0.1.0.0 https://github.com/pckg/collection

PHP collection implementation

  Sources   Download

MIT

by Bojan Rajh

22/02 2017

v1.0.0a

1.0.0.0-alpha https://github.com/pckg/collection

PHP collection implementation

  Sources   Download

MIT

by Bojan Rajh