Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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"
}