Skip to content

Instantly share code, notes, and snippets.

@pedroborges
Created February 3, 2014 12:42
Show Gist options
  • Save pedroborges/8783186 to your computer and use it in GitHub Desktop.
Save pedroborges/8783186 to your computer and use it in GitHub Desktop.
An alternative to ENUM in DB.
// 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