zul3s / enum-php
Advanced Php Enum Type
Installs: 25 358
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- jakub-onderka/php-parallel-lint: ^0.9.2
- phpbench/phpbench: @dev
- phpro/grumphp: ^0.11.6
- phpunit/phpunit: 6.*
- sebastian/phpcpd: ^3.0
- sensiolabs/security-checker: ^4.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2023-04-17 06:16:55 UTC
README
PHP >=7.1 only supported.
It's an abstract class that needs to be extended to use it.
What is an Enumeration?
In computer programming, an enumerated type (also called enumeration or enum) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but which do not have any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily.
Some advantages
When you using an enum instead of class constants, you take advantages of
- Type-hint : e.g. function setEnum(Enum $enum)
- Enrich your enum class with methods : e.g. first, format, ...
- Get all possibilities keys/values
Why this ?
Actually you can find others package offer PHP enum implementation, but this package have some advantage :
- Singleton : this package use singleton pattern
- Fast : Implemented small execution cache
- Right way : No circular referential
- Small : this package not needed an other to use
- Quality : PSR-2 standard
Installation
composer require zul3s/enum-php
Declaration
use Zul3s\EnumPhp\Enum;
/**
* Simpson enum
*
* @method static Simpson::HOMER()
* @method static Simpson::MARGE()
*/
class Simpson extends Enum
{
/**
* @description('Description for Homer const')
*/
const HOMER = 1;
const MARGE = 'marjorie_jacqueline';
}
Usage
$marge = Simpson::MARGE();
$homer = Simpson::byKey('HOMER');
$marge = Simpson::byValue('marjorie_jacqueline');
$homer = Simpson::byValue('1', false); // Disable strict type mode
Type-hint
function setMember(Simpson $member)
{
// ...
}
Documentation
Get
$myEnum = MyEnumClass::ENUM_CONST()
Return enum search by const name (method name)$myEnum = MyEnumClass::byValue(mixed $value, [optional] bool $strict)
Return enum search by value$myEnum = MyEnumClass::byKey(string $constName)
Return enum search by const name
Use
$myEnum->getValue() : mixed
Return value of enum$myEnum->getKey() : string
Return string with const name$myEnum->getDescription() : string
Return description annotation if is set or exception$myEnum->isEqual(Enum $myEnum) : bool
Check if enum is equal to anotherecho $myEnum : string
__toString() have implemented to return cast of string value
Helper
MyEnumClass::getAll() : array
Return array with all MyEnum possibilitiesMyEnumClass::getValues() : array
Return simple associate array with key is const name and value is const valueMyEnumClass::isValidKey(string $testedValue) : bool
Check if tested value is valid const nameMyEnumClass::isValidValue($testValue, [optional] bool $strict) : bool
Check if tested value is valid const value