Skip to content

damianulan/php-enumerable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PHP Enumerable

Static Badge   Licence   Static Badge

Description

This package provides extensive enumeration support for PHP and Laravel projects, as a substitute for PHP built-in enums.

Installation

You can install the package via composer in your laravel project:

composer require damianulan/php-enumerable

The package will automatically register itself.

Usage

Use Enum class to create enumeration in plain php, in laravel project use LaraEnum class, which provides additional casting support. Class Enum instances mimic implementation of BackedEnum and UnitEnum.

use Enumerable\Enum;

class CampaignStage extends Enum
{
    public const PENDING = 'pending';

    public const IN_PROGRESS = 'in_progress';

    public const COMPLETED = 'completed';
}

Check the examples below:

StageEnum::cases(); // returns an assoc array of all enum cases with enum instance
StageEnum::values(); // returns an array of all enum values
StageEnum::labels(); // should return an assoc array of all enum cases with human-readable label. This method should be declared in Enum child class. Accessible by attribue `label`.
// ['enum_value' => 'Human-readable label']

$stage = StageEnum::fromValue('pending'); // returns enum instance
$stage->label; // returns human-readable label if labels() method is declared in child class
$stage->value; // returns enum value

StageEnum::Pending; // returns enum value, not its instance

In Laravel assign your LaraEnum to yout model's casts property:

protected $casts = array(
    'stage' => StageEnum::class,
);

It will return StageEnum instance instead of string when accessing model's stage property.

More examples

$stage->is('pending'); // returns true if enum value is equal to given string
$stage->isNot('pending'); // returns true if enum value is not equal to given string
$stage->equals($otherEnum); // returns true if enum value is equal to given enum instance

Contact & Contributing

Any question You can submit to damian.ulan@protonmail.com.

About

Extensive enum classes for plain PHP and Laravel projects

Resources

License

Stars

Watchers

Forks

Languages