miladrahimi / php-enum
A PHP Abstract Enum Class
Installs: 7 607
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: >=7.1|^8.0
Requires (Dev)
- phpunit/phpunit: ^7|^8|^9
README
PHP-Enum
To make your Enums feel alive, just make them extend Enum
abstract class!
Installation
Install Composer and run following command in your project's root directory:
composer require miladrahimi/php-enum "1.*"
Documentation
Consider this enum class:
namespace MiladRahimi\Enum\Enum; class SampleEnum extends Enum { const UNO = 1; const ONE = 1; const TWO = 2; const STR = "sth"; }
Now you are able to use this methods:
SampleEnum::all(); // ['UNO' => 1, 'ONE' => 1, 'TWO' => 2, 'STR' => 'sth'] SampleEnum::keys(); // ['UNO', 'ONE', 'TWO', 'STR']; SampleEnum::values(); // [1, 1, 2, 'sth'] SampleEnum::hasKey('ONE'); // true SampleEnum::hasKey('xXx'); // false SampleEnum::hasValue(2); // true SampleEnum::hasValue('xXx'); // false SampleEnum::valueOf('ONE'); // 1 SampleEnum::keysOf(1); // ['UNO', 'ONE'] SampleEnum::keyOf(1); // 'UNO' SampleEnum::randomKey(); // One of 'UNO', 'ONE', 'TWO', 'STR' SampleEnum::randomKeyExceptKeys(['ONE', 'TWO']); // One of 'UNO', 'STR' SampleEnum::randomKeyExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of 'ONE', 'UNO' SampleEnum::randomValue(); // One of 1, 2, 'sth' SampleEnum::randomValueExceptValues([SampleEnum::STR, SampleEnum::TWO]); // One of SampleEnum::ONE, SampleEnum::UNO SampleEnum::randomValueExceptKeys(['STR', 'TWO']); // One of SampleEnum::ONE, SampleEnum::UNO
License
PHP-Enum is initially created by Milad Rahimi and released under the MIT License.