Download the PHP package dnl-blkv/simple-php-enum without Composer
On this page you can find all versions of the php package dnl-blkv/simple-php-enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dnl-blkv/simple-php-enum
More information about dnl-blkv/simple-php-enum
Files in dnl-blkv/simple-php-enum
Package simple-php-enum
Short Description A simple C/C++ alike enum library.
License MIT
Homepage https://github.com/dnl-blkv/simple-php-enum
Informations about the package simple-php-enum
Simple PHP Enum
Description
A simple C/C++ alike PHP library for Enums.
Stats and Health
PHP Version Support
The package is new and thus only supports PHP >=7.0
.
How To Basic
Installation
- Install composer
- Open your project folder in terminal
- Enter
composer require dnl-blkv/simple-php-enum
- Wait for the composer to finish the job
- Now, you can start using the Simple PHP Enums as described below!
Defining Enums
Defining a basic Enum with the package is straightforward:
Here null
means auto-determined ordinal value, or auto-ordinal. The default auto-ordinal is 0
. The further auto-ordinal values are determined as {previous ordinal} + 1
.
Enum constant names MUST be PSR-1-compliant AND start from a capital letter. If a constant name does not conform with the rules, the constant is ignored.
Creating
Once the class is defined, the enums can be acquired as:
Or:
Or:
In the examples above, if the name or the ordinal is not defined, exceptions will be thrown (UndefinedEnumNameException
and UndefinedEnumOrdinalException
correspondingly).
Accessing
You can access the name (string representation) and the ordinal (numeric representation) of the enum:
Comparison
Equality
The enums can be checked for equality as such:
Intuitively, two enums of different types are never equal. If we have an enum of type SomeOtherEnum
with const VALUE = 0;
then the following holds:
Comparison by Ordinal
It is also possible to compare the Simple PHP Enums by their ordinal values. There are four methods defined for this, as shown below:
If two enums of different types are compared, the InvalidArgumentException
is thrown.
How To Advanced
Defining Enums with Custom Ordinals
Besides letting the library assign the ordinals automatically, you could manually assign custom integer values to the ordinals:
In this case the enums will be defined as following:
Duplicate Ordinals
Similarly to the vanilla C/C++ enums, this Simple PHP Enums allow for duplicate ordinals. This may be used for tackling such cases as a default value:
For the enum defined above, the following will hold:
If you are getting an enum with duplicate ordinal using a magic method or by name, it works as usual.
However, if you get it by an ordinal, the behavior is slightly different, and you have two options as shown below:
More Equality
The Simple PHP Enum library only creates each enum object once and then reuses it. Therefore, the enums are comparable with ===
or its alias isSame
. This kind comparison is stricter than isEqual
. Whereas isEqual
only accounts for the enum type and ordinal, isSame
also takes the name
into account:
Checking Existence of Names and Ordinals
If you wish to check whether or not certain enum type has a given name or ordinal, there are methods allowing you to easily do so:
Converting to String
The enums have an embedded magical mechanism for serialization:
Notes
Extension
All the internals of the AbstractEnum
class are either public
or protected
. Therefore, it is completely open for extension and allows you to build your own, more complex constructions on top of it.
Use with Databases
If you opt to use these enums with databases and store the ordinals, I would recommend to make sure that no stored enum has duplicate ordinals. Otherwise, it could happen that you store DEFAULT = 0
, but receive IPA = 0
upon recreation.