jgswift/kenum

PHP 5.5+ enumerator pattern implementation

0.1.1 2014-09-11 02:48 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:04:22 UTC


README

PHP 5.5+ enumerator pattern implementation

Build Status Scrutinizer Code Quality Latest Stable Version License Coverage Status

Description

Kenum is a simple component to provide enum behavior using constants.
Both conventional enums and bitwise enums are included in this package.

Installation

Install via cli using composer:

php composer.phar require jgswift/kenum:0.1.*

Install via composer.json using composer:

{
    "require": {
        "jgswift/kenum": "0.1.*"
    }
}

Dependency

  • php 5.5+

Usage

Default Base

The following is a base Kenum minimal example

<?php
class MyEnum extends kenum\Enum\Base {
    const Option1 = 'Option1';
    const Option2 = 'Option2';
}

$enum = new MyEnum(MyEnum::Option2);

// get current enum value with the value method or through string conversion
$value = $enum->value()  // Returns 'Option1'
$string = (string)$enum; // Returns 'Option1'

// check for equality
$equals = $enum->equals(MyEnum::Option2); // returns true
$equals = $enum->equals(new MyEnum(MyEnum::Option1)); // returns false

Bitwise

The bitwise implementation that uses bitwise flags as values. Bitwise flags can be combined and allow multiple flags to be set at once Constants must be numbers in multiples of 2. An example set would be 1, 2, 4, 8, 16, 32, etc..

<?php
class MyEnum extends kenum\Enum\Bitwise {
    const Option1 = 1;
    const Option2 = 2;
    const Option3 = 4;

    /* etc... */
}

$enum = new MyEnum(MyEnum::Option2 | MyEnum::Option3);

// get current enum value with the value method or through string conversion
var_dump($enum->value());  // Returns '6'
var_dump((string)$enum); // Returns 'Option2 Option3'

// check for equality
var_dump($enum->equals(MyEnum::Option2)); // returns false
var_dump($enum->equals(MyEnum::Option2 | MyEnum::Option3)); // returns true

// check for flag
var_dump($enum->hasFlag(MyEnum::Option2)); // returns true
var_dump($enum->hasFlag(MyEnum::Option1)); // returns false

Constant Scalar Expressions

Since php 5.6 it is possible to use expressions to define constants which conceptually simplifies them and removes the need to manually hard-code every value.

class MyEnum extends kenum\Enum\Bitwise {
    const Option1 = 1;
    const Option2 = self::Option1 * 2; // 2
    const Option3 = self::Option2 * 2; // 4
    const Option4 = self::Option4 * 2; // 8
    /* etc... */
}

Custom Enum

Implementing a custom enum using this pattern is relatively simple with the enum trait

class MyEnum {
    use kenum\Enum;

    function __construct($value) {
        /* store value(s) */
    }

    // custom equality check
    function equals($value) {
        /* check for equality */
    }

    function __toString() {
        /* transform enum value(s) into human-readable text */
    }
}