2017 © Pedro Peláez
 

library arrr

Array class for PHP

image

pisc/arrr

Array class for PHP

  • Thursday, December 15, 2016
  • by pisc
  • Repository
  • 1 Watchers
  • 2 Stars
  • 109 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Latest Stable Version Build Status, (*1)

arrr ✅

Array class for PHP, (*2)

Installation

$ composer require pisc/arrr

Instantiation:

// shorthand function (global function)
$array = ar([ 'someKey' => 'someValue' ]);

// constructor
$array = new Arrr([ 'someKey' => 'someValue' ]);

$array = new Ar([ 'someKey' => 'someValue' ]); // returns Ar instance
$array->mapIt(function($item) {
    return $item . "_suffix";
}); // returns Arrr instance

// factory
$array = Arrr::ar([ 'someKey' => 'someValue' ]);
$array = Arrr::instance([ 'someKey' => 'someValue' ]);
$array = Ar::ar([ 'someKey' => 'someValue' ]);
$array = Ar::instance([ 'someKey' => 'someValue' ]);

How to use:

use pisc\arrr\arrr;

$array = ar([ 'someKey' => 'someValue' ]);


$flat = Ar::flatten([ 'cow', [ 'bear', ['bunny', 'santa' ], 'rabbit' ]]);

Most methods are callable as: - Static method with the first parameter the array - Ar::map($array, function($item) { return $item; }) - Instance method which returns a new instance of Arrr - $Arrr->map(function($item) { return $item; }) - Instance method which works on the current instance - $Arrr->mapIt(function($item) { return $item; }), (*3)

Convention

All methods ending on 'It' modify the array itself and return itself, so you can do something like:, (*4)

$places = ar([
    'Amsterdam',
    'Berlin',
    'Paris',
    'Vienna',
    'Rome',
    'Madrid'
]);

$places->filterIt(function($place) {
    return in_array($place[0], [ 'A', 'V', 'R' ]);
});

$places->mapIt(function($place) {
    return "To destination {$place}";
})

echo $places->toJson(); 

// returns
'[ "To destination Amsterdam", "To destination Vienna", "To destination Rome" ]'

Run tests

# When installed globally
$ phpunit 

# When installed locally
$ ./vendor/bin/phpunit

Methods

sortBy (sortByIt)

Sort an array by multiple keys or result of a closure When a compare function result in 0, it will test the next key or closure, (*5)

accepts: - $sortByKeys ((array) string / closure ) - $methods ((array) string / callable ), (*6)

$array = ar([
    (object)[ 'id' => 1, 'name' => 'Jack', 'length' => 180 ],
    (object)[ 'id' => 2, 'name' => 'Jack', 'length' => 150 ],
    (object)[ 'id' => 3, 'name' => 'Ben', 'length' => 180 ],
    (object)[ 'id' => 4, 'name' => 'Ben', 'length' => 150 ],
    (object)[ 'id' => 5, 'name' => 'Vince', 'length' => 180 ],
    (object)[ 'id' => 6, 'name' => 'Vince', 'length' => 150 ],
]);

$sortedArray = $array->sortBy([ 'length', 'name' ], [ 'Sort::byDefault', 'strcmp' ]);

// result
[
    (object)[ 'id' => 4, 'name' => 'Ben', 'length' => 150 ],
    (object)[ 'id' => 2, 'name' => 'Jack', 'length' => 150 ],
    (object)[ 'id' => 6, 'name' => 'Vince', 'length' => 150 ],
    (object)[ 'id' => 3, 'name' => 'Ben', 'length' => 180 ],
    (object)[ 'id' => 1, 'name' => 'Jack', 'length' => 180 ],
    (object)[ 'id' => 5, 'name' => 'Vince', 'length' => 180 ],

]


The Versions

15/12 2016

dev-master

9999999-dev

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2016

0.0.13

0.0.13.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

24/10 2016

0.0.12

0.0.12.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

18/10 2016

0.0.11

0.0.11.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

03/10 2016

0.0.10

0.0.10.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

03/10 2016

0.0.9

0.0.9.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/06 2016

0.0.8

0.0.8.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/06 2016

0.0.7

0.0.7.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

17/04 2016

0.0.6

0.0.6.0

Array class for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires