Download the PHP package timetoogo/enums without Composer
On this page you can find all versions of the php package timetoogo/enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download timetoogo/enums
More information about timetoogo/enums
Files in timetoogo/enums
Package enums
Short Description Hipster enums for PHP 5.4+
License MIT
Homepage http://www.github.com/TimeToogo/HIPSTER-Enums
Informations about the package enums
Enums for PHP 5.4+
Installation
Add package to composer.json
.
Or manually require {Path to Enum}/Loader.php
Summary
- An enum is a group of possible values:
DayOfWeek
- An an instance of an enum represents an underlying value:
DayOfWeek::Thursday()->GetValue()
- Two enum instances of the same type, representing the same value are identical:
DayOfWeek::Thursday() === DayOfWeek::Thursday()
- Enums are type safe:
function SetDay(DayOfWeek $DayOfWeek)
- Enums are extensible:
DayOfWeek::Thursday()->GetTomorrow()
- Enum values are serializable:
DayOfWeek::Serialize(DayOfWeek::Thursday())
- Enum values can be represented as a string:
(string)DayOfWeek::Thursday()
An introduction
Congrats, we have successfully reimplemented the boolean using enums.
Lets test it out:
Great it works,
now lets reimplement !
operator.
And test it out:
Cool, but pointless.
Lets do something useful and create the days of the week (as in summary) :
Notice something? The enum values are simply represented by their method name.
Well, lets keep DRY and see how we go:
Well that didn't do much, and it is still extremely verbose! And ugly.
Relax, there is a solution:
That's better! Note that there will be no difference in functionality between the above three examples.
Note that using the \Enum\Values
only works for PHP >5.4.16
If your enums values are represented by the method names, you can utilise the \Enum\Values
trait. This trait contains a single static method _
. You can alias this method to the required enum values, and the aliased methods will return the enum representing the method name string.
Using this trait the equivalent Boolean would become:
Conversion To String
Override the protected string ToString(mixed $Value)
to customize the conversion to string:
Serialization
Enums values can also be fully serialized/unserialized using the Enum\Base::Serialize(Enum\Base $Enum)
and Enum\Base::Unserialize(string $SerializedEnum)
repectively. This will work for any defined enum.
If you want to un/serialize an enum of a specific type, you can can call either method in the context of the required enum type:
Comparison and Equality
An two enum instances of the same type, representing the same value must be equal. Comparison results using ===
operator can be seen below.
As you can see, within a enum there is only ever one instance representing any given value.
Clean comprehension API
Enum\Base::Map(callable $MappingCallback)
Maps the enum instances with the supplied callbackEnum\Base::MapValues(callable $MappingCallback)
Maps the represented values with the supplied callbackEnum\Base::Filter(callable $MappingCallback)
Filters the enum instances with the supplied callbackEnum\Base::FilterByValue(callable $MappingCallback)
Filters the enums by their represented value with the supplied callbackEnum\Base::FirstOrDefault(callable $MappingCallback)
Returns the first enum according to the filter callback or the default value if none is matchedFirstOrDefaultByValue(callable $MappingCallback)
Usage
A more sophisticated example
If your requirements are more complex, enums can represent any serializable value:
We have defined an enum representing some countries and have provided methods to determine information on that data.