Skip to content

A Simple way to create enums from php constants, like PhpEnum::fromConstants('PHP_INT_*);

License

Notifications You must be signed in to change notification settings

evaisse/php-simple-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-simple-enum

codecov Build Status

A Simple ENUM utils, to ensure inputs are compliant with your some basic data structs.

composer require evaisse/php-simple-enum

Allow to fetch a static list of values from givens constants :

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', 
        ...
    ]
*/

About

A Simple way to create enums from php constants, like PhpEnum::fromConstants('PHP_INT_*);

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages