Download the PHP package kwaadpepper/enum without Composer
On this page you can find all versions of the php package kwaadpepper/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kwaadpepper/enum
More information about kwaadpepper/enum
Files in kwaadpepper/enum
Package enum
Short Description Enums to use for any project
License MIT
Homepage https://github.com/kwaadpepper/enum
Informations about the package enum
Enum
This package goal is to provide a complete solution to lack a long time php weakness : it do not have any enum
support.
This will change with php 8 enums proposition. In the mean time we still a support for this allowing us to generate generic and extendable applications (instead of have singletons sql tables..).
You will be able to use enums in any project using a simple definition, take a look at example more sophisticated Example
Installation
Via Composer
Usage
All enum have two properties : label
and value
, only value has to be unique. Each enum can have multiple options all written on class comment as static methods, these have to be unique !
- On Any project, take a look on examples to create an enum class.
-
Invoke an enum value
-
Compare enums
-
Print an enum
As you can see enum implements the __toString method which you can override to display the label instead of the value. This default behavior is set like this for a better behavior in laravel.
-
Serialise an enum
Enums implement the JsonSerializable interface
- On a Laravel project you can use enums in multiple ways
-
As a property
This will allow you model to store an enum in database using its value, and then cast the property to an enum when accessing it
-
As a route parameter
Define a route like the following
Then on your controller for the
DayController
example -
As a request parameter using validation
Take a look at this request example.
It use EnumIsValidRule to validate the parameter as being a valid enum value.
Note that you may still have to cast your parameter to int if your enum value is an int, as enum check is strict
-
As a model primary Key
Take a look at the Report enum class from tests
Change log
Please see the changelog for more information on what has changed recently.
Testing
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Jérémy Munsch
- All Contributors
License
MIT. Please see the license file for more information.