Giter VIP home page Giter VIP logo

enumerable.php's Introduction

Enumerable

Library provides support of enumerable classes for PHP.

Version Dev Version Downloads License Build Status

Installation

Install via composer:

composer require litgroup/enumerable:^0.7.0

Example of usage

Definition

  1. Create final class, which extends Enumerable;
  2. For each variant of values create a static method, which will creates an instance of value. For this purpose your method must call Enumerable::createEnum() with some index of value.

Note:

  • Enumerable class must be final!
  • Index can be of type string or int.

Enum definition example:

namespace Acme;

use LitGroup\Enumerable\Enumerable;

final class ColorEnum extends Enumerable
{
    /**
     * @return self
     */
    public static function red()
    {
        return self::createEnum('red');
    }

    /**
     * @return self
     */
    public static function green()
    {
        return self::createEnum('green');
    }

    /**
     * @return self
     */
    public static function blue()
    {
        return self::createEnum('blue');
    }
}

Equality/Identity checking

You can use enumerable values in equality/identity expressions:

ColorEnum::red() == ColorEnum::red() // => true
ColorEnum::red() === ColorEnum::red() // => true

ColorEnum::red() == ColorEnum::blue() // => false
ColorEnum::red() === ColorEnum::blue() // => false

Note: Enumerables works as runtime constants. Therefor enumerable values can be checked on identity. And we recommend to use check on identity (===) instesd of equality (==) if possible.

Usage in switch-case statement

$color = ColorEnum::green();

switch ($color) {
    case ColorEnum::red():
        echo "Red!\n";
        break;
    case ColorEnum::green():
        echo "Green!\n";
        break;
    case ColorEnum::blue():
        echo "Blue!\n";
        break;
}

// "Green!" will be printed

Serialization and Persistence

Enumerable works as runtime-constant. Enumerable type cannot be serialized. If you need to store representation of enumerable in a database or send it via an API you can use index of enumerable value as representation.

$enum->getRawValue();

To restore an instance of enumerable type by index from database or from API-request you can use static method getValueOf() on the concrete enum-class.

$colorIndex = getFromDatabase(/* something */);

$enum = ColorEnum::getValueOf($colorIndex);

If you need to get all values of enumerable type, use static method getValues() on the concrete enum-class.

ColorEnum::getValues(); // => Returns array of ColorEnum with index as key

Extensibility

Instances of your enumerable classes can have additional behaviour if it needed. But you cannot define any public static methods with behaviour. Public static methods used only for creation of values.

Note: You cannot define any public static methods with behaviour. Public static methods used only for creation of values.

Example:

final class MergeRequestStatus extends Enumerable {

    public static function open()
    {
        return self::createEnum('open');
    }
    
    public static function approved()
    {
        return self::createEnum('approved');
    }

    public static function merged()
    {
        return self::createEnum('merged');
    }
    
    public static function declined()
    {
        return self::createEnum('declined');
    }
    
    /**
     * Returns true if status is final.
     *
     * @return bool
     */
    public function isFinal()
    {
        return $this === self::merged() || $this === self::declined();
    }
}

Run tests

composer install
./tests.sh

LICENSE

See LICENSE file.

enumerable.php's People

Contributors

sharom avatar

Watchers

 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.