Download the PHP package my-com/laravel-conditional-actions without Composer
On this page you can find all versions of the php package my-com/laravel-conditional-actions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-conditional-actions
Conditional Actions
This package allows configuring business logic by API without changing your code. This is helpful when you don`t know specific conditions because they defined dynamically by your managers/users/etc.
How it works
Codebase provides predefined conditions, actions, targets and API for mix them into business logic to end users. Objects:
Target
- provides all necessary data for conditions and actions;State
- key-value pairs. Actions should update state when applying;Condition
- condition has methodcheck
, it returns succeed it or not (bool);Action
- action has methodapply
, it changeState
or make any other actions and returns changedState
;
Lifecycle:
Target
creates aState
object;Target
gets all related activeCondition
sorted by priority and run the check on each condition;- For succeeded
Condition
,Condition
gets all related actions and apply them to theState
; Action
returns changedState
which used in next conditions or actions;- After checking all
Condition
,Target
gets newState
toapplyState
method. You can use its state as you needed.
Get started
For example, you have a shop that sells toys. Your marketing runs some promotions for specific toys. If a user buys chests in the past or today is his birthday, "Barbie doll" should have a 10% discount. Promotion starts at 2019/05/01 at 00:00 and finishes at 2019/05/01 at 23:59.
You should create:
Conditions:
- User bought toys in the past (
HasPaidToysCondition
) - Today is his birthday (
TodayIsBirthdayCondition
)
Action:
- "Barbie doll" should have a 10% discount (
DiscountAction
)
For time restrictions (Promotion starts at 2019/05/01 00:00 and finishes at 2019/05/01 23:59) you can use fields starts_at
and ends_at
.
Both conditions should be succeeded. You can use AllOfCondition
condition from the package.
Marketing can use it for promotions without changing your code.
The final scheme for promotion:
Let`s go to implementation!
Install package
Laravel
For versions < 5.5:
Add package service provider to config/app.php
:
For laravel >= 5.5
Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider
Lumen
Register service provider and config in app.php
:
Add migrations
Command options:
Implement Target
Target is an object that provides all necessary data for conditions and actions. It can be also an eloquent model.
Since Toy
- object for conditional actions, it should use EloquentTarget
trait (trait has relationships and some method to get conditions for model)
Implement conditions
Each condition should implement ConditionalActions\Contracts\ConditionContract
contract.
The package has a base abstract class ConditionalActions\Entities\Conditions\BaseCondition
with all contract methods except the check
method.
Implement action
Each condition should implement ConditionalActions\Contracts\ActionContract
contract.
The package has a base abstract class ConditionalActions\Entities\Actions\BaseAction
with all contract methods except the apply
method.
Add conditions to config config/conditional-actions.php
Implement API for adds conditions and actions for Toy
model
You can use eloquent models or any other objects to put business logic into external storage.
The package has basic CRUD for conditions and actions. You can enable it:
Or you can implement your own API. Sample example:
Run conditional actions
P.S.
The package includes conditions and actions:
- Condition
AllOfCondition
- succeeded when all children conditions are succeeded. All children actions will be included to parentAllOfCondition
condition; - Condition
OneOfCondition
- succeeded when any of children conditions are succeeded. All children actions for first succeeded condition will be included to parentOneOfCondition
condition; - Condition
TrueCondition
- always succeeded; - Action
UpdateStateAttributeAction
- Updates an attribute value in the state.
Both conditions and actions have fields:
priority
- execution priority;- nullable
starts_at
andends_at
- enables condition or action at specific time period; parameters
- parameters of conditions or actions;is_inverted
- determines whether the condition result should be inverted.
All versions of laravel-conditional-actions with dependencies
illuminate/database Version ^6.0
illuminate/config Version ^6.0
illuminate/http Version ^6.0
ext-json Version *