2017 © Pedro Peláez
 

library arrayze

A callback-based decorator that gives array access to values.

image

nicmart/arrayze

A callback-based decorator that gives array access to values.

  • Sunday, June 22, 2014
  • by nicmart
  • Repository
  • 2 Watchers
  • 24 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Arrayze

Build Status Coverage Status Scrutinizer Code Quality, (*1)

Give your values a lazy array interface!, (*2)

What is Arrayze?

Arrayze gives you an adapter for ArrayAccess and Traversable php interfaces. The adapter is built from a collection of callbacks, that maps the original value to runtime-computed values., (*3)

This means that you can easily give your objects or values an array-like interface, specifying how to compute offsets through callbacks., (*4)

Example

Let's suppose you have a Person class:, (*5)

class Person
{
    private $firstName;
    private $lastName;
    private $birthYear;

    public function __construct($firstName, $surname, $birthYear) { ... }

    public function getFirstName() { return $this->firstName; }
    public function getLastName() { return $this->lastName; }
    public function getBirthYear() { return $this->birthYear; }
}

You can then specify a collection of maps:, (*6)

use NicMart\Arrayze\MapsCollection;

$maps = (new MapsCollection)->registerMaps([
    "first name" =>   function(Person $p) { return $p->getFirstName(); },
    "last name" =>    function(Person $p) { return $p->getFirstName(); },
    "full name" =>    function($_, $x) { return "{$x['first name']} {$x['last name']}"; },
    "age" =>          function(Person $p) { return date("Y") - $p->getBirthYear(); },
    "name and age" => function($_, $x) { return "{$x['full name']}, {$x['age']}" }
]);

With that collection in place, you can now adapt Person instances to the new lazy array interface:, (*7)

use NicMart\Arrayze\ArrayAdapter;

$nic = new Person("Nicolò", "Martini", 1983);

$arrayzedNic = new ArrayAdapter($nic, $maps);

echo $arrayzedNic["full name"];    // Prints "Nicolò Martini"
echo $arrayzedNic["age"];          // Prints 31
echo $arrayzedNic["name and age"]; // Prints "Nicolò Martini, 31"

ArrayAdapter implements also the Iterator interface, so you can iterate (lazily) through your arrayzed objects:, (*8)

foreach ($arrayzedNic as $key => $value)
    echo "$key: $value\n";

// Prints
// first name: Nicolò
// last name: Martini
// full name: Nicolò Martini
// age: 31
// name and age: Nicolò Martini, 31

Convert to array

You can easily convert your adapted object to a native array with the ArrayAdapter::toArray() method., (*9)

Install

The best way to install Arrayze is through composer., (*10)

Just create a composer.json file for your project:, (*11)

{
    "require": {
        "nicmart/arrayze": "~0.1"
    }
}

Then you can run these two commands to install it:, (*12)

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

or simply run composer install if you have have already installed the composer globally., (*13)

Then you can include the autoloader, and you will have access to the library classes:, (*14)

<?php
require 'vendor/autoload.php';

The Versions

22/06 2014

dev-master

9999999-dev

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

22/06 2014

v0.1.4

0.1.4.0

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

17/06 2014

v0.1.3

0.1.3.0

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

16/06 2014

v0.1.2

0.1.2.0

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

15/06 2014

v0.1.1

0.1.1.0

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

15/06 2014

v0.1.0

0.1.0.0

A callback-based decorator that gives array access to values.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires