Created
July 30, 2018 23:43
-
-
Save jackdpeterson/5156e15f96778104496149250f33d4d8 to your computer and use it in GitHub Desktop.
Sample immutable Typed collection
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 | |
final class ImageCollection implements \Iterator, \JsonSerializable, \Countable | |
{ | |
private $data; | |
public function __construct(array $values) | |
{ | |
$this->data = (function (Image ...$values) { | |
return $values; | |
}) (...$values); | |
} | |
/** | |
* @return Image|boolean | |
*/ | |
public function current() | |
{ | |
return current($this->data); | |
} | |
public function next() | |
{ | |
$var = next($this->data); | |
return $var; | |
} | |
public function key() | |
{ | |
return key($this->data); | |
} | |
public function valid(): bool | |
{ | |
$key = key($this->data); | |
return ($key !== null && $key !== false); | |
} | |
public function rewind() | |
{ | |
reset($this->data); | |
} | |
public function jsonSerialize() | |
{ | |
$out = []; | |
/** | |
* @var Image $datum | |
*/ | |
foreach ($this->data as $datum) { | |
array_push($out, $datum->jsonSerialize()); | |
} | |
return $out; | |
} | |
public function count() | |
{ | |
return (int)count($this->data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment