2017 © Pedro Peláez
 

library enum

Simple ENUM class.

image

spareparts/enum

Simple ENUM class.

  • Friday, April 20, 2018
  • by SpareParts
  • Repository
  • 1 Watchers
  • 3 Stars
  • 4,767 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 7 Versions
  • 11 % Grown

The README.md

Enum

PHP Enum done right, (*1)

Build Status Scrutinizer Code Quality Code Coverage, (*2)

Easy way to use enumerated values in PHP., (*3)

Installation

Please, use composer., (*4)

composer require spareparts/enum

Basic usage

/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum
{
}

// obtain enum value
$state = WindowStateEnum::OPEN();

// assign enum value
$windows->state = WindowStateEnum::CLOSED();

// compare enum values
if ($window->state === WindowStateEnum::OPEN()) {
    ....
}

// use enum to guard method parameters
function changeWindowState(WindowStateEnum $newState) {
    ...
}

How to prepare Enum

  1. extend Enum class
  2. Annotate enum class with @method annotations describing Enum values

How to use Enum

There are two possible ways to use enum values, with first one being preferred., (*5)

  1. using static methods with same name as your desired value.

This works with help from magic __callStatic method, meaning you do not have to add any methods manually., (*6)

$state = WindowStateEnum::OPEN();

This method is preferred, as it nicely shows its intended value without having to use weakly guarded strings., (*7)

Important tip: To have values correctly autocompleted, use @method annotations, like this:, (*8)

/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum 
{
}

This way, your IDE should know WindowStateEnum has 2 methods OPEN and CLOSE and correctly hint on their names. In case you are using IDE without support for @method annotations, you can always just add those methods "for real" :), (*9)

  1. using instance() method with desired value as instance parameter
$state = WindowStateEnum::instance('OPEN');

There is nothing wrong with this approach, but mistakes/typos can be easily made., (*10)

Asking enum for unsupported value

In case you ask for value that is not in the enum values, an InvalidEnumValueException exception is thrown., (*11)

try {
    $window->setState(WindowStateEnum::FLYING());
} catch (InvalidEnumValueException $e) {
    echo "This is not a correct state for window to be in!";
}

Testing for multiple values

You can check for enum belonging to any of an enum group / set like this:, (*12)

if ($state->isAnyOf([Window::OPEN(), Window::CLOSED()]));

The Versions

20/04 2018

dev-master

9999999-dev

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

20/04 2018

v0.4.0

0.4.0.0

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

07/08 2017

v0.3.0

0.3.0.0

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

12/05 2017

dev-composerci5991

dev-composerci5991

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

24/02 2017

v0.2.0

0.2.0.0

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

02/02 2017

dev-enum_sets

dev-enum_sets

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum

12/01 2017

v0.1.0

0.1.0.0

Simple ENUM class.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

enum