Skip to content

Instantly share code, notes, and snippets.

@kiproping
Created July 21, 2017 14:17
Show Gist options
  • Save kiproping/b549985857ae1f73eb78029b7dd5a7c6 to your computer and use it in GitHub Desktop.
Save kiproping/b549985857ae1f73eb78029b7dd5a7c6 to your computer and use it in GitHub Desktop.
Enumerator
<?php
/**
* SageOne Library
*
* @category Library
* @package SageOne
* @author Brian Cline https://stackoverflow.com/a/254543/536434 / Brian Maiyo kiproping@github.com
* @license MIT <https://github.com/darrynten/sage-one-php/blob/master/LICENSE>
* @link https://github.com/darrynten/sage-one-php
*/
namespace DarrynTen\SageOne;
abstract class BasicEnum {
private static $constCacheArray = NULL;
private static function getConstants() {
if (self::$constCacheArray == NULL) {
self::$constCacheArray = [];
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value, $strict = true) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict);
}
public static function toString($value){
$constants = self::getConstants();
$flippedConstants = array_flip($constants);
return $flippedConstants[$value];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment