Download the PHP package matthewpageuk/laravel-bitty-enums without Composer
On this page you can find all versions of the php package matthewpageuk/laravel-bitty-enums. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download matthewpageuk/laravel-bitty-enums
More information about matthewpageuk/laravel-bitty-enums
Files in matthewpageuk/laravel-bitty-enums
Package laravel-bitty-enums
Short Description This package helps you use bitwise enums in PHP and Laravel.
License MIT
Homepage https://github.com/matthewpageuk/laravel-bitty-enums
Informations about the package laravel-bitty-enums
Laravel Bitwise Enums
This package helps you use bitwise enums in your Laravel application if you choose to, think before you do. It provides an Enum Container, a trait for your model query scopes and a model attribute cast.
You can think of this as a hasMany relationship but using a single integer column to store the data.
Installation
You can install the package into your Laravel project via composer:
Usage
- Create an Enum
- Using the Bitty Enum Container
- Model Attribute Cast
- Scoped Queries
- Config settings
Create an Enum
You can create a new enum
using the bitty-enum:make
Artisan command. This command will create a new enum class in the app/Enums
directory with the cases you supply. It will ensure the values and names are suitable for use with the package.
To use your own enums
with this package they must :
- Implement the
MatthewPageUK\BittyEnums\Contracts\BittyEnum
interface. - Return type
int
- Values be a power of 2 starting from 1 in order
Invalid enums will throw an BittyEnumException
when used with the container.
There is a current limit of 16 cases (bits) per enum. This can be overiding in your config files.
Example of a Colour enum:
Using the Bitty Enum Container
The container is used to store the selected enum values. It is a wrapper around the integer value and provides methods to manage and check the values. It also performs validation on the values you set to prevent accidental misuse.
Creating a container
You can create a new container using the Contract binding in the Laravel app.
This will create a container suitable for the Colour
enum.
You can also use the MatthewPageUK\BittyEnums\Support\Container
directly.
Example usage
Container public methods
Validation
The container also perfoms validation on the values you set, throwing a BittyEnumException
if you try to set an invalid value or have a malformed enum.
Model Attribute Cast
You can cast the integer
column on your models to a BittyEnumContainer
using the MatthewPageUK\BittyEnums\Casts\BittyEnumCast
cast.
You must pass the enum class you intend to use as the second parameter.
Your database column should be a BIGINT
.
@todo see also bit limit on enum, how many can we have?
Example Laravel Model
You can now use the container methods to update and retrieve the enum values direct from your model attribute.
Example Usage
Scoped Queries
To access the scoped queries in your model you need to use the MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope
trait.
You should also ensure your model has the BittyEnumCast set on the column you want to query.
Example Model
Example Queries
Methods accepting multiple choices can be an array of BittyEnum
or a BittyEnumContainer
.
A BittyEnumException
will be thrown if you pass the incorrect type or invalid enum to the query scope.
Config settings
You can set the maximum number of bits for the container in the config file.
Package Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.