Coder Social home page Coder Social logo

enum's Introduction

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

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.

Installation

composer require tebru/enum

Usage

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

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.

DirectionEnum::create('north');

You can also create an instance using the __callStatic magic method.

DirectionEnum::NORTH();

Add a hint to the enum doc block

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

Reference

There are multiple methods available on each enum

  • 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()

enum's People

Contributors

natebrunette avatar peter279k avatar

Stargazers

 avatar Jansen Price avatar Brian Litzinger avatar

Watchers

James Cloos avatar  avatar

Forkers

peter279k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.