GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - tailflow/enum: PHP Enums

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

Latest Version on Packagist Build Status

Introduction

The package offers strongly typed enums in PHP. In this package, enums are always objects, never constant values on their own. This allows for proper static analysis and refactoring in IDEs.

Installation

You can install the package via composer:

composer require tailflow/enum

Usage

Here is how enums are defined:

use Tailflow\Enum\Enum;

class Status extends Enum
{
    public const Inactive = 0;
    public const Active = 1;
    public const OnHold = 3;
}

This is how they are used:

$class->setStatus(Status::Inactive);

Custom enum labels

By default, enum labels are derived from the constant names. To get enum label, you can use ::getLabel method on enum class:

// $label will be equal to "OnHold" - the name of the constant
$label = Status::getLabel(Status::OnHold); 

Optionally, you can provide a different label for any given enum value:

use Tailflow\Enum\Enum;

class Status extends Enum
{
    public const Inactive = 0;
    public const Active = 1;
    public const OnHold = 3;

    public static function labels(): array
    {
        return [
            self::OnHold => 'waiting'
        ];
    }
}

// $label will be equal to "waiting" - the custom label defined in "labels" method
$label = Status::getLabel(Status::OnHold); 

Listing all enum labels

To get a list of all labels of the enum, you can use ::getLabels method:

// $labels will contain the array - ['Inactive', 'Active', 'waiting']
$labels = Status::getLabels(); 

Listing all enum values

To get a list of all values of the enum, you can use ::getValues method:

// $labels will contain the array - [0, 1, 3]
$labels = Status::getValues(); 

License

The Laravel Orion is open-source software licensed under the MIT license.

About

PHP Enums

Resources

Readme
Activity
Custom properties

Stars

8 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.