Skip to content

Instantly share code, notes, and snippets.

@zmiftah
Last active August 29, 2015 14:16
Show Gist options
  • Save zmiftah/92056f81e38358ed1ea6 to your computer and use it in GitHub Desktop.
Save zmiftah/92056f81e38358ed1ea6 to your computer and use it in GitHub Desktop.
Create Enumerable Class as Array
<?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;
}
}
<?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