Created
March 13, 2020 20:33
-
-
Save DarkGhostHunter/7b4e47e5032011af50b76714324694ce to your computer and use it in GitHub Desktop.
A nice class that enumerates values
This file contains 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 Enumerate | |
{ | |
/** | |
* Current state of the enumerated object. | |
* | |
* @var string | |
*/ | |
protected $current; | |
/** | |
* Enumerate the possible states for this current instance. | |
* | |
* @var array | |
*/ | |
protected $states = []; | |
/** | |
* Create a new Enumerated instance with a list of available states. | |
* | |
* @param array|\ArrayAccess $states | |
*/ | |
public function __construct($states = []) | |
{ | |
if (! empty($states)) { | |
$this->states = $states; | |
} | |
if ($this->current) { | |
$this->__call($this->current, []); | |
} | |
} | |
/** | |
* Return the enumerated value. | |
* | |
* @return mixed | |
*/ | |
public function value() | |
{ | |
return array_key_exists($this->current, $this->states) | |
? $this->states[$this->current] | |
: $this->current; | |
} | |
/** | |
* Returns if the state exists. | |
* | |
* @param string $state | |
* @return bool | |
*/ | |
public function has(string $state) | |
{ | |
return array_key_exists($state, $this->states) || in_array($state, $this->states, true); | |
} | |
/** | |
* Returns if the current state is equal to the issued state. | |
* | |
* @param string $state | |
* @return bool | |
*/ | |
public function is(string $state) | |
{ | |
return $this->current === $state; | |
} | |
/** | |
* Return the current state. | |
* | |
* @return string | |
*/ | |
public function current() | |
{ | |
return $this->current; | |
} | |
/** | |
* Return the possible states of this instance. | |
* | |
* @return array|\ArrayAccess | |
*/ | |
public function states() | |
{ | |
return $this->states; | |
} | |
/** | |
* Handle dynamically setting the state. | |
* | |
* @param string $name | |
* @param array $arguments | |
* @return \DarkGhostHunter\Laratraits\Enumerate | |
* | |
* @throws \BadMethodCallException | |
*/ | |
public function __call($name, $arguments) | |
{ | |
if ($this->has($name)) { | |
$this->current = $name; | |
return $this; | |
} | |
throw new BadMethodCallException(sprintf( | |
'Call to undefined method %s::%s()', static::class, $name | |
)); | |
} | |
/** | |
* Transform the class instance into a string. | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->current ?? ''; | |
} | |
/** | |
* Creates a new Enumerate instance. | |
* | |
* @param array $states | |
* @param string|null $initial | |
* @return mixed | |
*/ | |
public static function from(array $states, string $initial = null) : self | |
{ | |
$instance = (new static($states)); | |
if ($initial) { | |
$instance->{$initial}(); | |
} | |
return $instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment