PHP code example of azimkordpour / power-enum

1. Go to this page and download the library: Download azimkordpour/power-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

azimkordpour / power-enum example snippets




use AzimKordpour\PowerEnum\Traits\PowerEnum;

enum PostStatus: string
{
    use PowerEnum;

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

 
 
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,
    ];
}

$post = Post::find(1);

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

true

$post = Post::find(1);

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

false

$post = Post::find(1);

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

false

$post = Post::find(1);

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

"active"

PostStatus::values();

[
    'active',
    'inactive'
]

PostStatus::names();

[
    'Active',
    'Inactive'
]

PostStatus::list();

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

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

true

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

false

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

false

PostStatus::fromName('Active');

PostStatus::Active

PostStatus::Active->label();

"active"

PostStatus::Active->getLabels();

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

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

PostStatus::Active->label();

"published post"

PostStatus::Active->getLables();

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