2017 © Pedro Peláez
 

library php-magic-enum

image

brzez/php-magic-enum

  • Sunday, June 19, 2016
  • by brzez
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

php-magic-enum

Easy to use & extend enum value objects for php, (*1)

Overview

PHP lacks enums. Everyone usually just uses const values, which don't really provide type safety, and can create messy code., (*2)

This might be a solution to this problem. It provides a enum value object., (*3)

This magic enum can be instanced via magic methods (named the same as consts) which create new instances of the enum with value set to the consts value., (*4)

Example enum: ``` php class OrderStatus extends \MagicEnum\MagicEnum { const CREATED = 1; const COMPLETE = 2; const CANCELLED = 3; const PROCESSING = 4;, (*5)

// easy way to extend with the status (for labels/translations etc)
public function getLabel()
{
    $name = strtolower($this->getName());
    return "order_status.${name}";
}

}, (*6)


Usage: ``` php // create new instance: $status = OrderStatus::PROCESSING(); $status->getValue() // => 4 // type safety public function setStatus(OrderStatus $status) { $this->status = $status; } // safe setter (but less annoying) public function setStatus($status) { if($status instanceof OrderStatus){ $this->status = $status; }else{ $this->status = new OrderStatus($status); // this validates if $status is defined in OrderStatus const values } }

Extras: twig {# it's easy to extend the enums with some additional features for example: #} {{ order.status.label|trans }}, (*7)

todo:

  • [ ] Add to packagist
  • [ ] Write help for installation via composer
  • [ ] Write some more usage help

The Versions

19/06 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

by Avatar brzez

19/06 2016

v0.9.8

0.9.8.0

  Sources   Download

MIT

The Development Requires

by Avatar brzez