2017 © Pedro Peláez
 

library ordinary-enum

Ordinary enum type implementation for PHP

image

linmad/ordinary-enum

Ordinary enum type implementation for PHP

  • Tuesday, January 2, 2018
  • by LinMAD
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

OrdinaryEnum

'Ordinary' enum it's another implementation for PHP, but with one difference - simple as it's possible.

Where to and why to use

I recommend to use when need to construct some think in strict way, so for that comes Enum type. You define in some class in constructor or method a Enum class with will wait a const value form child of enum class. In that case you will control code and reduce dummy switch\if's in your code to validate states or string values etc., (*1)

How it works and how to use

Easy as possible, you crates some class and extends for Orinary's Enum class, then defines needed const values. Like that:, (*2)

class StoreEnum extends Enum
{
  public const __default = self::GROCERY;
  public const GROCERY   = Grocery::class;
  public const COSMETIC  = Cosmetic::class;
}

So as you see it's easy to create list of controlled values, so it will help ya to keep cleaner your code with dependencies., (*3)

Example:, (*4)

...
// Let's imagine you have fabric with stores
$fruitStore = $this->storeFabric()->create(StoreEnum::GROCERY);
...
// Now let's see how can be used Enum in create method
...
public function create(Enum $storeType): StoreInterface
{
  try {
    $storeType = new StoreEnum($type);
  } catch(InvalidEnumTypeException $e) {
    throw new \RuntimeException('Unable to create store from factory');
  }

   return new $storeType->getValue();
}
...

That's it, you define objects and provide them or handling errors, light and simple., (*5)

P.s. From me, that implementation I'm using in different web services and client library's to keep more controllable code and reduce errors with ungiven values as expected., (*6)

The Versions

02/01 2018

dev-master

9999999-dev https://github.com/LinMAD/OrdinaryEnum/

Ordinary enum type implementation for PHP

  Sources   Download

MIT

The Development Requires

by Artjoms Nemiro

light structure data enum type plain ordinary

02/01 2018

0.1

0.1.0.0 https://github.com/LinMAD/OrdinaryEnum/

Ordinary enum type implementation for PHP

  Sources   Download

MIT

The Development Requires

by Artjoms Nemiro

light structure data enum type plain ordinary