Download the PHP package grifart/enum without Composer
On this page you can find all versions of the php package grifart/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package enum
Short Description Provides bullet proof enums with behaviours.
License MIT
Informations about the package enum
grifart/enum
Enumeration value object. Enumerate values and behaviour with type-safety.
Repositories gitlab.grifart.cz and github.com.
Sponsored by grifart.com.
Do you like video 📺 instead of reading? Here is one in english and one in czech.
Introduction
Enums represent predefined set of values. The available values are defined statically by each enum class. Each value is represented by an instance of this class in a flyweight manner.
- This enum allows you to add individual behaviour for every enum value (as in Java). This allows you to transform your
switch
es/if
s into more readable composition. (see example bellow) - Checks enum annotations if phpdoc-declared methods are properly declared (will generate docblock for you when not specified or incorrect)
===
,==
and usage ofswitch
es is supported- string or integer scalar keys are supported
- Easily access scalar value of enum
DayOfWeek::MONDAY()->toScalar()
or(string) DayOfWeek::MONDAY()
Also includes:
- It is type safe. By annotating your enumeration type, you are guaranteed that there will be no other values then you declared.
function translateTo(DayOfWeek $day) { ...
- You can get a list of all the possible values
Enum::getAvailableValues()
Installation
This library uses semantic versioning 2.0.
You can safely use ^
constrain in you composer.json
.
Requirements
This library requires PHP 7.1 and later.
Project status & release process
While this library is still under development, it is well tested and should be stable enough to use in production environments.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.1.*.
If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.
Overview
Static methods
- fromScalar() - returns enum instance (value) for given scalar
- getAvailableValues() - returns all values for given type
- provideInstances() - implement to return enum instances or automatically implemented by
Grifart\Enum\AutoInstances
trait.
Instance methods
- toScalar() - return scalar value identifier
- equals() - returns true if the same enum value is passed
- scalarEquals() - returns true if passed scalar value is equal to current value
Simplest enumeration
`
Values with behaviour
This way conditions can be replaced by composition.
This example originally comes from loyalty program domain, continue to full code sample with context.
`
Migrating from class constants [source code]
This guide show how to migrate from classes with constants to \Grifart\Enum
in few simple steps. Continue to example
Adding behaviour to values [source code]
This guide show how to slowly add behaviour to enum values. Step by step. Continue to example
Complex showcase: order lifecycle tracking [source code]
This example contains 5 ways of implementing order state. Continue to example.
Big thanks
- David Grudl for making Nette Tester
- Ondřej Mirtes for making PHP Stan.