2017 © Pedro Peláez
 

library enum

Simple and base functional to start using Enum in your projects.

image

githubjeka/enum

Simple and base functional to start using Enum in your projects.

  • Tuesday, July 17, 2018
  • by githubjeka
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Usage ENUM

Simple and base functional to start using Enum in your projects. Just initialize enum value by creating Enum class., (*2)

final class SizeEnum extends \githubjeka\enum\BaseObjectEnum
{
    const XS = 'xs';
    const SMALL = 's';
    const MEDIUM = 'm';
    const LARGE = 'l';
    const XL = 'xl';
    const XXL = 'xxl';

    public static function getList(): array
    {
        return [
            self::XS => 'Extra small(xs)',
            self::SMALL => 'Small',
            self::MEDIUM => 'Medium',
            self::LARGE => 'Large',
            self::XL => 'Extra large(xl)',
            self::XXL => 'Extra extra large(xxl)',
        ];
    }
}

After that you can use API enums as:, (*3)

SizeEnum::getList()

Return ready list for use in HTML list elements like checkboxes, select or others, (*4)

SizeEnum::getKeys()

Return values of ENUM for use it in validation rules like Assertion::inArray('xs', SizeEnum::getKeys());;, (*5)

SizeEnum::XXL

Return string key. Use to compare with the saved values (bool)('xxl' === SizeEnum::XXL)?>;, (*6)

SizeEnum::getLabel(SizeEnum::LARGE)

Return human description of value. InvalidArgumentException will be returned if value is wrong., (*7)

SizeEnum::isValid('xl')

Use to avoid InvalidArgumentException exception., (*8)

new SizeEnum('xxl')

Return Object enum., (*9)

class Shirt
{
    private $size;

    public function __construct(SizeEnum $size)
    {
        $this->size = $size;
    }

    public function size(): SizeEnum
    {
        return $this->size;
    }
}

$sizeFromDb = 'xxl';
$size = new SizeEnum($sizeFromDb);
$shirt = new Shirt($size);

$shirt->size()->asKey(); // (string) xxl 

$shirt->size()->asLabel(); // (string) Extra extra large(xxl)

$shirt->size()->equals(new SizeEnum(SizeEnum::XS)); // (bool) false

echo $shirt->size(); // (string) xxl

Extra

Strongly recommend that you do not use numbers as enumeration values:, (*10)

const XS = '0';         // not recommend
const SMALL = '1';      // not recommend
const MEDIUM = 2;       // not recommend
const LARGE = 3;        // not recommend

because it is easy to mix up the strings and the underlying number values if you quote the ENUM values incorrectly., (*11)

By default expected that all Enum(constants) are Strings., (*12)

If you want to use integers you can change $stringMode property to FALSE in your Enum class. After that should use only integers value for constants:, (*13)

php const XS = 0; const SMALL = 1; const MEDIUM = 2; const LARGE = 3;, (*14)

If you use MySQL ENUM see limits also, (*15)

The Versions

17/07 2018

dev-master

9999999-dev

Simple and base functional to start using Enum in your projects.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

enum

17/07 2018

v1.0.0

1.0.0.0

Simple and base functional to start using Enum in your projects.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

enum