2017 © Pedro Peláez
 

library enum

Simple enum library for PHP

image

tebru/enum

Simple enum library for PHP

  • Friday, July 13, 2018
  • by natebrunette
  • Repository
  • 1 Watchers
  • 3 Stars
  • 444 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight, (*1)

Enum

A simple PHP library to add support for enums. This requires slightly more work than myclabs/php-enum, but does not require reflection. It also forces enums to be singletons., (*2)

Installation

composer require tebru/enum

Usage

To use, extend AbstractEnum and implement the getConstants() method., (*3)

class DirectionEnum extends AbstractEnum
{
    const NORTH = 'north';
    const EAST = 'east';
    const SOUTH = 'south';
    const WEST = 'west';

    /**
     * Return an array of enum class constants
     *
     * @return array
     */
    public static function getConstants()
    {
        return [
            self::NORTH,
            self::EAST,
            self::SOUTH,
            self::WEST,
        ];
    }
}

Now you can create a new instance using the static method., (*4)

DirectionEnum::create('north');

You can also create an instance using the __callStatic magic method., (*5)

DirectionEnum::NORTH();

Add a hint to the enum doc block, (*6)

/**
 * @method static $this NORTH()
 */

Reference

There are multiple methods available on each enum, (*7)

  • create() [static] Returns an instance of the enum
  • values() [static] A 0-indexed array of all of the enum values
  • exists($value) [static] Returns true if the value exists
  • toArray() [static] Returns a hash with keys and values as the enum values
  • equals($enum) Performs a strict comparison of two enum values
  • getValue() Returns the current value of the enum
  • __toString() Same as getValue()

The Versions

13/07 2018

dev-master

9999999-dev

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

03/02 2017

v0.5.0

0.5.0.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

07/01 2017

v0.4.1

0.4.1.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

06/01 2017

v0.4.0

0.4.0.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

15/09 2016

v0.3.0

0.3.0.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

15/09 2016

v0.2.1

0.2.1.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

01/12 2015

v0.2.0

0.2.0.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette

30/11 2015

v0.1.0

0.1.0.0

Simple enum library for PHP

  Sources   Download

MIT

The Requires

  • php >= 5.4

 

The Development Requires

by Nate Brunette