Created
February 3, 2014 12:42
-
-
Save pedroborges/8783186 to your computer and use it in GitHub Desktop.
An alternative to ENUM in DB.
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
// Taken from: https://laracasts.com/forum/88-how-do-you-handle-enums | |
class OrderStatus implements Enum | |
{ | |
const FRAUD = -2; | |
const CANCELLED = -1; | |
const UNPAID = 0; | |
const RECEIVED = 1; | |
const PARTIALLY_SHIPPED = 10; | |
const SHIPPED = 20; | |
public static function getCode($label) | |
{ | |
$class = new ReflectionClass( get_class() ); | |
$constants = $class->getConstants(); | |
return $constants[$label]; | |
} | |
public static function getLabel($code) | |
{ | |
$class = new ReflectionClass( get_class() ); | |
$constants = $class->getConstants(); | |
$constants = array_flip($constants); | |
return $constants[$code]; | |
} | |
} | |
interface Enum { | |
public static function getCode($label); | |
public static function getLabel($code); | |
} | |
var_dump( OrderStatus::getCode('CANCELLED') ); // Returns -1 | |
var_dump( OrderStatus::getLabel(-1) ); // Returns CANCELLED |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment