2017 © Pedro Peláez
 

library enumeration

A simple Typedef Enum implementation for php

image

dreamscapes/enumeration

A simple Typedef Enum implementation for php

  • Tuesday, October 14, 2014
  • by Alaneor
  • Repository
  • 1 Watchers
  • 2 Stars
  • 64 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Enumerations for PHP

Build Status Coverage Status Dependency Status, (*1)

Every php programmer at some point wants to put some structure to all those constants defined in their code. Usually one would put those constants under a dedicated class, but that's it; no additional features, no real benefit, no nothing., (*2)

What if you could get more?

This small library aims at providing additional features to your "Enumerations" - a set of static values that represent something meaningful to your code., (*3)

The problem with constants

Let's say our program will work with animals - various different animals, and we would like each type of animal to have a distinct value ( which I completely made up in this example ). Here's what many programmers currently do:, (*4)

define('MYAPP_ANIMAL_HORSE', 0);
define('MYAPP_ANIMAL_DOG', 1);
define('MYAPP_ANIMAL_CAT', 2);
// ...

While this certaily works, there is a better way of defining those., (*5)

namespace MyApp;

class Animal
{
  const Horse = 0;
  const Dog = 1;
  const Cat = 2;
}

Defining the constants as a class has several benefits:, (*6)

  1. You can use real namespacing, which can save you a few typing when using the class
  2. It feels more natural to use Animal::Horse than MYAPP_ANIMAL_HORSE
  3. Since it's a class it opens up new possibilities and ideas -> that's where this library comes to use :)

How this library helps

The above example with a class introduces some issues but also opens up new possibilities., (*7)

  1. There's nothing preventing the programmer from instantiating the class
  2. What if you wanted to do it the opposite way? -> you have a value and you want to know the constant's name that holds such value in the enumeration?
  3. What if you wanted to check if a constant is defined in the Enumeration?
  4. What if you wanted to type-hint an enumerated value in a function/method's parameter list?

Let's take a look at another example that demonstrates the use of Enumerations provided by this library., (*8)

namespace MyApp;

// Let's extend our Enumeration class
class Animal extends \Dreamscapes\Enumeration
{
  const Horse = 0;
  const Dog = 1;
  const Cat = 2;
}

// So far looks the same, but watch now...

$animal = new Animal; // Raises fatal error (private constructor)

Animal::isDefined('Horse'); // Returns (bool)true
Animal::isDefined('Cow'); // Returns (bool)false

// "Reverse resolution"
$value = Animal::Dog;
echo Animal::getName($value); // prints (string)"Dog"

// Type-hinting
function doSomething(Animal $animal)
{
  // $animal is now an instance of the Animal class
  // that can be easily represented as string
  echo $animal;
}
doSomething(Animal::Horse()); // prints (string)"Horse"

// To get the actual value
$value = $animal->value();
// Or, use the Enumeration::getValue() class method
$value = Animal::getValue($animal);

As you can see, suddenly there's much more you can possibly do with a class as simple as enumeration can be. Learn more in the API Docs which also include code examples and full method descriptions., (*9)

Semantic methods

Enumeration contains several semantically named methods you can use to write code that can be understood simply by looking at the code., (*10)

Consider following example:, (*11)

if (Animal::defines('Horse')) {
  // Animal::Horse is defined!
}

echo Animal::withValue(0); // Horse

More methods are available - be sure to check out API Docs!, (*12)

Installation

Via Composer:

composer require dreamscapes\enumeration:dev-master, (*13)

Composer's autoloading is supported so as long as you require "vendor/autoload.php"; somewhere in your code you can simply start using it., (*14)

Documentation

API documentation is available online - it includes all public methods you can use and also several code samples and use cases., (*15)

Offline documentation

Sure! Just install the development dependencies and generate the docs., (*16)

composer require --dev dreamscapes\enumeration:dev-master
php vendor/bin/phpdoc.php

Documentation is now available at ./docs/index.html., (*17)

License

This software is licensed under the BSD (3-Clause) License. See the LICENSE file for more information., (*18)

The Versions

14/10 2014

dev-develop

dev-develop https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list

14/10 2014

dev-master

9999999-dev https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list

14/10 2014

1.0.0

1.0.0.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list

09/07 2014

0.3.3

0.3.3.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list

12/01 2014

0.3.2

0.3.2.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list

30/11 2013

0.3.1

0.3.1.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list license

25/11 2013

0.3

0.3.0.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list license

28/10 2013

0.2

0.2.0.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list license

28/10 2013

0.1.1

0.1.1.0 https://github.com/Dreamscapes/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list license

27/10 2013

0.1

0.1.0.0 https://github.com/Alaneor/Enumeration

A simple Typedef Enum implementation for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

enum constant enumeration typedef static list license