2017 © Pedro Peláez
 

library enum

A simple enum implementation for php that allows for typehinting.

image

macrominds/enum

A simple enum implementation for php that allows for typehinting.

  • Thursday, December 7, 2017
  • by tpraxl
  • Repository
  • 1 Watchers
  • 0 Stars
  • 74 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 3 % Grown

The README.md

A simple enum implementation for php that allows for typehinting.

Build Status, (*1)

Usage

Example

In order to create a blazingly simple Salutation enum, just create it as follows:, (*2)

use macrominds\enum\Enumerations;


class Salutation
{
    use Enumerations;
    protected static $enums = [
            'MRS' => 1,
            'MR' => 2,
            'MS' => 3
        ];
}

If you require complex epressions for your enum values, just take the alternative static method approach and you're fine., (*3)

//alternative approach

use macrominds\enum\Enumerations;

class AnyValueEnum
{
    use Enumerations;

    protected static function enums(){
        return [
            'String' => 'string',
            'Integer' => 2,
            'Object' => Salutation::MR()
        ];
    }
}

That's it. You are now able to typehint your functions and you're sure that you get instances of your custom enum. See tests/EnumTest.php if you're in doubt. It shows which expectations you can make., (*4)

// example for type hinted function
public function save(Salutation $salutation) {
    saveToDB($salutation->value());
}

// example for fetching the enum from value
public function load($value) {
    // throws \Exception if $value is invalid
    return Salutation::fromValue($value);
}

// example for fetching the enum from key
public function loadKey($key) {
    // throws \Exception if $key is invalid
    return Salutation::fromKey($key);
}

IDE Support

In order to support code completion, you may want to give hints about the resulting methods:, (*5)

use macrominds\enum\Enumerations;

/**
 * @method static \macrominds\enum\Salutation MRS()
 * @method static \macrominds\enum\Salutation MR()
 * @method static \macrominds\enum\Salutation MS()
 */
class Salutation
{
    use Enumerations;
    protected static $enums = [
            'MRS' => 1,
            'MR' => 2,
            'MS' => 3
        ];
}

Installation

composer require macrominds/enum., (*6)

TODO

  • More source documentation.
  • Make sure that configured names and values are unique: This is currently the developer's responsibility when creating a custom Enum.
  • Make php5.6 compatible

The Versions

07/12 2017

dev-develop

dev-develop

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

07/12 2017

dev-master

9999999-dev

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

07/12 2017

v1.1.0

1.1.0.0

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

26/09 2016

v1.0.1

1.0.1.0

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

25/09 2016

v1.0.0

1.0.0.0

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

09/09 2016

v0.1.0

0.1.0.0

A simple enum implementation for php that allows for typehinting.

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires