Download the PHP package webcodr/collection without Composer
On this page you can find all versions of the php package webcodr/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webcodr/collection
More information about webcodr/collection
Files in webcodr/collection
Package collection
Short Description Collection library for PHP
License MIT
Homepage https://github.com/WebCodr/Collection
Informations about the package collection
Collection
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