GithubHelp home page GithubHelp logo

typed-enum's Introduction

Typed Enumerations

Typed-enum is A simple, lightweight and efficient enumeration library. Type hint your enumerated options efficiently and strictly to eliminate unintended side effects and bugs introduced by typos.

How to use:

Download

Download with composer:

Extending from TypedEnum

Extend from TypedEnum and use any scalar constants to define the enumeration:

final class Foo extends TypedEnum {
    private const BAR = 'bar';
    private const BAZ = 2;
    private const FOO = 2.1;
} 

You can now create instances of Foo by using the __callstatic method based on the name of the constant:

Foo::BAR();  // Returns an instance of Foo with value 'bar'

Testing for equality

As a bonus, you can now use strict comparisons. TypedEnum guarantees there to be only one instance of the same enumerated value at runtime.

function isBar(Foo $enum): bool {
    return Foo::BAR() === $enum;     
}

Return the actual value:

If your application depends on the value assigned to the enumeration, you can easily fetch it with the getValue() getter.

echo Foo::BAR()->getValue(); //'bar'

Resolve the enumeration

Resolve an enumeration based on its value. The resolve method will return all enumerations with the same value in the array.

echo Foo::resolve('bar')[0] === Foo::BAR() // true 

Tips

IDE integration

You can easily use the power of you ide by simply adding @method tags in the docblock of your class like this:

/**
 * @method static TypedEnum TEST()
 */
final class Foo extends TypedEnum {
    private const TEST = 'test';
}

Psalm

We built TypedEnum with psalm! With these powerful annotations, you can now hint the scalar value of the enumeration:

/**
 * @extends TypedEnum<string>
 */
final class Foo extends TypedEnum {
    private const TEST = 'test';
}

Easy Bug protection

Php 7.2 allows for protected constants, and 7.4 uses private ones. Use these features to protect against unintended side effects!

echo Foo::BAR === Foo::BAR() // echo's false but is impossible if it is a protected constant

Developed by laudis

typed-enum's People

Contributors

ghlen avatar tim-hanssen avatar transistive avatar

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.