2017 © Pedro Peláez
 

library enum

Ultralightweight enum type for PHP

image

richard-ejem/enum

Ultralightweight enum type for PHP

  • Wednesday, June 21, 2017
  • by amik
  • Repository
  • 1 Watchers
  • 0 Stars
  • 440 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 39 % Grown

The README.md

PHP lightweight Enum trait

Build Status Downloads this Month Latest stable, (*1)

Simple trait to create enumeration classes., (*2)

Separate your enumerations into standalone classes and validate through your application that you are passing valid enumeration values., (*3)

Installation

Recommended to install using composer:, (*4)

composer require richard-ejem/enum, (*5)

Example

class UserRole
{
    use \Ejem\Enum\EnumTrait;

    const GUEST = 'guest';
    const USER = 'user';
    const ADMIN = 'admin';
}

class User
{
    /** @var string UserRole enum */
    private $role;

    /** @throws \Ejem\Enum\InvalidEnumValueException */
    public function __construct(string $role)
    {
        $this->setRole($role);
    }

    public function getRole(): string
    {
        return $this->role;
    }

    /** @throws \Ejem\Enum\InvalidEnumValueException */
    public function setRole(string $role): void
    {
        UserRole::assertValidValue($role);
        $this->role = $role;
    }

    // ... more user stuff
}

Other features


// Validate without throwing exception. // imagine you are importing users from an old system, // setting all unsupported roles to GUEST: $role = UserRole::isValidValue($row['role']) ? $row['role'] : UserRole::GUEST; // Retrieve all possible values. // Assign random color to a shape $values = Color::getPossibleValues(); $shape->setColor($values[mt_rand(0, count($values)-1)]);

The Versions

21/06 2017

dev-master

9999999-dev

Ultralightweight enum type for PHP

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=7

 

The Development Requires

php enum

21/06 2017

1.0

1.0.0.0

Ultralightweight enum type for PHP

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=7

 

The Development Requires

php enum