Download the PHP package zachflower/eloquent-interactions without Composer
On this page you can find all versions of the php package zachflower/eloquent-interactions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zachflower/eloquent-interactions
More information about zachflower/eloquent-interactions
Files in zachflower/eloquent-interactions
Package eloquent-interactions
Short Description An implementation of the interactor pattern for Laravel.
License MIT
Informations about the package eloquent-interactions
Eloquent Interactions
Eloquent Interactions manages application-specific business logic. It's an implementation of the command pattern in PHP for Laravel, and is inspired by the ActiveInteraction library in Ruby.
Eloquent Interactions gives you a place to put your business logic. It also helps you write safer code by validating that your inputs conform to your expectations, and provides a platform for creating discrete, easily testable code.
Installation
To install Eloquent Interactions, require the library via Composer:
Eloquent Interactions is built with Laravel 7.0+ in mind, and has tests currently validating compatibility with Laravel 7, 8, 9, and 10 on PHP 7.2.5 through 8.3. If you find any issues with your specific version of Laravel or PHP, please open an issue and I will do my best to address it.
Basic Usage
To get started with Eloquent Interactions, let's first create a new Interaction. Interactions typically live in the app/Interactions
directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json
file. All Eloquent Interactions extend the \ZachFlower\EloquentInteractions\Interaction
abstract class.
The easiest way to create an Interaction is using the make:interaction
Artisan command:
Now, let's take a look at the base Interaction that was created by the make:interaction
command above:
Once generated, every interaction will require the following two components:
- Input Validations. The class
$validations
property utilizes the built-in Laravel validator to define and validate the expected input of a given Interaction. Alternatively, the$validations
property can be replaced with avalidations()
method. - Business Logic. The
execute()
method takes the provided input—after it passes validation, of course—and executes any necessary business logic on it. Each input you defined will be available. If any of the inputs are invalid,execute()
won't be run.
Given that information, let's update the generated Interaction into something usable:
To execute the Interaction, you can call the static run()
method on the class. As the Interaction's $validations
property defines the expected inputs, a simple key-value array should be passed to run()
with the expected input. This method will return a new instance of the \ZachFlower\EloquentInteractions\Outcome
class. To check the success of the outcome, a boolean $valid
property will be set on the Outcome
object, with TRUE
meaning the input validation passed, and FALSE
meaning it failed. If the validation failed, all validation errors will be stored in the $errors
property on the Outcome
object. If validation passes, the value returned from the execute()
method will be stored in the $result
property on the Outcome
object.
If you would rather deal with error handling on your own, you can pass TRUE
as a second parameter to the run()
method. This, for lack of a better word, will execute the Interaction "dangerously," meaning that any defined errors will be thrown as exceptions of the type \ZachFlower\EloquentInteractions\Exceptions\ValidationException
instead.
Validations
Eloquent Interactions relies heavily on the build-in Laravel validator. This means that any validation method available within a Laravel application will also be available to the Eloquent Interactions validator. That said, there is currently one custom validator (with more on the horizon) to better facilitate the backend-nature of Eloquent Interactions.
Advanced Validators
If the built-in validators aren't powerful enough for your needs, you can use a validations()
method in lieu of the $validations
property. For example, let's say that we want to use a class to validate the meters
parameter in the above examples. Our interaction would change to look something like this:
Objects
In some instances, it might be desireable to validate the type of an object. For example, if we wanted to validate that an input parameter is User
model, the following validation rule may be used:
In a nutshell, this validator checks the instanceof
of an input parameter against the defined validation. This is especially useful when validating whether or not a provided object is a child of the defined validation object.
Errors
In addition to the built-in validation errors, Eloquent Interactions also has support for custom validation errors directly within the execute()
method. This can be accomplished by utilizing the Laravel validator's own add()
method directly on its errors()
method:
It is important to note that, while adding custom validation errors within the execute()
method will mark the Outcome
as invalid and return the expected error messages, what it won't do is halt Interaction execution, so any business logic in the execute()
method will be executed as normal unless special steps are taken.
Contributing
Please read through the contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.
For personal support requests, please use Gitter to get help.
Versioning
For transparency into the release cycle and in striving to maintain backward compatibility, Eloquent Interactions is maintained under the Semantic Versioning guidelines. Sometimes I screw up, but I'll adhere to those rules whenever possible.
See the Releases section of the GitHub project for changelogs for each release version of Eloquent Interactions.
Support
The issue tracker is the preferred channel for bug reports, feature requests and submitting pull requests.
Copyright and License
Code and documentation copyright 2024 Zachary Flower. Code released under the MIT license.
All versions of eloquent-interactions with dependencies
illuminate/validation Version ^7.0|^8.0|^9.0|^10.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0
illuminate/console Version ^7.0|^8.0|^9.0|^10.0