Skip to content

Instantly share code, notes, and snippets.

@mihaeu
Last active February 4, 2016 07:56
Show Gist options
  • Save mihaeu/559229b3e6cf384cedf4 to your computer and use it in GitHub Desktop.
Save mihaeu/559229b3e6cf384cedf4 to your computer and use it in GitHub Desktop.
<?php declare(strict_types = 1);
class XCollection implements Countable, IteratorAggregate
{
/**
* @var X[]
*/
private $x;
public function __construct()
{
$this->x = [];
}
public function add(X $x)
{
$this->x[] = $x;
}
public function remove(X $x)
{
$key = array_search($x, $this->x);
if ($key !== false) {
unset($this->x[$key]);
}
}
public function getIterator()
{
return new ArrayIterator(array_values($this->x));
}
public function count()
{
return count($this->x);
}
public function sort()
{
uasort($this->x, function ($elementA, $elementB) {
return $elementA->y() <=> $elementB->y();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment