PHP code example of tezmanian / utils-enum

1. Go to this page and download the library: Download tezmanian/utils-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tezmanian / utils-enum example snippets


use Tezmanian\Utils\Enum\EnumTrait;

enum Status
{
    use EnumTrait;

    case Draft;
    case Published;
    case Archived;
}

Status::find('Published');              // Status::Published
Status::find('published', ignoreCase: true); // Status::Published
Status::Published->is('Published');     // true
Status::Published->is(Status::Published); // true
Status::Published->isOneOf([Status::Draft, Status::Published]); // true
Status::Published->isNoneOf([Status::Archived]); // true

Status::Published->ordinal(); // 1
Status::first();              // Status::Draft
Status::last();               // Status::Archived

Status::filter(fn($c) => $c !== Status::Archived); // [Draft, Published]
Status::toArray(); // ['Draft' => 'Draft', 'Published' => 'Published', 'Archived' => 'Archived']

use Tezmanian\Utils\Enum\BackedEnumTrait;

enum Color: string
{
    use BackedEnumTrait;

    case Red   = 'red';
    case Green = 'green';
    case Blue  = 'blue';
}

Color::find('red');           // Color::Red  — matched by value
Color::find('Red');           // Color::Red  — matched by name
Color::find('RED', ignoreCase: true); // Color::Red

Color::hasValue('blue');      // true
Color::values();              // ['red', 'green', 'blue']
Color::fromName('Green');     // Color::Green  (throws ValueError if not found)
Color::tryFromName('X');      // null

Color::toArray();             // ['red' => 'Red', 'green' => 'Green', 'blue' => 'Blue']
Color::toValueArray();        // ['Red' => 'red', 'Green' => 'green', 'Blue' => 'blue']

use Tezmanian\Utils\Enum\BitFlagTrait;

enum Permission: int
{
    use BitFlagTrait;

    case Read    = 1;
    case Write   = 2;
    case Execute = 4;
}

// Build a mask
$mask = Permission::combine(Permission::Read, Permission::Write); // 3

// Check flags
Permission::Read->isSetIn($mask);     // true
Permission::Execute->isSetIn($mask);  // false

// Add / remove flags
$mask = Permission::Execute->addTo($mask);      // 7
$mask = Permission::Write->removeFrom($mask);   // 5

// Expand a mask back to cases
Permission::fromBitmask($mask); // [Permission::Read, Permission::Execute]
Permission::fromBitmask(0);     // []

use Tezmanian\Utils\Enum\ComparableTrait;

enum Priority: int
{
    use ComparableTrait;

    case Low    = 1;
    case Medium = 2;
    case High   = 3;
}

Priority::High->isGreaterThan(Priority::Low);             // true
Priority::Medium->isGreaterThanOrEqual(Priority::Medium); // true
Priority::Medium->between(Priority::Low, Priority::High); // true
Priority::High->clamp(Priority::Low, Priority::Medium);   // Priority::Medium

use Tezmanian\Utils\Enum\StepTrait;

enum CheckoutStep
{
    use StepTrait;

    case PersonalInfo;
    case Address;
    case Payment;
    case Confirmation;
}

CheckoutStep::Address->next(); // CheckoutStep::Payment
CheckoutStep::Address->prev(); // CheckoutStep::PersonalInfo

CheckoutStep::Confirmation->next(); // null  (last step)
CheckoutStep::PersonalInfo->prev(); // null  (first step)

use Tezmanian\Utils\Enum\TranslatableTrait;

enum UserRole
{
    use TranslatableTrait;

    case Admin;
    case Editor;
    case Viewer;

    protected static function labels(): array
    {
        return [
            self::Admin->name  => 'Administrator',
            self::Editor->name => 'Content Editor',
            // Viewer has no entry — falls back to 'Viewer'
        ];
    }
}

UserRole::Admin->label();   // 'Administrator'
UserRole::Viewer->label();  // 'Viewer'  (fallback)

UserRole::labelsArray();
// ['Admin' => 'Administrator', 'Editor' => 'Content Editor', 'Viewer' => 'Viewer']

use Tezmanian\Utils\Enum\EnumCollectionTrait;

enum Suit
{
    use EnumCollectionTrait;

    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

Suit::except(Suit::Hearts, Suit::Diamonds); // [Suit::Clubs, Suit::Spades]
Suit::only(Suit::Clubs, Suit::Spades);      // [Suit::Clubs, Suit::Spades]
Suit::random();                             // a random Suit case

// Full-featured backed enum
enum Status: string
{
    use BackedEnumTrait, TranslatableTrait, EnumCollectionTrait, StepTrait;

    case Draft     = 'draft';
    case Review    = 'review';
    case Published = 'published';

    protected static function labels(): array
    {
        return [
            self::Draft->name     => 'Draft',
            self::Review->name    => 'In Review',
            self::Published->name => 'Published',
        ];
    }
}

Status::Draft->next();               // Status::Review
Status::Review->label();             // 'In Review'
Status::except(Status::Draft);       // [Status::Review, Status::Published]
Status::find('draft');               // Status::Draft

// Int-backed enum with bitmask + comparison
enum Permission: int
{
    use BackedEnumTrait, BitFlagTrait, ComparableTrait;

    case Read    = 1;
    case Write   = 2;
    case Execute = 4;
}