granam/integer-enum

Enumeration with integers only

1.2.0 2021-03-24 09:15 UTC

This package is auto-updated.

Last update: 2024-11-21 23:21:42 UTC


README

Usage

  1. Use enum
  2. NULL is NULL, not Enum
  3. Installation

Use enum

<?php
use \Granam\IntegerEnum\IntegerEnum;

$enum = IntegerEnum::getEnum(12345);
echo $enum->getValue(); // 12345
var_dump($enum->is('12345')); // false
var_dump($enum->is(12345)); // true
var_dump($enum->is($enum)); // true
var_dump($enum->is(IntegerEnum::getEnum(12345))); // true
var_dump($enum->is(IntegerEnum::getEnum(99999))); // false

NULL is NULL, enum can not hold it

You can not create IntegerEnum with NULL value. Just use NULL directly for such value.

<?php
try {
    \Granam\IntegerEnum\IntegerEnum::getEnum(null);
} catch(\Granam\IntegerEnum\Exceptions\UnexpectedValueToConvert $unexpectedValueToEnum) {
    echo $unexpectedValueToEnum->getMessage(); // Expected scalar or object with __toString method on strict mode, got NULL
}

Installation

composer.phar require granam/integer-enum

or manually edit composer.json at your project and "require": block (extend existing)

"require": {
    "granam/integer-enum": "dev-master"
}