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