Coder Social home page Coder Social logo

enum's Introduction

Laravel Enum

A robust and flexible package for creating enumerations in Laravel. This package simplifies the creation and management of enumerated types, offering a consistent and extensible approach to handle options and their corresponding labels.

Features

  • Automatic Label Generation: Automatically generates human-readable labels from enumeration keys.
  • Custom Label Support: Allows for custom labels through a static values method.
  • Option Retrieval: Provides methods to retrieve all options, specific options by key, and check the existence of options.
  • Key and Label Extraction: Easily extract all keys or labels from the enumeration.

Installation

To install the package, use Composer:

composer require salekur/enum

Usage

Extend the Enum class and define constants for each enumerated value to create a new enumeration. Optionally, provide a values method to customize labels.

namespace Salekur\Enum\Enum;

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

    // optional values function
    public static function values(): array {
        return [
            self::ADMIN => 'Administrator',
            self::EDITOR => 'Content Editor',
            self::VIEWER => 'Content Viewer'
        ];
    }
}

Retrieving Options

The Enum class provides several methods to retrieve and interact with enumeration options. These methods allow you to access all enumeration options easily, specify option labels, check for the existence of an option, and extract keys and labels. Below are examples demonstrating how to use these methods with the UserRole enum.

$options = UserRole::options();
// ['admin' => 'Administrator', 'editor' => 'Content Editor', 'viewer' => 'Content Viewer']

$label = UserRole::get('admin');
// 'Administrator'

$exists = UserRole::has('editor');
// true

$keys = UserRole::keys();
// ['admin', 'editor', 'viewer']

$labels = UserRole::labels();
// ['Administrator', 'Content Editor', 'Content Viewer']

enum's People

Contributors

salekurpolas avatar maruf-abd avatar

Watchers

 avatar

Forkers

maruf-abd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.