robusto / enum
A PHP 7.0+ Robust enumeration library.
Installs: 1 544
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^5.5
- squizlabs/php_codesniffer: 2.7.1
This package is not auto-updated.
Last update: 2024-11-15 20:08:21 UTC
README
A robust and simple way to define enumerative types in php.
Requirements:
- PHP 7.0+
- DBAL (only for use EnumType)
Simple Example: ```php class DayWeekEnum extends Enum { const SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4, THURSDAY = 5, FRIDAY = 6, SATURDAY = 7 ; } ```
Example using descriptions for the enumerative values:
class LanguageEnum extends Enum { const JAVA = 1, PHP = 2, PYTHON = 3, RUBY = 4, JS = 5 ; protected static $descriptions = [ 'Java', 'PHP', 'Python', 'Ruby', 'Javascript' ]; }
Possibility of using enum with php typehint, restricting their value according to their respective values of type:
public function setLanguage(LanguageEnum $language) { $this->language = $language; } $language = LanguageEnum::JAVA(); $foo->setLanguage($language);
Printing description of its enum value:
public function getLanguage(): LanguageEnum { return $this->language; } $foo->setLanguage(LanguageEnum::JS()); echo $foo->getLanguage(); // Javascript echo LanguageEnum::JS; // 5 echo get_class($foo->getLanguage()); // LanguageEnum