granam / integer-enum
Enumeration with integers only
1.2.0
2021-03-24 09:15 UTC
Requires
- php: >=7.3
- granam/integer: ^7.0
- granam/scalar-enum: ^1.1
Requires (Dev)
- granam/exceptions-hierarchy: ^5.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^9.0
- rector/rector: ^0.10.3
- roave/security-advisories: dev-master
README
Usage
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" }