2017 © Pedro Peláez
 

library enum

Easy to use enum class.

image

giritli/enum

Easy to use enum class.

  • Wednesday, July 26, 2017
  • by giritli
  • Repository
  • 1 Watchers
  • 1 Stars
  • 9,268 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 13 % Grown

The README.md

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

Enum

PHP lacks enums. This is an implementation of an enum type in PHP. It allows you to define your own enum types and use them as value objects., (*2)

Installation

To install this package using composer, run the following command:, (*3)

composer require giritli/enum

Example usage

Create your initial class that will extend the Enum class., (*4)


final class StatusEnum extends Giritli\Enum\Enum { const draft = 'draft'; const active = 'active'; const archived = 'archived'; const cancelled = 'cancelled'; protected $default = self::draft; }

It is that simple. You now have a fully fledged enum. As the enum class is meant to be a data time, it is recommended that all enum classes be made final but this is of course down to individual use case., (*5)

Example usage:, (*6)


// This status defaults to draft as specified in the class $status = new StatusEnum(); // You can instantiate an enum by value $status = new StatusEnum('active'); $status = new StatusEnum(StatusEnum::active); // Or by name $status = StatusEnum::draft(); // Get the ordinal value of the enum $status->getOrdinal(); // 0 // Get the value of the enum $status->getValue(); // draft echo $status; // Get the key of the enum $status->getKey(); // draft // Get all values of an enum $status->getValues(); StatusEnum::getValues(); // Get all ordinal values of an enum $status->getOrdinals(); StatusEnum::getOrdinals(); // Get all key values of an enum $status->getKeys(); StatusEnum::getKeys();

A key is the name of the class constant and value is the value of the class constant. Usually these values should be identical but there are cases when this is not true., (*7)

An enum once instantiated cannot have it's value changed. You will need to instantiate a new object of a different value. Enum values cannot be dynamically altered either., (*8)

How does it work?

Enum values are specified by the class constants that are defined. An enum can be instantiated by passing an enum value, or without passing any value if there is a default enum value. An enum can also be instantiated by calling the enum name as a method., (*9)

The Versions

26/07 2017

dev-master

9999999-dev

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli

26/07 2017

v0.1.4

0.1.4.0

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli

05/11 2015

v0.1.3

0.1.3.0

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli

26/10 2015

v0.1.2

0.1.2.0

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli

26/10 2015

v0.1.1

0.1.1.0

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli

26/10 2015

v0.1.0

0.1.0.0

Easy to use enum class.

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Ozzy Giritli