https://github.com/kongulov/interact-with-enum
Trait for convenient use of ENUM in PHP
https://github.com/kongulov/interact-with-enum
enum enums laravel php php81
Last synced: 3 months ago
JSON representation
Trait for convenient use of ENUM in PHP
- Host: GitHub
- URL: https://github.com/kongulov/interact-with-enum
- Owner: kongulov
- License: mit
- Created: 2023-06-28T16:48:21.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-04T19:18:05.000Z (3 months ago)
- Last Synced: 2025-04-04T20:24:58.064Z (3 months ago)
- Topics: enum, enums, laravel, php, php81
- Language: PHP
- Homepage: https://kongulov.dev/blog/simplifying-data-structures-with-enum-in-laravel
- Size: 13.7 KB
- Stars: 26
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Interact With Enum in PHP
[](https://packagist.org/packages/kongulov/interact-with-enum)

[](https://packagist.org/packages/kongulov/interact-with-enum)This package contains the `InteractWithEnum.php` trait, which you can use to conveniently work with ENUMs.
## Requirements
- `php: >=8.1`
## Installation
Install the package via Composer:
```bash
# Install interact-with-enum
composer require kongulov/interact-with-enum
```## Usage
Imagine you have ENUM `StatusEnum.php` where we already use the `InteractWithEnum` trait:
```php
array:3 [
0 => "Pending"
1 => "Active"
2 => "Inactive"
]
* **values()**
```php
StatusEnum::values()
```
Return:
array:3 [
0 => "pending"
1 => "active"
2 => "inactive"
]
* **array()**
```php
StatusEnum::array()
```
Return:
array:3 [
"pending" => "Pending"
"active" => "Active"
"inactive" => "Inactive"
]
* **find($needle)**
```php
StatusEnum::find('Active') // Find by name
StatusEnum::find('active') // Find by value
```
Return:
App\Enums\StatusEnum {
name: "Active"
value: "active"
}
* **count()**
```php
StatusEnum::count()
```
Return:
3
* **exists($value)**
```php
StatusEnum::exists('active')
```
Return:
true
* **getByIndex($index)**
```php
StatusEnum::getByIndex(1)
```
Return:
App\Enums\StatusEnum {
name: "Active"
value: "active"
}