brosua/enums

Helpers for PHP enums, especially for TYPO3 projects.

1.0.0 2025-03-21 13:23 UTC

This package is auto-updated.

Last update: 2025-03-21 13:24:57 UTC


README

Total Downloads Monthly Downloads

Enums

Installation

Install this extension via composer req brosua/enums.

Concept

This package provides helpers for PHP enums. It is designed with a special focus on TYPO3 projects.

URL
Repository: https://github.com/brosua/enums/

Usage

Simply apply the needed trait on your enum.

Base

Base includes all available traits.

use Brosua\Enums\Base;

enum MyEnum: int
{
    use Base;

    case One = 1;
    case Two = 1;

    protected static function getLanguageFilePath(): string
    {
        return 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:myEnum.';
    }
}

Now you can use all functions described for the following traits.

Translation

use Brosua\Enums\Translation;

enum MyEnum: int
{
    use Translation;

    case One = 1;
    case Two = 1;

    protected static function getLanguageFilePath(): string
    {
        return 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:myEnum.';
    }
}

Now you can use the following functions:

MyEnum::One->getLabel() // => 'translation1'
MyEnum::One->getTranslationString() // => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:myEnum.One'

Options

use Brosua\Enums\Options;

enum MyEnum: int
{
    use Options;

    case One = 1;
    case Two = 1;
}

Now you can use the following functions:

MyEnum::getOptions() // => [1 => 'translation1', 2 => 'translation2']

TcaOptions

use Brosua\Enums\TcaOptions;

enum MyEnum: int
{
    use TcaOptions;

    case One = 1;
    case Two = 1;

    protected static function getLanguageFilePath(): string
    {
        return 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:myEnum.';
    }
}

Now you can use the following functions:

MyEnum::getTcaOptions() // => [['translation1', 1], ['translation2', 2]]

From

use Brosua\Enums\From;

enum MyEnum: int
{
    use From;

    case One = 1;
    case Two = 1;
}

Now you can use the following functions:

MyEnum::fromName('One') // => Enum instance One