PHP code example of evaisse / php-simple-enum

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

    

evaisse / php-simple-enum example snippets


use evaisse\PhpSimpleEnum\PhpEnum;

$enumInt = new PhpEnum([
    'FOO' => 1,
    'BAR' => 2,
]);

$enum = PhpEnum::fromConstants('\Symfony\Component\HttpFoundation\Request::METHOD_*');

$enum->getAllowedValues(); 
/*
[
    'GET', 'POST', ...
]
 */        

/*
 * Fetch enum name for a given value
 */
assert($enum->getKeyForValue(\Symfony\Component\HttpFoundation\Request::METHOD_GET) === 'METHOD_GET');

/*
 * test value
 */
$enum->isAllowed('GET'); // true

/*
 * fetch and assign, or throw invalid argument exception
 */
$param = $enum->validate($_GET['askedValue']);

/*
 * Fetch the whole dictonary
 */
$enum->getHash(); /*
   [
        'METHOD_GET' => 'GET', 
        ...
    ]
*/
bash
composer