PHP code example of pit-tech / v-enum

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

    

pit-tech / v-enum example snippets


enum Animals: string
{
    use EnumTrait;

    // several groups of animals
    const SPOTTED = 'spotted';
    const HAS_MANE = 'hasMane';
    const CATS = 'cats';
    const GIRAFFIDAE = 'giraffidae';

    // 123 - is external id  
    #[vEnum(123, tags: [self::CATS, self::HAS_MANE])]
    case LEO = 'leo'; // 'leo' - is local unique id of animal 

    #[vEnum(124, tags: [self::CATS, self::SPOTTED])]
    case JAGUAR = 'jaguar';

    #[vEnum(125, tags: [self::CATS])]
    case PANTHER = 'panther';

    // Guepard - is title of the enum value 
    #[vEnum(126, title: 'Guepard', [self::CATS, self::SPOTTED])]
    case GUEPARD = 'guepard';

    #[vEnum(127, tags: [self::GIRAFFIDAE, self::SPOTTED])]
    case GIRAFFE = 'giraffe';

}


    // get all external ids of animals belonging to the cats group and having spots
    Animals::filter([Animals::CATS, Animals::SPOTTED => true])->payloads();
    // get all external ids of animals belonging to the cats group and having spots
    Animals::filter([Animals::HAS_MANE])->values();
    
    // enums can be mapped with callback and transform to entries in your API
    Animals::map(fn(vEnum $vEnum) => [
        'label' => $vEnum->label ?? ucfirst($vEnum->value). ' as default',
        'value' => $vEnum->value,
    ])
}