- var_dump: documentation ( source)
- array_sum: documentation ( source)
- is_array: documentation ( source)
- iterator_to_array: documentation ( source)
<?php
namespace Atom;
interface CollectionInterface extends \IteratorAggregate
{
/**
*
* @param \Atom\AggregatorInterface $aggregator
* @return mixed
*/
function aggregate(\Atom\AggregatorInterface $aggregator);
/**
*
* @param \Atom\EnumeratorInterface $enumerator
* @return \Atom\CollectionInterface
*/
function enumerate(\Atom\EnumeratorInterface $enumerator);
/**
*
* @return array
*/
function getArray();
}
class Collection implements \Atom\CollectionInterface
{
/**
*
* @param mixed $iterable
* @return \Atom\Collection
*/
public static function create($iterable)
{
if (is_array($iterable)) {
return new \Atom\Collection(new \ArrayIterator($iterable));
}
return new \Atom\Collection(new \IteratorIterator($iterable));
}
/**
*
* @var \Iterator
*/
private $iterator;
/**
*
* @param \Iterator $iterator
*/
public function __construct(\Iterator $iterator)
{
$this->iterator = $iterator;
}
/**
*
* @param \Atom\AggregatorInterface $aggregator
* @return mixed
*/
public function aggregate(\Atom\AggregatorInterface $aggregator)
{
return $aggregator->aggregate($this);
}
/**
*
* @param \Atom\EnumeratorInterface $enumerator
* @return \Atom\CollectionInterface
*/
public function enumerate(\Atom\EnumeratorInterface $enumerator)
{
return self::create($enumerator->enumerate($this));
}
/**
*
* @return \Iterator
*/
public function getIterator()
{
return $this->iterator;
}
/**
*
* @return array
*/
public function getArray()
{
return iterator_to_array($this->iterator);
}
}
interface EnumeratorInterface
{
/**
*
* @param \Atom\CollectionInterface $collection
* @return mixed
*/
function enumerate(\Atom\CollectionInterface $collection);
}
interface AggregatorInterface
{
/**
*
* @param \Atom\CollectionInterface $collection
* @return mixed
*/
function aggregate(\Atom\CollectionInterface $collection);
}
class MapEnumerator implements \Atom\EnumeratorInterface
{
/**
*
* @var callable
*/
private $function;
/**
*
* @param callable $function
*/
public function __construct(callable $function)
{
$this->function = $function;
}
/**
*
* @param \Atom\CollectionInterface $collection
* @return mixed
*/
public function enumerate(\Atom\CollectionInterface $collection)
{
$function = $this->function;
foreach ($collection as $key => $value) {
yield $key => $function($value, $key);
}
}
}
class FilterEnumerator implements \Atom\EnumeratorInterface
{
/**
*
* @var callable
*/
private $function;
/**
*
* @param callable $function
*/
public function __construct(callable $function)
{
$this->function = $function;
}
/**
*
* @param \Atom\CollectionInterface $collection
* @return mixed
*/
public function enumerate(\Atom\CollectionInterface $collection)
{
$function = $this->function;
foreach ($collection as $key => $value) {
if ($function($value, $key)) {
yield $key => $value;
}
}
}
}
class SumAggregator implements \Atom\AggregatorInterface
{
/**
*
* @param \Atom\CollectionInterface $collection
* @return mixed
*/
public function aggregate(\Atom\CollectionInterface $collection)
{
return array_sum($collection->getArray());
}
}
$sum = \Collection::create([1, 2, 3, 4, 5])->enumerate(new \MapEnumerator(function ($value) {
return $value * 3;
}))->enumerate(new \FilterEnumerator(function ($value) {
return $value % 2 == 0;
}))->aggregate(new \SumAggregator());
var_dump($sum);