PHPackages                             nicmart/arrayze - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nicmart/arrayze

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nicmart/arrayze
===============

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

v0.1.4(11y ago)25232MITPHPPHP &gt;=5.5

Since Jun 15Pushed 11y ago2 watchersCompare

[ Source](https://github.com/nicmart/Arrayze)[ Packagist](https://packagist.org/packages/nicmart/arrayze)[ RSS](/packages/nicmart-arrayze/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (6)Used By (0)

Arrayze
=======

[](#arrayze)

[![Build Status](https://camo.githubusercontent.com/3f5b136e78d11dc90bab67495654ed5ae7119bfd975c23d17bcc7e1f10c869d0/68747470733a2f2f7472617669732d63692e6f72672f6e69636d6172742f41727261797a652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/nicmart/Arrayze)[![Coverage Status](https://camo.githubusercontent.com/e221b327a507822d17a3caaa5a0d48cac85f48e1564b4ef4b4e6c597b40dd1f9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6e69636d6172742f41727261797a652f62616467652e706e67)](https://coveralls.io/r/nicmart/Arrayze)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ea53c09898c487161f707913ca4a3a6bb543e5b437d3c423b25715ecfc6a99bf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e69636d6172742f41727261797a652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nicmart/Arrayze/?branch=master)

Give your values a lazy array interface!

What is Arrayze?
----------------

[](#what-is-arrayze)

Arrayze gives you an [adapter](http://en.wikipedia.org/wiki/Adapter_pattern) for ArrayAccess and Traversable php interfaces. The adapter is built from a collection of callbacks, that maps the original value to runtime-computed values.

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

Example
-------

[](#example)

Let's suppose you have a Person class:

```
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:

```
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:

```
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:

```
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

[](#convert-to-array)

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

Install
-------

[](#install)

The best way to install Arrayze is [through composer](http://getcomposer.org).

Just create a composer.json file for your project:

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

Then you can run these two commands to install it:

```
$ 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](http://getcomposer.org/doc/00-intro.md#globally).

Then you can include the autoloader, and you will have access to the library classes:

```
