PHP code example of vixen / laravel-enums

1. Go to this page and download the library: Download vixen/laravel-enums 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/ */

    

vixen / laravel-enums example snippets


use Vixen\Enums\Attributes\Description;
use Vixen\Enums\Attributes\Label;
use Vixen\Enums\Attributes\LongLabel;
use Vixen\Enums\Attributes\ShortLabel;
use Vixen\Enums\HasDescriptions;
use Vixen\Enums\HasLabels;
use Vixen\Enums\HasOptions;
use Vixen\Enums\HasValues;
use Vixen\Enums\Filterable;
use Vixen\Enums\Snakeable;
use Vixen\Enums\ToArray;
use Vixen\Enums\ToValue;

enum Status: string
{
    use HasDescriptions;
    use HasLabels;
    use HasOptions;
    use HasValues;
    use Filterable;
    use Snakeable;
    use ToArray;
    use ToValue;

    #[Label('Pending')]
    #[LongLabel('Pending Review')]
    #[ShortLabel('PND')]
    #[Description('Items waiting to be reviewed by an administrator')]
    case Pending = 'pending';

    #[Label('Approved')]
    #[LongLabel('Approved Review')]
    #[ShortLabel('APR')]
    #[Description('Items that have passed review')]
    case Approved = 'approved';

    #[Label('Rejected'), LongLabel('Rejected Review'), ShortLabel('REJ')]
    case Rejected = 'rejected';
}

// Static methods — return a Collection keyed by enum value
Status::labels();      // ['pending' => 'Pending', 'approved' => 'Approved', ...]
Status::longLabels();  // ['pending' => 'Pending Review', ...]
Status::shortLabels(); // ['pending' => 'PND', ...]

// Instance methods — return the label for a single case
Status::Pending->label();      // 'Pending'
Status::Pending->longLabel();  // 'Pending Review'
Status::Pending->shortLabel(); // 'PND'

case Pending = 'pending';
// Pending->label() returns 'Pending'

#[Label('Pending')]                        // default label
#[Label('Pending Review', type: 'long')]   // equivalent to #[LongLabel(...)]
#[Label('PND', type: 'short')]             // equivalent to #[ShortLabel(...)]

#[Label('enums.status.pending')]
case Pending = 'pending';

// Static method — returns a Collection keyed by enum value
Status::descriptions();
// ['pending' => 'Items waiting to be reviewed by an administrator', 'approved' => 'Items that have passed review', 'rejected' => null]

// Instance method — returns the description for a single case, or null
Status::Pending->description();     // 'Items waiting to be reviewed by an administrator'
Status::Rejected->description();    // null

#[Description('enums.status.pending_description')]
case Pending = 'pending';

Status::options();
// [
//     ['label' => 'Pending', 'value' => 'pending'],
//     ['label' => 'Approved', 'value' => 'approved'],
//     ['label' => 'Rejected', 'value' => 'rejected'],
// ]

Status::descriptiveOptions();
// [
//     ['label' => 'Pending', 'value' => 'pending', 'description' => 'Items waiting to be reviewed by an administrator'],
//     ['label' => 'Approved', 'value' => 'approved', 'description' => 'Items that have passed review'],
//     ['label' => 'Rejected', 'value' => 'rejected', 'description' => null],
// ]

Status::options(only: ['shortLabel', 'description']);
// [
//     ['value' => 'pending', 'shortLabel' => 'PND', 'description' => 'Items waiting to be reviewed by an administrator'],
//     ['value' => 'approved', 'shortLabel' => 'APR', 'description' => 'Items that have passed review'],
//     ['value' => 'rejected', 'shortLabel' => 'REJ', 'description' => null],
// ]

// Use ['*'] to 

Status::values(); // ['pending', 'approved', 'rejected']

Status::only(['Pending', 'Approved']);
// EnumCollection of [Status::Pending, Status::Approved]

Status::except(['Rejected']);
// EnumCollection of [Status::Pending, Status::Approved]

// Chain with EnumCollection methods
Status::only(['Pending', 'Approved'])->toOptions();
// [
//     ['label' => 'Pending', 'value' => 'pending'],
//     ['label' => 'Approved', 'value' => 'approved'],
// ]

use Vixen\Enums\EnumCollection;

$collection = new EnumCollection(Status::cases());

$collection->toOptions();
// [
//     ['label' => 'Pending', 'value' => 'pending'],
//     ['label' => 'Approved', 'value' => 'approved'],
//     ['label' => 'Rejected', 'value' => 'rejected'],
// ]

$collection->toDescriptiveOptions();
// [
//     ['label' => 'Pending', 'value' => 'pending', 'description' => 'Items waiting to be reviewed by an administrator'],
//     ['label' => 'Approved', 'value' => 'approved', 'description' => 'Items that have passed review'],
//     ['label' => 'Rejected', 'value' => 'rejected', 'description' => null],
// ]

// Customize fields with the only parameter
$collection->toOptions(only: ['shortLabel', 'description']);
// [
//     ['value' => 'pending', 'shortLabel' => 'PND', 'description' => '...'],
//     ...
// ]

Status::Pending->snake();       // 'pending'
Status::PendingReview->snake(); // 'pending_review'

Status::Pending->toArray();
// ['label' => 'Pending', 'value' => 'pending']

Status::Pending->toValue(); // 'pending'