Download the PHP package jkbennemann/laravel-foliage without Composer
On this page you can find all versions of the php package jkbennemann/laravel-foliage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jkbennemann/laravel-foliage
More information about jkbennemann/laravel-foliage
Files in jkbennemann/laravel-foliage
Package laravel-foliage
Short Description This packages allows you to validate arbitrary business requirements within your application against a defined set of rules.
License MIT
Homepage https://github.com/jkbennemann/laravel-foliage
Informations about the package laravel-foliage
Laravel Foliage
This packages allows you to validate arbitrary business requirements within your application.
What can be validated?
You can basically validate anything as long as you can make a logical expression out of it.
Examples:
- Extend Laravel Gates/Policies with extensive validations based on your business rules
- Validate rules stored on your database models, eg. Coupon Code availability, Subscription validations, ..
- Perform any other arbitrary data validation within your classes based on e.g. request input, ...
- ...
Your options are basically endless
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
How it works
The whole concept of this package is to enable arbitrary validations of business rules.
To achieve this we will create a binary tree from your set of rules.
The resulting tree can then be validated by a given input of data.
For each rule the required data to validate against will automatically be taken from the provided payload.
Each tree node later will be expressed as an array, containing the following data.
Usage
Instantiating new rules
To create new you can either use the class provided by the package or just make use of the Facade for an easy and expressive API.
Basic instantiation
Facade usage
A sample representation of a simple and
rule will look like this
Use Aliases for rule payload
If you want to use the same rule multiple times, with different options you will need to specify an alias to the rule.
A alias basically overrides the argument name for the payload to validate against.
Assuming you as an administrator want to perform an action on behalf of a user of your application.
You as the administrator have the right to do so, but the user under consideration itself has not.
Creating rule by structured tree data
Eventually you want to store the tree structure inside your database to validate against eloquent models.
In this case you model should implement the HasValidationRules
trait provided by the package.
This gives you access to those rules.
Your database field should be a json
field if you're using MySQL/MariaDB.
Your model now has access to
From existing array
If you have an array, already in the tree structure, you can create a node from it like so
Validating rules
To validate a created set of rules you can
- invoke the
validate()
method of the Foliage class - create your own validator
- call the
validate()
method on aNode
instance
Exception Handling
By default, the validator throws an exception on first occurring validation error.
If you want to change this behaviour you can instruct the validator not to raise an exception.
Container resolution
The package makes use of the Laravel container, by taking the settings from the config file config/foliage.php
Because of this you can also instantiate a validator by calling the container.
Create a new Validation Rule
If you want to create a new rule, you can run the artisan
command.
This command creates a new rule within the namespace, specified inside the config file.
The content will be
Create a new Payload Class
If you want to create a payload class that can be used for a specific rule, you can run the artisan
command.
This command creates a new payload within the namespace, specified inside the config file.
The content will be
You can add any arguments to the constructor you like, e.g:
Now inside of you validation rule, you can override the used payload class for this rule as follows:
To construct the rule you can now validate your business logic like this:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Jakob Bennemann
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-foliage with dependencies
illuminate/contracts Version ^10.0
spatie/laravel-data Version ^3.11
spatie/laravel-package-tools Version ^1.14.0