PHP code example of legecha / enumpty

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

    

legecha / enumpty example snippets


use Legecha\Enumpty\Attributes\DescribeEnum;
use Legecha\Enumpty\Describable;

enum MyEnum
{
    use Describable;

    #[DescribeEnum(label: 'My First Case', description: "My first description")]
    case First;
    #[DescribeEnum(description: "My second description", label: 'My 2nd Case'])
    case Second;
    case Third;
    #[DescribeEnum(label: 'My Fourth Case')]
    case Fourth;
}

$enum = MyEnum::First;
$enum->label(); // string(13) "My First Case"
$enum->description(); // string(20) "My first description"

$enum = MyEnum::Third;
$enum->description(); // NULL

MyEnum::labelCases();
/*
array(4) {
  ["MyEnum::First"]=>
  string(13) "My First Case"
  ["MyEnum::Second"]=>
  string(11) "My 2nd Case"
  ["MyEnum::Third"]=>
  NULL
  ["MyEnum::Fourth"]=>
  string(14) "My Fourth Case"
}
*/
MyEnum::descriptionCases();
/*
array(4) {
  ["MyEnum::First"]=>
  string(20) "My first description"
  ["MyEnum::Second"]=>
  string(21) "My second description"
  ["MyEnum::Third"]=>
  NULL
  ["MyEnum::Fourth"]=>
  NULL
}
*/

use Legecha\Enumpty\Names;

enum MyEnum
{
    use Names;

    case MyFirstCase;
    case Second;
    case HereIsTheThird;
    case DontForgetTheFourth;
}

$enum = MyEnum::MyFirstCase;
$enum->name(); // string(13) "My First Case"
MyEnum::HereIsTheThird->name(); // string (15) "Here Is The Third"

MyEnum::names();
/*
array(4) {
  ["MyEnum::MyFirstCase"]=>
  string(13) "My First Case"
  ["MyEnum::Second"]=>
  string(6) "Second"
  ["MyEnum::HereIsTheThird"]=>
  string(15) "Here Is The Third"
  ["MyEnum::DontForGetTheFourth"]=>
  string(23) "Dont Forget The Fourth"
}
*/