Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 5 days 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 (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-07T05:50:17.000Z (5 months ago)
- Last Synced: 2024-09-25T20:06:03.373Z (8 days ago)
- Topics: enum, enums, laravel, php, php81
- Language: PHP
- Homepage: https://kongulov.dev/blog/simplifying-data-structures-with-enum-in-laravel
- Size: 8.79 KB
- Stars: 24
- 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
[![Latest Version on Packagist](https://img.shields.io/packagist/v/kongulov/interact-with-enum?style=flat-square)](https://packagist.org/packages/kongulov/interact-with-enum)
![Licence](https://img.shields.io/github/license/kongulov/interact-with-enum?style=flat-square)
[![Total Downloads](https://poser.pugx.org/kongulov/interact-with-enum/downloads?format=flat-square)](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"
}