PHP code example of cable8mm / enum-getter

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

    

cable8mm / enum-getter example snippets


use Cable8mm\EnumGetter\EnumGetter;

enum Size: string
{
    use EnumGetter;

    case LARGE = 'large';
    case MIDDLE = 'middle';
    case SMALL = 'small';
}

print Size::LARGE->name
//=> 'LARGE'

print Size::LARGE->value
//=> 'large'

print Size::LARGE->value()
//=> 'large'

print Size::LARGE->name()
//=> 'LARGE'

print Size::names()
//=> ['LARGE', 'MIDDLE', 'SMALL']

print Size::values()
//=> ['large', 'middle', 'small']

print Size::array()
//=> ['LARGE'=>'large', 'MIDDLE'=>'middle', 'SMALL'=>'small']

print Size::getName('large')
//=> 'LARGE'

use Cable8mm\EnumGetter\EnumGetter;

enum Size2: string
{
    use EnumGetter;

    case LARGE = 'large';
    case MIDDLE = 'middle';
    case SMALL = 'small';

    public function value(): string
    {
        return match ($this) {
            self::LARGE => 'grand', // __('large') can use a translation module
            self::MIDDLE => 'milieu', // __('milieu') can use a translation module
            self::SMALL => 'petit(e)', // __('small') can use a translation module
        };
    }
}

print Size2::LARGE->name
//=> 'LARGE'

print Size::LARGE->value
//=> 'large'

print Size::LARGE->value()
//=> 'grand'

print Size2::LARGE->name()
//=> 'LARGE'

print Size2::names()
//=> ['LARGE', 'MIDDLE', 'SMALL']

print Size2::values()
//=> ['grand', 'milieu', 'petit(e)']

print Size2::array()
//=> ['LARGE'=>'grand', 'MIDDLE'=>'milieu', 'SMALL'=>'petit(e)']

print Size::getName('grand')
//=> 'LARGE'

// In field of Nova resource
Select::make(__('Size'), 'size')
    ->options(Size2::array())
    ->displayUsingLabels(),

// In Nova factory file
public function definition(): array
{
    return [
        'size' => fake()->randomElement(Size2::names()),
    ];
}