Skip to content

Instantly share code, notes, and snippets.

@mihaeu
Created February 4, 2016 07:56
Show Gist options
  • Save mihaeu/42606f35bdba58481a5f to your computer and use it in GitHub Desktop.
Save mihaeu/42606f35bdba58481a5f to your computer and use it in GitHub Desktop.
<?php declare(strict_types = 1);
class XCollection implements Countable, IteratorAggregate
{
/**
* @var SplObjectStorage
*/
private $x;
public function __construct()
{
$this->x = new SplObjectStorage();
}
public function add(X $x)
{
$this->x->attach($x);
}
public function remove(X $x)
{
$this->x->detach($x);
}
public function getIterator()
{
return $this->x;
}
public function count()
{
return count($this->x);
}
public function sort()
{
$array = iterator_to_array($this->x);
uasort($array, function ($elementA, $elementB) {
return $elementA->y() <=> $elementB->y();
});
$this->x = new SplObjectStorage();
foreach ($array as $element) {
$this->x->attach($element);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment