neutronstars / enum
Added an enumeration system as close as possible to PHP 8.1 for earlier versions.
1.4.0
2021-07-08 20:26 UTC
Requires
- php: ^7.1 | ^8.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^6.0 | ^7.0 | ^8.0 | ^9.0
This package is auto-updated.
Last update: 2024-11-09 23:33:26 UTC
README
Installation:
Only Enum PHP whiteout Doctrine and Symfony:
composer require neutronstars/enum
For Doctrine
composer require neutronstars/doctrine-enum-php-type
For Symfony
composer require neutronstars/symfony-normalizer-enum-php
Docs: https://github.com/Neutron-Pro/symfony-normalizer-enum-php
For Symfony and Doctrine
composer require neutronstars/doctrine-enum-php-type
composer require neutronstars/symfony-normalizer-enum-php
How create an Enum class:
Method 1
/** * @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 2
/** * @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; }
Use an Enum class:
Get instance of an enum
$two = MyStringEnum::TWO(); echo $two; // return TWO echo $two->key; // return TWO echo $two->value; // return Two
Get instance of an enum from a string
$three = MyStringEnum::from('THREE'); echo $three; // THREE echo $three->key; // return THREE echo $three->value; // return Three
Compare the instances of an enum
$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.
Difference between from
and tryFrom
.
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.
Retrieve all keys of the enum.
$cases = MyIntEnum::cases();
Result:
[
MyIntEnum::ONE(),
MyIntEnum::TWO(),
MyIntEnum::THREE()
]
foreach (MyIntEnum::cases() as $case) { echo '- ' . $case->value; }
Result:
- 1
- 2
- 3
Serialization and Deserialization
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()