2017 © Pedro Peláez
 

library enum

A simple PHP library for creating enum objects.

image

wirus15/enum

A simple PHP library for creating enum objects.

  • Thursday, February 1, 2018
  • by wirus15
  • Repository
  • 1 Watchers
  • 1 Stars
  • 360 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 10 % Grown

The README.md

Enum

A simple PHP library for creating Enum objects., (*1)

Examples

Defining a new Enum class, (*2)

use Enum\Enum;

// defining our Enum class
final class Example extends Enum
{
    const FOO = 1;
    const BAR = 2;
    const YOLO = 3;
}

Basic usage, (*3)

$foo = Example::get(Example::FOO);
$bar = Example::get('bar');
$yolo = Example::YOLO();

function test(Example $enum) 
{
    if ($enum->is(Example::FOO)) {
        echo 'foo';
    } else if ($enum->is(Example::BAR)) {
        echo 'bar';
    } else {
        echo $enum;
    }
}

test($foo);   // foo
test($bar);   // bar
test($yolo);  // Yolo

Getting Enum instances, listing keys and values, (*4)

Example::get(Example::FOO);               // instance of Example
Example::FOO();                           // shortcut
Example::all();                           // array of Example instances

// keys and values
Example::keys();                          // array of keys ['FOO', 'BAR', 'YOLO']
Example::values();                        // array of values [1, 2, 3]

// checking if value exists
Example::has(3);                          // true
Example::has(4);                          // false

Getting single Enum properties, (*5)

$foo = Example::FOO();
$foo->key();                              // 'FOO'
$foo->value();                            // 1

Comparing Enums, (*6)

$foo = Example::FOO();
$foo->is(1);                              // true
$foo->is(Example::FOO);                   // true
$foo->is($foo);                           // true

// comparing with strict option (type comparison)
$foo->is($foo, true);                     // true
$foo->is(1, true);                        // false

// searching in array
$foo->in([1,2,3]);                        // true
$foo->in([2,3]);                          // false

You can also make static calls and specify Enum class, (*7)

Enum::get(Example::FOO, Example::class);
Enum::all(Example::class);    
Enum::keys(Example::class);      
Enum::values(Example::class); 
Enum::has(1, Example::class);

Development and tests

To develop this lib we are using docker and docker-compose. After installation of those you should run:, (*8)

bash
docker-compose run enum bash

Then on docker console run:, (*9)

composer install
composer test

License

This library is released under MIT license., (*10)

The Versions

01/02 2018

dev-master

9999999-dev https://github.com/wirus15/enum

A simple PHP library for creating enum objects.

  Sources   Download

MIT

The Requires

  • php >=7

 

The Development Requires

by Maciej Krawczyk

01/02 2018

0.5.1

0.5.1.0 https://github.com/wirus15/enum

A simple PHP library for creating enum objects.

  Sources   Download

MIT

The Requires

  • php >=7

 

The Development Requires

by Maciej Krawczyk

20/10 2017

dev-v0-5

dev-v0-5 https://github.com/wirus15/enum

A simple PHP library for creating enum objects.

  Sources   Download

MIT

The Requires

  • php >=7

 

The Development Requires

by Maciej Krawczyk

23/05 2016

0.4

0.4.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Maciej Krawczyk

07/02 2016

0.2

0.2.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Maciej Krawczyk

06/02 2016

0.1

0.1.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Maciej Krawczyk