Skip to content

Instantly share code, notes, and snippets.

@DaveRandom
Created May 27, 2016 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveRandom/352e6ed656c0fb8c9a5088ba6705aede to your computer and use it in GitHub Desktop.
Save DaveRandom/352e6ed656c0fb8c9a5088ba6705aede to your computer and use it in GitHub Desktop.
<?php
class Collection implements \Iterator
{
/**
* @var Room[][]
*/
private $rooms = [];
/**
* @return Room|false
*/
public function current()
{
if (null === $key = key($this->rooms)) {
return false;
}
return current($this->rooms[$key]);
}
public function next()
{
if (null === $key = key($this->rooms)) {
return;
}
if (next($this->rooms[$key]) !== false) {
return;
}
next($this->rooms);
if (null === $key = key($this->rooms)) {
return;
}
reset($this->rooms[$key]);
}
/**
* @return Identifier|null
*/
public function key()
{
if (false === $room = $this->current()) {
return null;
}
return $room->getIdentifier();
}
public function valid()
{
return $this->current() !== false;
}
public function rewind()
{
reset($this->rooms);
if (null !== $key = key($this->rooms)) {
reset($this->rooms[$key]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment