Created
June 30, 2014 20:25
-
-
Save blar/27eb519e2e8a8e463e5c to your computer and use it in GitHub Desktop.
Collection::map()
This file contains hidden or 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 | |
| class Collection extends ArrayObject { | |
| /** | |
| * $callback(mixed $value, mixed $key, Iterator $iterator) | |
| */ | |
| public function map(Closure $callback) { | |
| $iterator = $this->getIterator(); | |
| $result = new static(); | |
| iterator_apply($iterator, function($iterator) use($callback, $result) { | |
| $value = $iterator->current(); | |
| $key = $iterator->key(); | |
| if($status = $callback($value, $key, $iterator)) { | |
| $result[$key] = $value; | |
| } | |
| return $status; | |
| }, array($iterator)); | |
| return $result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment