Chainer is a small PHP library that makes functional style programming easier, as well as including a set of functions for data analysis.
- Implements itself as an array (allows for $chain0)
- Implements itself as an Iterator (allows for foreach)
- Supports chaining any class that implements Iterator
- Supports PHP5+ (Recommended 5.3+)
- Side-effect free, each call returns a new instance.
- Supports static calling syntax
<?php
include 'chain.php';
/*********************
* 3 Line Demo
********************/
$data = array(0, 1, 2, 3, 4);
Chain($data)
->map(function($x) { return $x * $x; })
->reduce(function($sum, $x) { return $sum + $x; }, 0);
// Squares each function and then sums it (in a verbose way)
$result = array(); // Array of database rows
/*********************
* Real World Demo
********************/
// Normalize data (run on all rows)
$results = Chain($result)->map(function($row) {
$row->date_created = strtotime($row->date_created)
});
// Select all featured ($results is left un-touched)
$featured = $results->select(function($x) {
return $x->featured;
});
// Get all id attributes
$showing = $results->pluck('id');
/*********************
* Static Calls
********************/
// Used if you only want to use one method
// all chainable functions available, first
// paramater is what the contuctor normally
// takes
$data = array('one', 'two', 'three');
$static = Chain::map($data, 'strtoupper');
$method = Chain($data)->map('strtoupper')->toArray();
// toArary is not nessesarily needed (only in case of an array cast)
?>