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

Overview

πŸ›  Native enum utilities you always need

Latest Version on Packagist

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

Installation

You can install the package via composer:

composer require dive-be/php-enum-utils

Usage

Assume the following string backed enum:

enum Role: string
{
    case Administrator = 'admin';
    case Auditor = 'audit';
    case Moderator = 'mod';
}

Arrayable (backed enums only)

Allows you to retrieve a key-value pair of names and values:

Role::toArray(); // ['Administrator' => 'admin', 'Auditor' => 'audit', 'Moderator' => 'mod']

Assertable

This relies on the enum names being in PascalCase, which is also recommended by the official PHP docs.

Allows you to make assertions on enum instances using predicate functions:

$role = Role::Moderator;

$role->isAuditor(); // false
$role->isModerator(); // true

Comparable

Allows you to compare enums. Mostly useful when providing backed values:

$role = Role::Administrator;

$role->equals('admin'); // true
$role->equals(Role::Administrator); // true
$role->equals('mod'); // false
$role->equals(Role::Moderator); // false

$role->equalsAny(['admin', 'mod', 'audit']); // true
$role->equalsAny([Role::Administrator, Role::Auditor]); // true
$role->equalsAny([Role::Moderator, 'audit']); // false

NameListable

Allows you to retrieve a list of the enum names:

Role::toNames(); // ['Administrator', 'Auditor', 'Moderator']

ValueListable (backed enums only)

Allows you to retrieve a list of the enum values:

Role::toValues(); // ['admin', 'audit', 'mod']

WithUtilities (backed enums only)

Aggregator trait for all of the aforementioned utilities.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Simplified and enhanced version of php built-in enum.

PHP Enum enhanced 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 easil

Simple opinionated framework agnostic PHP 8.1 enum helper
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

Simple opinionated PHP 8.1 enum helper for Laravel
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

Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.
Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

Dispatcher Dispatcher allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deployi

Create easy (and almost magic) forms
Create easy (and almost magic) forms

Why Magic Forms? Almost everyday we do forms for our clients, personal projects, etc Sometimes we need to add or remove fields, change validations, st

Do you want CronJob to follow the solar date?You need this package to solve this problem.
Do you want CronJob to follow the solar date?You need this package to solve this problem.

Shamsic Maybe it happened to you that you wanted to use CronJob in your project and you realized that you cannot manage the exact dates that are in th

PHP 7+ Payment processing library. It offers everything you need to work with payments: Credit card & offsite purchasing, subscriptions, payouts etc. - provided by Forma-Pro

Supporting Payum Payum is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our

The last validation library you will ever need!
The last validation library you will ever need!

Mighty The last validation library you will ever need! Table of Contents Installation About Mighty Quickstart Mighty Validation Expression Language Ex

Traits used primarily in the v6 package but also available as a helper package for applications

Phalcon Traits This package contains traits with methods that are used for Phalcon v6 onward. It can also be useful to others that want short snippets

Comments
  • Arrayable Key By Backed Value

    Arrayable Key By Backed Value

    It would be nice to have an option to flip the array so we are using the PascalCase as the array's value and the backed value as the array's index.

    Also maybe an option to use something like Laravel's String Helper to turn the PascaleCase into a Headline

    opened by jjjrmy 1
Releases(1.1.0)
Owner
DIVE
DIVE
YCOM Impersonate. Login as selected YCOM user πŸ§™β€β™‚οΈin frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
Collection of useful PHP functions, mini-classes, and snippets for every day.

JBZoo / Utils Collection of PHP functions, mini classes and snippets for everyday developer's routine life. Install composer require jbzoo/utils Usage

JBZoo Toolbox 786 Dec 30, 2022
This is a Native PHP MVC. If you will build your own PHP project in MVC with router, you can clone this ready to use MVC pattern repo.

Welcome to PHP-Native-MVC-Pattern ?? If you will build your own PHP project in MVC with router, you can clone this ready to use MVC pattern repo. Work

null 2 Jun 6, 2022
SpawnInLobby Pocketmine-MP plugin. With this plugin the player will always join the game at the default world spawn point

SpawnInLobby Pocketmine-MP plugin. With this plugin the player will always join the game at the default world spawn point

null 1 Jan 7, 2022
Description: A simple plugin that sets the current admin page to always be at the top of the admin menu.

=== Sticky Admin Menu === Contributors: sc0ttkclark Donate link: https://www.scottkclark.com/ Tags: admin menu, sticky Requires at least: 4.4 Tested u

Scott Kingsley Clark 2 Sep 29, 2022
TEC UTilities (or tut) are a collection of tools for managing plugins.

TEC Utilities TEC UTilities (or tut) are a collection of tools for managing plugins. /^\ L L /

The Events Calendar 5 Dec 2, 2022
A collection of useful codes and utilities for WordPress plugin development..

WordPress Utils A collection of useful codes and utilities for WordPress plugin development. These simplifies common tasks and promote code reusabilit

weDevs 5 Jun 9, 2023
A collection of command-line utilities to aid in debugging browser engines.

Browser debug utilities This project contains several scripts that make the process of debugging browser engines much easier (some special cases excep

Clay Freeman 5 May 29, 2023
This library can be used, among other things, to retrieve the classes, interfaces, traits, enums, functions and constants declared in a file

marijnvanwezel/reflection-file Library that allows reflection of files. This library can be used, among other things, to retrieve the classes, interfa

Marijn van Wezel 5 Apr 17, 2022