paillechat / php-enum
Enum implementation for PHP 7
Installs: 45 965
Dependents: 8
Suggesters: 0
Security: 0
Stars: 16
Watchers: 6
Forks: 3
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^2.8
- symfony/phpunit-bridge: ^4.0
This package is not auto-updated.
Last update: 2024-11-01 18:13:03 UTC
README
A PHP 7+ enumeration library.
Why?
To create perfect enums for PHP library
Installation
composer require "paillechat/php-enum:^2.0"
Usage
Declare enum class by extending basic Enum
and filling it with constants.
Constant value does not matter. You can fill it with any payload you can utilize as
general constant, but we suggest you to keep constants as protected
as possible
<?php use Paillechat\Enum\Enum; /** * These docs are used only to help IDE * * @method static static ONE * @method static static TWO */ class IssueType extends Enum { protected const ONE = 1; protected const TWO = 2; } # Now you can create enum via named static call /** @var Enum $one */ $one = IssueType::ONE(); # Enums keeps strict equality $one1 = IssueType::ONE(); $one2 = IssueType::ONE(); $two = IssueType::TWO(); $one1 === $one2; $one !== $two; # Enums plays well with built-in functions \in_array(IssueType::ONE(), [$one, $two], true); # Enums plays well with signature type checks function moveIssue(IssueType $type) { if ($type === IssueType::ONE()) { throw new \LogicException(); } // .... } # You can convert enum to name and back $name = $one->getName(); $new = IssueType::$name();