robier/enum

PHP enums implementation

v1.0.0 2023-11-08 09:12 UTC

This package is auto-updated.

Last update: 2024-06-08 10:26:16 UTC


README

This is yet another enum implementation in PHP.

When we did not have enums in PHP this library was trying to fix that. Now we have enums with PHP version 8.1 so there is no need for "yet another enum implementation". This library was changed to add "missing" features to already existing enums.

Supported features:

Name Trait Description
Default \Robier\Enum\HasDefault Adds 2 new methods to the enum:
- try(int|string $value, self $default): self
- tryName(string $name, self $default): self
Random \Robier\Enum\HasRandom Adds 1 new method:
- random(self ...$exclude): self
Boolean checks \Robier\Enum\HasBooleanChecks Adds 1 new method and 2 methods for every case in enum.
New method is ´any(self ...$test):bool´ and other methods are magic. Let's say you have ADMIN_USER case in enum, then new magic methods would be:
- isAdminUser():bool
- notAdminUser():bool
All \Robier\Enum\HasAll Have all already mentioned enum enhancements.

When using "Boolean checks", you can easily create a doc blocks for magic functions so your IDE can be developer friendly.

$ vendor/bin/enum [FQN enum]

It will just generate all magic methods in stdout. It will not change actual files.

Development

This project is dockerized. Before running tests you need to build a docker container by using provided script from the docker folder.

 $ docker/build

After the build is finished, you can run any command by using:

 $ docker/run %command%

for example:

 $ docker/run composer install

Additional tools for developing:

  • composer test - runs all PHPUnit tests
  • XDEBUG_MODE=coverage composer test:coverage:html - runs all PHPUnit tests and generates HTML report
  • composer test:infestation - runs mutation tests and generates mutation score

Feel free to contribute.