kongulov / interact-with-enum
Trait for convenient use of ENUM in PHP
Fund package maintenance!
kongulov
Installs: 24 096
Dependents: 5
Suggesters: 0
Security: 0
Stars: 26
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
This package is auto-updated.
Last update: 2025-04-04 19:18:08 UTC
README
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:
# 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 namespace App\Enums; use Kongulov\Traits\InteractWithEnum; enum StatusEnum: string { use InteractWithEnum; case Pending = 'pending'; case Active = 'active'; case Inactive = 'inactive'; }
After using the trait, you can call methods:
- names()
StatusEnum::names()
Return:
array:3 [ 0 => "Pending" 1 => "Active" 2 => "Inactive" ]
- values()
StatusEnum::values()
Return:
array:3 [ 0 => "pending" 1 => "active" 2 => "inactive" ]
- array()
StatusEnum::array()
Return:
array:3 [ "pending" => "Pending" "active" => "Active" "inactive" => "Inactive" ]
- find($needle)
StatusEnum::find('Active') // Find by name StatusEnum::find('active') // Find by value
Return:
App\Enums\StatusEnum { name: "Active" value: "active" }
- count()
StatusEnum::count()
Return:
3
- exists($value)
StatusEnum::exists('active')
Return:
true
- getByIndex($index)
StatusEnum::getByIndex(1)
Return:
App\Enums\StatusEnum { name: "Active" value: "active" }