Skip to content

Instantly share code, notes, and snippets.

@drock
Created July 26, 2013 12:04
Show Gist options
  • Save drock/6088355 to your computer and use it in GitHub Desktop.
Save drock/6088355 to your computer and use it in GitHub Desktop.
A PHP class to mimick the php SPL-Types extension's SPLEnum but in vanilla PHP. Use to create an enumeration and guarantee that the set value is one of the possible enumerations.
<?php
/**
* Class Enum
*
* Encapsulates (as best as PHP can, Enums as a type)
* Extend the class and define constants for the valid values
*
* This class basically mimics SPLEnum from the SPL-Types PECL
* extension except in vanilla PHP so no extension required.
*
* Example:
*
* class MyEnum extends Enum {
* const FOO = 'foo';
* const BAR = 'bar';
* }
*
* $enum = new MyEnum(MyEnum::FOO);
*
* $enum = new MyEnum(99); //throws exception because not valid value
*
*/
abstract class Enum
implements \JsonSerializable
{
/**
* @var string
*/
protected $value;
/**
* @param mixed $value
*/
public function __construct($value)
{
$this->setValue($value);
}
/**
* @param mixed $value
*/
public function setValue($value)
{
if (!static::isValid($value))
{
throw new InvalidArgumentException(sprintf("Invalid enumeration %s for Enum %s", $value, get_class($this)));
}
$this->value = $value;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* Check if the set value on this enum is a valid value for the enum
* @return boolean
*/
public static function isValid($value)
{
if(!in_array($value, static::validValues()))
{
return false;
}
return true;
}
/**
* Get the valid values for this enum
* Defaults to constants you define in your subclass
* override to provide custom functionality
* @return array
*/
public static function validValues()
{
$r = new \ReflectionClass(get_called_class());
return array_values($r->getConstants());
}
/**
* @see JsonSerialize
*/
public function jsonSerialize()
{
return $this->getValue();
}
/**
* @return string
*/
public function __toString()
{
return (string)$this->getValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment