Skip to content

This lightweight package provides a Trait that allows you to fully utilize Enum classes in your PHP projects, particularly in modern PHP frameworks like Laravel.

License

Notifications You must be signed in to change notification settings

azim-kordpour/power-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Power Enum Classes in PHP

This lightweight package provides a Trait that allows you to fully utilize Enum classes in your PHP projects, particularly in modern PHP frameworks like Laravel.

Installation

NOTE: As Enum was introduced in PHP 8.1, this package requires a minimum PHP version of 8.1.

You can install the package via composer:

composer require azimkordpour/power-enum

Usage Instructions

To use the PowerEnum trait in your Enum class, simply import it like this:

<?php

use AzimKordpour\PowerEnum\Traits\PowerEnum;

enum PostStatus: string
{
    use PowerEnum;

    case Active = 'active';
    case Inactive = 'inactive';
}

Now, let's take a closer look at the methods.


In Laravel

Eloquent allows you to cast your attribute values to PHP Enums.

 <?php
 
namespace App\Models;

use App\Enums\PostStatus; 
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'status' => PostStatus::class,
    ];
}

Then, you can use it like the below examples.

Check if the status of the model is active:

$post = Post::find(1);

// The status is active.
$post->status->isActive();

Returns boolean:

true

Check if the status of the model equals the given value:

$post = Post::find(1);

// The status is active.
$post->status->equals('inactive');

Returns boolean:

false

This method works like equals:

$post = Post::find(1);

// The status is active.
$post->status->is('inactive');

Returns boolean:

false

Get the label of the status:

$post = Post::find(1);

// The status is active.
$post->status->label();

Returns the value of the case if you have not set labels:

"active"

For setting custom labels and Seeing all methods in PHP projects, take a look at the next section.


All Methods

Get the values of PostStatus statically:

PostStatus::values();

Returns an array:

[
    'active',
    'inactive'
]

Get the names of PostStatus statically:

PostStatus::names();

Returns an array:

[
    'Active',
    'Inactive'
]

Get the names and values of PostStatus statically:

PostStatus::list();

Returns an array:

[
    'Active' => 'active',
    'Inactive' => 'inactive'
]

Check if the case is the active one:

PostStatus::from('active')->isActive();

Returns boolean:

true

Check if the case equals the given value:

PostStatus::Active->equals('inactive');

Returns boolean:

false

This method works like equals:

PostStatus::Active->is('inactive');

Returns boolean:

false

Initiate the class from name:

PostStatus::fromName('Active');

Returns the Enum object:

PostStatus::Active

Get the label of the case:

PostStatus::Active->label();

Returns the value of the case if you have not set labels:

"active"

Get the labels of the cases:

PostStatus::Active->getLabels();

Returns the values of the cases if you have not set labels:

[
    'active' => 'active',
    'inactive' => 'inactive'
]

You can write custom label for the cases in your Enum class:

/**
 * Set the labels of all the cases.
 */
 public static function setLabels(): array
 {
    return [
        self::Active->value => 'published post',
        self::Inactive->value => 'draft post',
    ];
 }

Then, the method of label:

PostStatus::Active->label();

Returns:

"published post"

And the method of getLabels:

PostStatus::Active->getLables();

Returns:

[
    'active' => 'published post',
    'inactive' => 'draft post'
]

Testing

composer test

About

This lightweight package provides a Trait that allows you to fully utilize Enum classes in your PHP projects, particularly in modern PHP frameworks like Laravel.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages