Skip to content

AdamLutka/PhpEnum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP implementation of enum Build Status

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.