Created
April 20, 2018 13:59
-
-
Save kobus1998/d235d914f80ae588472b64b7314433a1 to your computer and use it in GitHub Desktop.
Iterator
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 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