tkachikov/enum-values

Set methods for get values and keys from a enum

0.0.1 2023-09-05 08:30 UTC

This package is auto-updated.

Last update: 2025-03-05 11:30:12 UTC


README

Description

This PHP package provides a trait EnumValuesTrait that adds utility methods for working with enumerations (enums).

Installation

To install the package, run the following command:

composer require tkachikov/enum-values

Usage

Include the trait into your enum class:

<?php

namespace App\Enums;

use Tkachikov\EnumValues\EnumValuesTrait;

class StateEnum: int
{
    use EnumValuesTrait;

    const ACTIVE = 10;

    const BLOCKED = 20;
}

Now you can use the methods getKeys(), getValues(), getByKey() and getByValue() for interacting with the enum:

$values = StateEnum::getValues();           // [10, 20]

$keys = StateEnum::getKeys();               // ['ACTIVE', 'BLOCKED']

$arrayByKey = StateEnum::getByKey();        // ['ACTIVE' => 10, 'BLOCKED' => 20]

$arrayByValue = StateEnum::getByValue();    // [10 => 'ACTIVE', 20 => 'BLOCKED']

Author

Alex Tkachikov Email: a.tkachikov@mail.ru

License

MIT