Last active
February 4, 2016 07:56
-
-
Save mihaeu/559229b3e6cf384cedf4 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 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