Skip to content

omaradel-tech/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Enum Package

Latest Version License

A Laravel package that provides an easy and efficient way to work with enums in PHP, enhancing code readability and maintainability.

Features

  • Enum Definition: Define enums using a straightforward syntax.
  • Value Retrieval: Retrieve enum values seamlessly.
  • Key Retrieval: Access enum keys with ease.
  • Array Conversion: Convert enums to arrays for flexible usage.
  • Validation Rules: Integrate enum validation rules within Laravel's validation system.
  • Localization Support: Support for translating enum values using Laravel's localization features.

Installation

Install the package via Composer:

composer require omaradel-tech/enum

Usage

Defining an Enum

Create a new enum by extending the Enum class:

use OmarAdel\Enum\Enum;

class UserRole extends Enum
{
    const ADMIN = 'admin';
    const EDITOR = 'editor';
    const VIEWER = 'viewer';

    public static $langPath = 'enum::user_roles';
}

Retrieving Enum Values

Access the value of an enum:

$adminRole = UserRole::ADMIN; // 'admin'

Retrieving Enum Keys

Get the key associated with a specific value:

$key = UserRole::ADMIN()->getKey(); // 'ADMIN'

Converting Enum to Array

Convert the enum to an associative array:

$array = UserRole::toArray();
/*
[
    'ADMIN' => 'admin',
    'EDITOR' => 'editor',
    'VIEWER' => 'viewer',
]
*/

Validation

Use the enum in Laravel's validation rules:

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

public function store(Request $request)
{
    $request->validate([
        'role' => ['required', Rule::in(UserRole::values())],
    ]);

    // ...
}

Localization

To localize enum values, add translations in your /lang/vendor/enum files:

resources/lang/vendor/enum/en/user_roles.php:

return [
    'admin' => 'Administrator',
    'editor' => 'Editor',
    'viewer' => 'Viewer',
];

Then, retrieve the localized value:

$translated = UserRole::getLabel(UserRole::ADMIN); // 'Administrator'

To publish the translations files:

php artisan vendor:publish --tag=enum-translations

Predefined Classes:

1- BaseUserEnum Class:

use OmarAdel\Enum\Enum;

class BaseUserEnum extends Enum
{
    const ADMIN = 'Admin';
    const USER = 'User';
}

2- BaseStatusEnum Class:

use OmarAdel\Enum\Enum;

class BaseStatusEnum extends Enum
{
    const DRAFT = 'Draft';
    const PENDING = 'Pending';
    const PUBLISH = 'Published';
}

3- BaseUserStatusEnum Class:

use OmarAdel\Enum\Enum;

class BaseUserStatusEnum extends Enum
{
     const ACTIVE = 'Active';
    const IN_ACTIVE = 'In-Active';
}

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss improvements or features.

License

This package is open-sourced software licensed under the MIT license.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages