https://github.com/egorbulychev/enumshelper
Enums Helper Traits for PHP 8.1+ ❤
https://github.com/egorbulychev/enumshelper
helper php81 traits
Last synced: 6 months ago
JSON representation
Enums Helper Traits for PHP 8.1+ ❤
- Host: GitHub
- URL: https://github.com/egorbulychev/enumshelper
- Owner: EgorBulychev
- License: mit
- Created: 2023-01-22T01:01:13.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-25T00:58:34.000Z (over 3 years ago)
- Last Synced: 2025-03-13T03:15:49.131Z (over 1 year ago)
- Topics: helper, php81, traits
- Language: PHP
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Enums Helper Traits for PHP 8.1+ ❤
## Installation
> composer require bulychev/enums
## Usage trait
```php
/**
* @method static int USER()
* @method static int ADMIN()
* @method static int ROOT()
*/
#[Property(Description::class)]
enum Role: int
{
use EnumsHelper;
#[Description('Standard user account')]
case USER = 10;
#[Description('User with advanced permissions')]
case ADMIN = 50;
#[Description('User with full permissions')]
case ROOT = 99;
}
```
#### `__invoke & __callStatic`: int|string
BackedEnum
```php
Role::ADMIN(); // int(50)
Role::ROOT(); // int(99)
Role::MANAGER(); // throw new UndefinedEnumCaseException
```
UnitEnum
```php
/**
* @method static string RU()
* @method static string KG()
* @method static string EN()
*/
enum Lang
{
use EnumsHelper;
case RU;
case KG;
case EN;
}
Role::RU(); // string(2) "RU"
Role::KG(); // string(2) "KG"
Role::EN(); // string(2) "EN"
```
#### `names()`: array
```php
Role::names();
// array(3) {
// [0]=> string(4) "USER"
// [1]=> string(5) "ADMIN"
// [2]=> string(4) "ROOT"
// }
```
#### `values()`: array
```php
Role::values();
// array(3) {
// [0]=> int(10)
// [1]=> int(50)
// [2]=> int(99)
// }
```
#### `array()`: array
```php
Role::array();
// array(3) {
// [10]=> string(4) "USER"
// [50]=> string(5) "ADMIN"
// [99]=> string(4) "ROOT"
// }
```
#### `description()`: ?string
```php
Role::ROOT->description();
// string(26) "User with full permissions"
```