Skip to content

Instantly share code, notes, and snippets.

@blar
Created June 30, 2014 20:25
Show Gist options
  • Select an option

  • Save blar/27eb519e2e8a8e463e5c to your computer and use it in GitHub Desktop.

Select an option

Save blar/27eb519e2e8a8e463e5c to your computer and use it in GitHub Desktop.
Collection::map()
<?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