Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created April 20, 2018 13:59
Show Gist options
  • Save kobus1998/d235d914f80ae588472b64b7314433a1 to your computer and use it in GitHub Desktop.
Save kobus1998/d235d914f80ae588472b64b7314433a1 to your computer and use it in GitHub Desktop.
Iterator
<?php
class Iter {
private $arr;
private $result;
public function __construct($array) {
$this->arr = $array;
}
public function get() {
return $this->result;
}
public function getCurrent() {
if ($this->result == null) return $this->arr;
else return $this->result;
}
public function filter($callback) {
$result = [];
foreach($this->getCurrent() as $item) {
if ($callback($item)) {
$result[] = $item;
}
}
$this->result = $result;
return $this;
}
public function map($callback) {
$result = [];
foreach($this->getCurrent() as $item) {
$result[] = $callback($item);
}
$this->result = $result;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment