PHP code example of intrfce / enum-attribute-descriptors

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

    

intrfce / enum-attribute-descriptors example snippets




enum Colour: string {

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';

    public function getTitle()
    {
        return match($this->value) {
            'blue' => "Dark Blue",
            'red' => "Blood Red"
            default => ucfirst($this->value),
        };
    }
}



enum Colour: string {

    use HasAttributeDescriptors;

    #[Title('Blood Red')]
    #[Description('Our primary highlight colour')]
    case RED = 'red';

    #[Title('Dark Blue')]
    #[Description('Our primary logo colour')]
    case BLUE = 'blue';

    #[Title('Army Green')]
    #[Description('Only use this for background colours.')]
    case GREEN = 'green';
}



use Intrfce\EnumAttributeDescriptors\Attributes\KeyValue;

enum PaymentType: string {

    use HasAttributeDescriptors;

    #[Title('Donate')]
    #[KeyValue('group', 'Donations')]
    #[KeyValue('sort_order', 3)]
    case Donate = 'donate';
}

// Usage:
PaymentType::Donate->getKeyValue('group');      // 'Donations'
PaymentType::Donate->getKeyValue('sort_order'); // 3
PaymentType::Donate->getKeyValue('missing');    // null

public function titleFallback(): ?string
{
    return ucfirst($this->value);
}

public function descriptionFallback(): ?string
{
    return 'This option has no description yet';
}