Installation
composer require adrenalinkin/enum-mapper
No additional configuration is required—just autoload the package.
Basic Usage
Define an enum-like class (e.g., UserRole.php):
use Adrenalinkin\EnumMapper\EnumMapper;
class UserRole extends EnumMapper
{
protected static $map = [
'admin' => 'Administrator',
'user' => 'Regular User',
'guest' => 'Guest',
];
protected static $default = 'user';
}
Use it in your application:
// Convert to human-readable
$role = UserRole::get('admin'); // Returns "Administrator"
// Convert back to database value
$dbValue = UserRole::getKey('Administrator'); // Returns "admin"
First Use Case Replace hardcoded strings in controllers/models with mapped values:
// Before
if ($user->role === 'admin') { ... }
// After
if (UserRole::get($user->role) === 'Administrator') { ... }
Database Integration Use with Eloquent models to enforce consistency:
class User extends Model
{
protected $casts = [
'role' => UserRole::class, // Automatically maps DB value to enum
];
}
Validation Leverage the mapper in Form Requests:
use Adrenalinkin\EnumMapper\Rules\ValidEnum;
public function rules()
{
return [
'role' => ['required', new ValidEnum(UserRole::class)],
];
}
API Responses Transform enum keys to human-readable labels in JSON:
return response()->json([
'role' => UserRole::get($user->role),
]);
class UserRole extends EnumMapper
{
protected static $map = [
'admin' => trans('roles.admin'),
// ...
];
}
class SiteStatus extends EnumMapper
{
protected static $map = [];
public static function setMap(array $map) {
static::$map = $map;
}
}
public function handle($request, Closure $next)
{
if (!UserRole::has('admin', $request->user()->role)) {
abort(403);
}
return $next($request);
}
Case Sensitivity
Database values must match enum keys exactly (e.g., 'ADMIN' ≠ 'admin'). Use strtolower() if needed:
UserRole::get(strtolower($dbValue));
Default Fallback
If $default is undefined and an invalid key is passed, get() returns null. Set a default:
protected static $default = 'user'; // Fallback to 'user' if invalid
Static Nature Avoid instantiating the mapper directly—all methods are static. ❌ Bad:
$role = new UserRole(); // Fails
has() to check validity before conversion:
if (!UserRole::has($dbValue)) {
throw new \InvalidArgumentException("Invalid role: {$dbValue}");
}
get() returns null, verify $map is populated and keys match.Custom Logic
Override get() or getKey() for business rules:
public static function get($key, $default = null)
{
if ($key === 'legacy_admin') {
return parent::get('admin', $default);
}
return parent::get($key, $default);
}
Database Storage
Store enum keys as integers (e.g., 1 for 'admin') by extending the mapper:
class NumericUserRole extends EnumMapper
{
protected static $numericMap = [
1 => 'admin',
2 => 'user',
];
public static function getKey($value) {
return array_search($value, static::$map, true) + 1;
}
}
Performance Cache mapped values in production:
$cachedRole = cache()->remember("role_{$dbValue}", 3600, fn() => UserRole::get($dbValue));
How can I help you explore Laravel packages today?