Haldayne\Boost
improves the readability of PHP code that does a lot of
array manipulation.† Using a fluent interface that wraps any
collection-like data structure, you can build compact and readable expressions
to manipulate the elements in that collection.
Map
is the Boost wrapper around collection-like data. That means, you can
create a map from any of these types:
\Haldayne\Boost\Map
\Haldayne\Boost\Contract\Arrayable
\Haldayne\Boost\Contract\Jsonable
\Traversable
object
array
The Map
class gives you a fluent API on which you can solve many general
array-problems, like map/reduce:
$words = new Map(array ('bee', 'bear', 'beetle'));
$length = $words
->map(function ($word) { return strlen($word); })
->reduce(function ($total, $length) { return $total + $length; })
;
There are a boatload of built-in functions that apply to all Maps: review the API for details. But there are also purpose-built classes that extend the API to specific use-cases:
// join words together
$words = new MapOfStrings([ 'bleak', 'house' ]);
echo $words->join(' ');
// or sum all the odd integers between 1 and 10
// (Note use of short-hand code in grep statement)
$nums = new MapOfInts(range(0, 10));
echo $nums->all('1 == $_0 % 2')->sum();
These purpose-built classes demonstrate another feature of Boost's Map
:
membership requirements. With these requirements, you can solve the "type-hint
an array of Foo" problem. In the example below, the Bar
constructor
enforces that it takes only a collection of Foo
instances:
class Foo { }
class Bar {
public function __construct(MapOfFoo $foos) {
$this->foos = $foos;
}
}
class MapOfFoo extends \Haldayne\Boost\MapOfObjects {
protected function allowed($value) { return $value instanceof Foo; }
}
† On the Boost roadmap are plans to also improve the readability of heavy string manipulation code.