PHP code example of tourze / enum-extra

1. Go to this page and download the library: Download tourze/enum-extra 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/ */

    

tourze / enum-extra example snippets


use Tourze\EnumExtra\Itemable;
use Tourze\EnumExtra\ItemTrait;
use Tourze\EnumExtra\Labelable;
use Tourze\EnumExtra\Selectable;
use Tourze\EnumExtra\SelectTrait;

enum Status: string implements Labelable, Itemable, Selectable
{
    use ItemTrait;
    use SelectTrait;

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

    public function getLabel(): string
    {
        return match($this) {
            self::ACTIVE => 'Active',
            self::INACTIVE => 'Inactive',
        };
    }
}

// Generate select options
$options = Status::genOptions();
// Result: [['label' => 'Active', 'text' => 'Active', 'value' => 'active', 'name' => 'Active'], ...]

// Convert single case to array
$array = Status::ACTIVE->toArray();
// Result: ['value' => 'active', 'label' => 'Active']

interface Labelable
{
    public function getLabel(): string;
}

interface Itemable
{
    public function toSelectItem(): array;
}

interface Selectable
{
    public static function genOptions(): array;
}

interface SelectDataFetcher
{
    public function genSelectData(): iterable;
}

interface TreeDataFetcher
{
    public function genTreeData(): array;
}

interface BadgeInterface
{
    public const SUCCESS = 'success';
    public const WARNING = 'warning';
    public const DANGER = 'danger';
    public const INFO = 'info';
    public const PRIMARY = 'primary';
    public const SECONDARY = 'secondary';
    public const LIGHT = 'light';
    public const DARK = 'dark';
    public const OUTLINE = 'outline';

    public function getBadge(): string;
}

$options = Status::genOptions();

// In your .env file or server configuration
$_ENV['enum-display:App\\Enums\\Status-inactive'] = false;

// Now Status::genOptions() will exclude the INACTIVE option

use Tourze\EnumExtra\BoolEnum;

$value = BoolEnum::YES;
$boolValue = $value->toBool(); // true

// Generate boolean options
$options = BoolEnum::genBoolOptions();
// Result: [['label' => '是', 'text' => '是', 'value' => true, 'name' => '是'], ...]

$array = Status::ACTIVE->toArray();
// Result: ['value' => 'active', 'label' => 'Active']

use Tourze\EnumExtra\BadgeInterface;

enum UserStatus: string implements BadgeInterface, Labelable
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
    case BANNED = 'banned';

    public function getLabel(): string
    {
        return match($this) {
            self::ACTIVE => 'Active',
            self::INACTIVE => 'Inactive',
            self::BANNED => 'Banned',
        };
    }

    public function getBadge(): string
    {
        return match($this) {
            self::ACTIVE => self::SUCCESS,
            self::INACTIVE => self::WARNING,
            self::BANNED => self::DANGER,
        };
    }
}