uginroot/php-enum

Php enum type

v2.6 2020-06-02 04:34 UTC

This package is auto-updated.

Last update: 2024-05-29 04:34:58 UTC


README

Install

composer require uginroot/php-enum:^2.3

Using

use Uginroot\PhpEnum\Choice;
use Uginroot\PhpEnum\EnumAbstract;

class Month extends EnumAbstract {
    public const January = 1;
    public const February = 2;
}

$jun = new Month(Month::January);
$junName = Month::createByName('January');
$junValue = Month::createByValue(Month::January);

$jun->getValue(); // 1
$jun->getName();  // 'January'
$name = (string)$jun; // 'January'

$jun->is($junName);            // true
$jun->is($junValue);           // true
$jun->isName('January');       // true
$jun->isValue(Month::January); // true

// Immutable
$jun->setValue(Month::February); // new Month(Month::February)
$jun->setName('February'); // Month::createByName('February')

// Name and value variants
/** @var Choice $choice */
$choice = Month::getChoice();
$choice->getValues(); // [1, 2]
$choice->getNames();  // ['January', 'February']
$choice->getValue('January'); // 1
$choice->getName(Month::January); // 'January'
$choice->isValidName('January'); // true
$choice->isValidValue(Month::January); // true