adam-lutka/php-enum

PHP implementation of enum type

v1.0.2 2019-11-30 14:24 UTC

This package is auto-updated.

Last update: 2024-04-29 04:33:35 UTC


README

Enum types are represented by classes. Values of specific enum type are represented by @method annotations that enables code completion and doesn't cause duplicity.

<?php
require_once('vendor/autoload.php');

/**
 * @method static TypeEnum TYPE_1()
 * @method static $this TYPE_2()
 */
final class TypeEnum extends \AL\PhpEnum\Enum {}


var_dump((string)TypeEnum::TYPE_2());                        // string(6) "TYPE_2"
var_dump(TypeEnum::TYPE_2()->getValue());                    // string(6) "TYPE_2"
var_dump(TypeEnum::TYPE_1()->getOrder());                    // int(0)
var_dump(TypeEnum::TYPE_2()->getOrder());                    // int(1)
var_dump(TypeEnum::TYPE_2() === TypeEnum::TYPE_2());         // bool(true)
var_dump(TypeEnum::TYPE_2() == TypeEnum::TYPE_2());          // bool(true)
var_dump(TypeEnum::TYPE_1() === TypeEnum::TYPE_2());         // bool(false)
var_dump(TypeEnum::TYPE_1() == TypeEnum::TYPE_2());          // bool(false)
var_dump(TypeEnum::parse('TYPE_2') === TypeEnum::TYPE_2());  // bool(true)
var_dump(TypeEnum::tryParse('NOT_EXIST'));                   // NULL
var_dump(TypeEnum::inOrder(0) === TypeEnum::TYPE_1());       // bool(true)
var_dump(TypeEnum::tryInOrder(1000));                        // NULL

Getting Started

Prerequisites

The code needs PHP 7.1 or greater.

Installing

composer require adam-lutka/php-enum

Running the tests

composer install
vendor/bin/phpunit

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.