Simplified and enhanced version of php built-in enum.

Overview

PHP Enum enhanced

Version Downloads count Repository count Last commit Stars count

Finally, in php81 has been added support for Enums. But as enums are new in php, we do not have some helpers to work with that easily. So with our enum-pro package you have pretty more options for working with enums than built-in methods.

It is just trait Lazerg\LaravelEnumPro\EnumPro which must be added into enum. So it means it use built-in enum class while enhancing it

Installation

composer require lazerg/laravel-enum-pro

Usage

Create a new enum class and use our trait on it.

enum LevelTypes: int {
    use \Lazerg\LaravelEnumPro\EnumPro;

    case VERY_EASY = 1;
    case EASY = 2;
    case MEDIUM = 3;
    case STRONG = 4;
    case VERY_STRONG = 5
}

Calling

With default functions, if you want to get value of case you should write LevelTypes::VERY_EASY->value which is little long. With our package, you can get value of case, by just calling it statically

LevelTypes::VERY_EASY() // 1

Names

As you can see, names here VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG. To get all case names of enum. you can use these helper methods:

LevelTypes::names();         // Collection: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToArray();  // Array: ['VERY_EASY', 'EASY', 'MEDIUM', 'STRONG', 'VERY_STRONG']
LevelTypes::namesToString(); // String: VERY_EASY, EASY, MEDIUM, STRONG, VERY_STRONG

Values

As you can see, values here 1, 2, 3, 4, 5. Common usage is for: validate incoming request data.

LevelTypes::values();         // Collection: [1, 2, 3, 4, 5]
LevelTypes::valuesToArray();  // Array: [1, 2, 3, 4, 5]
LevelTypes::valuesToString(); // String: 1, 2, 3, 4, 5

Randomize

Sometimes we need to get random value or values from enum. This is mainly used in factories.

LevelTypes::random(int $count = 1);      // Collection of $count random values
LevelTypes::randomArray(int $count = 1); // Array of $count random values
LevelTypes::randomFirst();               // One random value

Options

While creating admin panel, we always change state of models. And basically, it is recommended to save all state types in enum. So in admin panel we need to get all options of enum for select. That's why we have options() method.

LevelTypes::options(); // Return collection of selectable options
LevelTypes::optionsToArray(); // Return array of selectable options

Example of options:

Illuminate\Support\Collection {#7777
    #items: array:5 [
        1 => "Very Easy"
        2 => "Easy"
        3 => "Medium"
        4 => "Strong"
        5 => "Very Strong"
    ]
}
You might also like...
The light version of NexoPOS 4.x, which is a web-Based Point Of Sale (POS) System build with Laravel, TailwindCSS, and Vue.Js.
The light version of NexoPOS 4.x, which is a web-Based Point Of Sale (POS) System build with Laravel, TailwindCSS, and Vue.Js.

About NexoPOS 4.x NexoPOS 4 is a free point of sale system build using Laravel, TailwindCSS, Vue and other open-source resources. This POS System focu

A highly compressed version of the magento 1.9 sample data and a script to create it.

Compressed Magento 1.9 Sample Data The following variations are available: 65M compressed-magento-sample-data-1.9.2.4.tgz 64M compressed-magento-sampl

A PHP library for creating EDI 837 claim equivalent to paper-claim 1500. EDI X12 ANSI 837 File 5010 Version

EDI X12 ANSI 5010 PHP Library for creating EDI X12 ANSI 837 File 5010 Version A Simple PHP function for creating an EDI X12 ANSI 837 file version 0050

An improved version of the PHP port of KuzuhaScript

KuzuhaScriptPHP+ (くずはすくりぷとPHP+) An improved version of the PHP port of KuzuhaScript (くずはすくりぷと). To my knowledge, it works with PHP version 4.1.0 and a

Check if files are compatible with X version of PHP.

grumphp-php-compatibility Check if files are compatible with X version of PHP. grumphp.yml: parameters: tasks: php_compatibility:

RMT is a handy tool to help releasing new version of your software
RMT is a handy tool to help releasing new version of your software

RMT - Release Management Tool RMT is a handy tool to help releasing new versions of your software. You can define the type of version generator you wa

Better Version of Cvolton's GMDprivateServer

BetterCvoltonGDPS Better Version of Cvolton's GMDprivateServer Supported version of Geometry Dash: 1.0 - 2.11 (so any version of Geometry Dash works,

Lite version of STY, Super Typecho
Lite version of STY, Super Typecho

STY Lite Lite version of STY, Super Typecho 因为STY的正确策略,对于主题部件的修改非常容易,这是在几个小时内诞生的Lite版本 STY lite版本,这是体验版本,体验版本也有一个特殊的名字:stylite -- 修行者 带着开源,修行吧! 关于主题 S

A Backwards Multi Version plugin for PocketMine-MP (API3)

MultiVersion A Backwards Multi Version plugin for PocketMine-MP (API3). This plugin is currently in development. Supported Versions 1.17.30 1.17.40 TO

Comments
  • No support for Laravel 8.*

    No support for Laravel 8.*

    I am in need of this package, we are integrating the new enums in a project but the truth is that they are quite basic. I am currently working on Laravel version 8.*, I believe that the implemented functionalities do not explicitly require Laravel 9. Are you planning to support lower Laravel versions?

    Using version ^0.1.1 for lazerg/laravel-enum-pro
    ./composer.json has been updated
    Running composer update lazerg/laravel-enum-pro
    Loading composer repositories with package information
    Updating dependencies
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Root composer.json requires lazerg/laravel-enum-pro ^0.1.1 -> satisfiable by lazerg/laravel-enum-pro[v0.1.1].
        - lazerg/laravel-enum-pro v0.1.1 requires illuminate/support ^9.0 -> found illuminate/support[v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require.
    
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require lazerg/laravel-enum-pro:*" to figure out if any version is installable, or "composer require lazerg/laravel-enum-pro:^2.1" if you know which you need.
    
    enhancement 
    opened by Daespinosag 2
  • randomFirst method return string

    randomFirst method return string

    PHP version: ^8.1 Laravel version: 9.2 Package version: 0.3

    I've created enum:

        case SM = " 10-13 ";
        case MD = " 14-19 ";
        case LG = " 20-26 ";
        case XL = " 28 ";
    

    I can't get the first random element, because in your randomFirst() method return type is Integer

    bug enhancement 
    opened by adiasrim 1
  • fixed valuesToString() method;

    fixed valuesToString() method;

    PHP version: ^8.1 Laravel version: 9.2 Package version: 0.3.1

    my enum looks:

        case SM = "10-13";
        case MD = "14-19";
        case LG = "20-26";
        case XL = "28";
    

    When I used valuesToString() method in my request file, this method returned values with space as ["10-13, 14-19, 20-26, 28"];

    But for in: validation, we need values without any spaces.

    I've tried to solve this bug. Can you check my commits, if everything is okay, merge it into new updates?

    opened by adiasrim 0
Owner
Lazizbek Ergashev
Freelancer | Fullstack Software Engineer | 20 y.o
Lazizbek Ergashev
This library provides a collection of native enum utilities (traits) which you almost always need in every PHP project.

This library provides a collection of native enum utilities (traits) which you almost always need in every PHP project.

DIVE 20 Nov 11, 2022
Simple opinionated framework agnostic PHP 8.1 enum helper

Enum Helper A simple and opinionated collections of PHP 8.1 enum helpers inspired by archtechx/enums and BenSampo/laravel-enum. This package is framew

Datomatic 52 Jan 1, 2023
Simple opinionated PHP 8.1 enum helper for Laravel

Laravel Enum Helper This is an extension of the datomatic/enum-helper package based on Laravel Framework. The package consists on a LaravelEnumHelper

Datomatic 17 Jan 1, 2023
Enhanced Yii2 wrapper for the bootstrap timepicker plugin

yii2-widget-timepicker The TimePicker widget allows you to easily select a time for a text input using your mouse or keyboards arrow keys. The widget

Kartik Visweswaran 40 Apr 12, 2022
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

Ernest Buffington 7 Aug 28, 2022
Version is a library that helps with managing the version number of Git-hosted PHP projects

Version Version is a library that helps with managing the version number of Git-hosted PHP projects. Installation You can add this library as a local,

Sebastian Bergmann 6.3k Dec 26, 2022
This is the US hardened version of PHP-Nuke Titanium and is secure and safe

This is the US hardened version of PHP-Nuke Titanium and is secure and safe. Built on PHP Version 7.4.30 - Forums - Blogs - Projects - Advanced Site Map - Web Links - Groups - Advanced Theme Support - Downloads - Advertising - Network Advertising - Link Back System - FAQ - Bookmark Vault - Private Virtual Cemetery - Loan Amortization - Image Hosting

Ernest Allen Buffington (The Ghost) 12 Dec 25, 2022
Get mobile app version and other related data from Google Play Store, Apple App Store and Huawei AppGallery

Mobile App Version Get mobile app version and other related data from Google Play Store, Apple App Store and Huawei AppGallery. Installation Add to co

Omer Salaj 11 Mar 15, 2022
Audit your PHP version for known CVEs and patches

PHP Version Audit PHP Version Audit is a convenience tool to easily check a given PHP version against a regularly updated list of CVE exploits, new re

Daniel 103 Dec 19, 2022
Exploiting and fixing security vulnerabilities of an old version of E-Class. Project implemented as part of the class YS13 Cyber-Security.

Open eClass 2.3 Development of XSS, CSRF, SQLi, RFI attacks/defences of an older,vulnerable version of eclass. Project implemented as part of the clas

Aristi_Papastavrou 11 Apr 23, 2022