Git Product home page Git Product logo

php-enum's Introduction

Enum

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads

A PHP 7+ enumeration library.

Why?

To create perfect enums for PHP library

Installation

composer require "paillechat/php-enum:^2.0"

Usage

Declare enum class by extending basic Enum and filling it with constants. Constant value does not matter. You can fill it with any payload you can utilize as general constant, but we suggest you to keep constants as protected as possible

<?php

use Paillechat\Enum\Enum;

/**
 * These docs are used only to help IDE
 * 
 * @method static static ONE
 * @method static static TWO
 */
class IssueType extends Enum 
{
    protected const ONE = 1;
    protected const TWO = 2;
} 

# Now you can create enum via named static call
/** @var Enum $one */
$one = IssueType::ONE();

# Enums keeps strict equality
$one1 = IssueType::ONE();
$one2 = IssueType::ONE();
$two = IssueType::TWO();

$one1 === $one2;
$one !== $two;

# Enums plays well with built-in functions
\in_array(IssueType::ONE(), [$one, $two], true);

# Enums plays well with signature type checks
function moveIssue(IssueType $type) {
    if ($type === IssueType::ONE()) {
        throw new \LogicException();
    }
    
    // ....
}

# You can convert enum to name and back
$name = $one->getName();
$new = IssueType::$name();

php-enum's People

Contributors

paillechat avatar scaytrase avatar

Stargazers

Carlos Alberto Costa Beppler avatar  avatar Chun-Sheng, Li avatar Anton Trekov avatar Nguyen Chi Duy Duc avatar Kangin Kim avatar Deryk w. King avatar  avatar Pavel Savelyev avatar Clicman avatar  avatar Daniel Sentker avatar Robert Akhmerov avatar aleksmt avatar Dmitriy Sergeev avatar Pavel avatar

Watchers

James Cloos avatar  avatar Dmitriy Sergeev avatar Pavel avatar  avatar Deryk w. King avatar

php-enum's Issues

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.