Last active
August 29, 2015 14:16
-
-
Save zmiftah/92056f81e38358ed1ea6 to your computer and use it in GitHub Desktop.
Create Enumerable Class as Array
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 EnumerableData implements Iterator, Countable | |
{ | |
private $position = 0;private $count = 0; | |
private $keys = array(); | |
private $vars = array(); | |
public function __construct() | |
{ | |
$this->rewind(); | |
} | |
public function current() | |
{ | |
return $this->vars[$this->keys[$this->position]]; | |
} | |
public function key() | |
{ | |
return $this->keys[$this->position]; | |
} | |
public function next() | |
{ | |
$this->position++; | |
} | |
public function rewind() | |
{ | |
$this->position = 0; | |
$reflection = new ReflectionClass(get_class($this)); | |
$this->vars = $reflection->getConstants(); | |
$this->keys = array_keys($this->vars); | |
$this->count = count($this->vars); | |
} | |
public function valid() | |
{ | |
return $this->position < count($this->vars); | |
} | |
public function count() | |
{ | |
return $this->count; | |
} | |
public function toArray() | |
{ | |
$data = array(); | |
foreach ($this as $val=>$key) { | |
$data[$key] = $val; | |
} | |
return $data; | |
} | |
} |
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 ProposalTypeEnum extends EnumerableData | |
{ | |
const Customize = 1; | |
const Regular = 2; | |
const Charity = 3; | |
const Pemberdayaan = 4; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment