webcodr/collection

Collection library for PHP

Installs: 167

Dependents: 1

Suggesters: 0

Security: 0

Stars: 7

Watchers: 2

Forks: 0

Open Issues: 0

Language:JavaScript

2.1.4 2013-06-08 22:04 UTC

This package is not auto-updated.

Last update: 2024-11-09 15:31:00 UTC


README

Build Status

PHP version

A set of array replacement classes for PHP

Requirements

  • PHP 5.4
  • Composer

Setup

Add Collection to your project

$ php composer.phar require webcodr/collection:2.*

Basic information

Collections provides two classes: MutableMap and ArrayMap

ArrayMap extends MutableMap with an implementation of the SPL interface ArrayAccess

MutableMap implements the SPL interfaces IteratorAggregate and Countable.

Both classes support a fluent interface with method chaining. Methods without a specific return value like get() or has() will return in the object itself.

Usage

A little code example says more than 1,000 words, so here we go:

The complete documentation is available in doc directory. (it's little buggy due to the use of traits)

<?php

use Collection\MutableMap;

$map = new MutableMap([
    'name' => 'William Adama',
    'rank' => 'Admiral'
]);

// getter and setter methods
echo $map->get('name'); // result = William Adama
var_dump($map->has('name')); // result = true

$map->set('commands', 'BSG 75');

// iterator method (you also use foreach() on the $map object)
$map->each(function($value, $attribute) {
    echo "{$attribute}: {$value}";
});

// update attributes with an array
$map->update([
    'name' => 'Lee Adama',
    'rank' => 'Commander',
    'commands' => 'BSG 62'
]);

echo $map->get('name'); // result = Lee Adama

// fast access with first() and last()
$battlestars = new MutableMap('Galactica', 'Pegasus', 'Atlantia');
echo $battlestars->first(); // result = Galactica
echo $battlestars->last(); // result = Atlantia

// method chaining
echo $battlestars->reverse()->last(); // result = Galactica