Download the PHP package vaened/php-enum without Composer
On this page you can find all versions of the php package vaened/php-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vaened/php-enum
More information about vaened/php-enum
Files in vaened/php-enum
Package php-enum
Short Description PHP enumeration support
License MIT
Informations about the package php-enum
PHP Enum
Friendly enumeration implementation.
Installation
PHP Enum requires PHP 8.
To get the latest version, simply require the project using Composer:
Declaration
To create an enumeration it is necessary to extend from Vaened\Enum\Enum
, in addition to creating constants for each value of the enumeration. This library has the ability to add attributes for each enumeration, very similar to the enumerations in java.
Usage
Once the enumeration is implemented, you can use its values in the following way. The library uses __callStatic
to create instances of a specified enumeration, according to the requested value.
In case you want autocompletion ID, you can define the methods manually.
or if you are using some ID that supports phpdocs, you can do something like this:
Advanced
There may be specific cases where you need to add some additional attribute to the enumeration. To do this, you will need to override the protected static method attributes available in all Enum
children, here you must define the attributes you will use for each enumeration.
Must use the
Vaened\Enum\Attributor
class.
To get any attribute, you can use the protected method attribute which gets the name of the attribute as parameter
API
Method | Description | Returns |
---|---|---|
static::create(string $value) | Create an instance for the given value. | Enum |
static::instantiate(string $constant) | Instantiate from constant name. | Enum |
static::isValid(string $value) | Check if the given value belongs to the enumaracion. | boolean |
static::values() | Returns an array with all the enumeration values. |
Enum[] |
$instance->value() | Returns the value of an enumeration. | string |
$instance->key() | Returns the constant name of an enumeration. | string |
$instance->equals(string $value) | Verify that the value of an enumeration is equal to a given value. | boolean |
$instance->match(Enum $enum) | Verify that the value of an enumeration is equal to a given enum. | boolean |
More documentation
You can find a lot of comments within the source code as well as the tests located in the tests
directory.