Created
February 4, 2016 07:56
-
-
Save mihaeu/42606f35bdba58481a5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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