Skip to content

Instantly share code, notes, and snippets.

@calcinai
Created October 3, 2017 01:15
Show Gist options
  • Save calcinai/d007edd33bc4d9ba977e9f399c2332cb to your computer and use it in GitHub Desktop.
Save calcinai/d007edd33bc4d9ba977e9f399c2332cb to your computer and use it in GitHub Desktop.
<?php
/**
* @author Michael Calcinai <michael@calcin.ai>
*/
class IteratorPropertySorterIterator implements \Iterator
{
/**
* @var \Iterator[]
*/
private $iterators;
/**
* @var callable
*/
private $sort_callback;
/**
* IteratorPropertySorterIterator constructor.
* @param callable $sort_callback
*/
public function __construct(callable $sort_callback)
{
$this->iterators = array_slice(func_get_args(), 1);
$this->sort_callback = $sort_callback;
}
public function addIterator(\Iterator $iterator)
{
$this->iterators[] = $iterator;
return $this;
}
/**
* @return \Iterator|null
*/
public function getLeadingIterator()
{
/** @var \Iterator $leading_iterator */
$leading_iterator = null;
foreach ($this->iterators as $iterator) {
//Skip any that have run out
if (!$iterator->valid()) {
continue;
}
//First iteration or higher value
if ($leading_iterator === null || call_user_func($this->sort_callback, $iterator->current(),
$leading_iterator->current())
) {
$leading_iterator = $iterator;
}
}
return $leading_iterator;
}
/**
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current()
{
$leading_iterator = $this->getLeadingIterator();
if ($leading_iterator !== null) {
return $leading_iterator->current();
}
return null;
}
/**
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next()
{
$leading_iterator = $this->getLeadingIterator();
if ($leading_iterator !== null) {
$leading_iterator->next();
}
}
/**
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key()
{
$leading_iterator = $this->getLeadingIterator();
if ($leading_iterator !== null) {
return $leading_iterator->key();
}
return null;
}
/**
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid()
{
//Only need one to be valid
foreach ($this->iterators as $iterator) {
if ($iterator->valid()) {
return true;
}
}
return false;
}
/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind()
{
//Will throw an exception if any have been started
foreach ($this->iterators as $iterator) {
$iterator->rewind();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment