evaisse/php-simple-enum

A very stupid, basic, enum control struct for php

v1.0.0 2020-05-27 16:39 UTC

This package is auto-updated.

Last update: 2024-10-28 03:25:40 UTC


README

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