composer require neutronstars/enum
composer require neutronstars/doctrine-enum-php-type
composer require neutronstars/symfony-normalizer-enum-php
Docs: https://github.com/Neutron-Pro/symfony-normalizer-enum-php
composer require neutronstars/doctrine-enum-php-type
composer require neutronstars/symfony-normalizer-enum-php
/**
* @method static self ONE()
* @method static self TWO()
* @method static self THREE()
*/
class MyEnum extends \NeutronStars\Enum\Enum
{
public const ONE = null;
public const TWO = null;
public const THREE = null;
}
This enum does not require a constructor. We simply create a list of constants that have no value other than their name.
/**
* @method static self ONE()
* @method static self TWO()
* @method static self THREE()
*/
class MyStringEnum extends \NeutronStars\Enum\Enum
{
public const ONE = 'One';
public const TWO = 'Two';
public const THREE = 'Three';
}
/**
* @method static self ONE()
* @method static self TWO()
* @method static self THREE()
*/
class MyIntEnum extends \NeutronStars\Enum\Enum
{
public const ONE = 1;
public const TWO = 2;
public const THREE = 3;
}
$two = MyStringEnum::TWO();
echo $two; // return TWO
echo $two->key; // return TWO
echo $two->value; // return Two
$three = MyStringEnum::from('THREE');
echo $three; // THREE
echo $three->key; // return THREE
echo $three->value; // return Three
$value = MyStringEnum::tryFrom($_GET['number']);
if ($value === MyStringEnum::ONE()) {
echo 'The number is ONE !';
}
Result:
if $_GET['number] is ONE or One or 1 then 'The number is ONE !'
else nothing.
If you use "tryFrom" and the parameter value is not found, the method returns null.
If you use "from" and the value of the parameter is not found, an error of type ValueError is raised.
$cases = MyIntEnum::cases();
Result:
[
MyIntEnum::ONE(),
MyIntEnum::TWO(),
MyIntEnum::THREE()
]
foreach (MyIntEnum::cases() as $case) {
echo '- ' . $case->value;
}
Result:
- 1
- 2
- 3
You can serialize an enum with the serialize function of PHP.
$serialized = serialize(MyEnum::THREE());
But for deserialization, there will be a small difference, You must use the from
or tryFrom
method:
$deserialized = MyEnum::from(unserialize($deserialized));
// $deserialized = MyEnum::THREE()