PHPackages                             saadsebai/enum-support - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. saadsebai/enum-support

ActiveLibrary

saadsebai/enum-support
======================

Add more support to enum

v1.0.2(1y ago)09MITPHPPHP ^8.1

Since Dec 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SaadSebai/enum-support)[ Packagist](https://packagist.org/packages/saadsebai/enum-support)[ RSS](/packages/saadsebai-enum-support/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

Laravel Enumerable Trait Package
================================

[](#laravel-enumerable-trait-package)

A lightweight and easy-to-use package for extending PHP enumerations with additional functionality in your Laravel projects.

---

Features
--------

[](#features)

This package adds the following functionality to PHP enums using the `Enumerable` trait:

- Retrieve all enum values (`values()`).
- Retrieve all enum names (`names()`).
- Get an enum case by name (`getByName()`).
- Get an enum case by value (`getByValue()`).
- Translate an enum (`translate()`).

---

Installation
------------

[](#installation)

To use this package, add it to your Laravel project with Composer:

```
composer require saadsebai/enum-support
```

Usage
-----

[](#usage)

### 1. Create an Enum

[](#1-create-an-enum)

The Enumerable trait can be added to any PHP enum to extend its functionality. Here’s an example:

```
namespace App\Enums;

use Saad\EnumSupport\Traits\Enums\Enumerable;

enum UserRole: string
{
    use Enumerable; // Add the trait to your enum.

    case ADMIN = 'admin';
    case EDITOR = 'editor';
    case VIEWER = 'viewer';
}
```

### 2. Use the Enum

[](#2-use-the-enum)

- Get an array of all the enum values:

```
UserRole::values(); // ['admin', 'editor', 'viewer']
```

- Get an array of all the enum names:

```
UserRole::names(); // ['ADMIN', 'EDITOR', 'VIEWER']
```

- Retrieve a specific enum case by its name:

```
UserRole::getByName('ADMIN'); // Returns UserRole::ADMIN
UserRole::getByName('invalid'); // Returns null
```

- Retrieve a specific enum case by its value:

```
UserRole::getByValue('admin'); // Returns UserRole::ADMIN
UserRole::getByValue('invalid'); // Returns null
```

- Translate an enum:

> ⚠ **Warning**: Before using the `translate()` function, you must create the corresponding translation file. This file should contain the keys and values for the enum cases in the specified language.

```
UserRole::ADMIN->translate('role'); // Returns the translation of the enum value using the giving translation file path.
```

Testing
-------

[](#testing)

This package uses **Pest** for testing. To run the tests, use the following command:

```
php vendor/bin/pest
```

### Example Tests

[](#example-tests)

Below is an example of how the `Enumerable` trait is tested:

#### Test File: `tests/Feature/EnumerableTest.php`

[](#test-file-testsfeatureenumerabletestphp)

```
