splitstack/laravel-enum-friendly

Make your Laravel enums friendly with TypeScript, selects, and many more convenient features

1.0.0 2025-04-01 00:18 UTC

This package is auto-updated.

Last update: 2025-05-31 00:45:42 UTC


README

Tests

PHP Version Laravel Version Total Downloads

Introduction

EnumFriendly is a powerful PHP package that enhances your Laravel application's enum experience. It provides a friendly interface for working with enums, making them more versatile and easier to integrate with TypeScript, forms, translations, and more.

With EnumFriendly, you can:

  • Generate enum classes with a simple artisan command
  • Convert enum values to TypeScript types
  • Create form-friendly select options
  • Generate validation rules automatically
  • Access readable labels and collections
  • And much more!

Installation

You can install the package via composer:

composer require splitstack/laravel-enum-friendly

The package will automatically register its service provider.

Usage

Creating a Friendly Enum

You can use the MakeFriendlyEnum command to create a new friendly enum:

php artisan split:enum {name} {--type= : string or int, the backed type} {--u|upper : Convert the case name to uppercase} {values*}

Example:

php artisan split:enum Status --type=string active:ACTIVE inactive:INACTIVE

This will create an enum Status with the following cases and the ExtendedEnum trait:

use Splitstack\EnumFriendly\Traits\ExtendedEnum;

enum Status: string
{
  use ExtendedEnum;

  case ACTIVE = 'active';
  case INACTIVE = 'inactive';
}

Using the ExtendedEnum Trait

The ExtendedEnum trait provides additional methods for your enums (string-backed, int-backed, or plain):

use Splitstack\EnumFriendly\Traits\ExtendedEnum;

enum MyEnum: string
{
  use ExtendedEnum;

  case ADMIN = 'admin';
  case USER = 'user';
}

Available Methods

Method Description Example Output
values() Get the enum values ['active', 'inactive']
collect() Get the enum values as a collection Collection of ['active', 'inactive']
implode(string $glue = ',') Implode the enum values 'active,inactive'
toSelectOptions() Get the enum values as select options [['value' => 'active', 'label' => 'Active'], ...]
keys() Get the enum keys ['ACTIVE', 'INACTIVE']
readable() Get the enum keys in a readable format ['Active', 'Inactive']
random() Get a random enum value 'active' or 'inactive'
toTypeScript() Make the enum friendly for TypeScript ['type' => 'Status', 'values' => [...]]
rules(array $extra = []) Make the enum friendly for validation ['required', 'string', 'in:active,inactive']

TypeScript Integration

The toTypeScript() method generates TypeScript-friendly type definitions:

Status::toTypeScript();
// Returns:
// [
//   'type' => 'Status',
//   'values' => ['active', 'inactive']
// ]

Form Integration

Create select options for your forms easily:

Status::toSelectOptions();
// Returns:
// [
//   ['value' => 'active', 'label' => 'Active'],
//   ['value' => 'inactive', 'label' => 'Inactive']
// ]

Validation Rules

Generate Laravel validation rules automatically:

Status::rules(['required']);
// Returns: ['required', 'string', 'in:active,inactive']

Testing

Run the test suite:

composer test

Generate test coverage report:

composer test:coverage

Contributing

Contributions are welcome! Feel free to:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

The MIT License (MIT). Please see License File for more information.

Credits