rougin / weasley
Generators and helpers for the Slytherin framework.
Requires
- php: >=5.3.0
- rougin/slytherin: ~0.9
- symfony/console: ~2.8|~3.0|~4.0|~5.0|~6.0
- vlucas/valitron: ~1.4
Requires (Dev)
- phpunit/phpunit: ~4.2|~5.7|~6.0|~7.0|~8.0|~9.0
- sanmai/phpunit-legacy-adapter: ~6.1|~8.0
README
Weasley is a PHP package that provides generators, helpers, and utilities when writing Slytherin-based applications.
Installation
Install Weasley
via Composer:
$ composer require rougin/weasley
Features
Generators
Access the generator commands through vendor/bin/weasley
in the terminal/command line. To know the more the arguments and options, include the option --help
to the chosen command.
Command | Description |
---|---|
make:check | Creates a new check (validation) class based on Valitron. |
make:handler | Creates a new Slytherin Middleware class. |
make:package | Creates a new Slytherin Integration class. |
make:route | Creates a new HTTP route class. |
HTTP Routes
Controller | Description |
---|---|
HttpRoute | A simple HTTP route class for RESTful APIs. |
JsonRoute | Provides methods for RESTful APIs in JSON format. |
NOTE: In other PHP frameworks, this is also known as Controllers
.
Packages
The following classes below are using the IntegrationInterface
from Slytherin:
Package | Description |
---|---|
Laravel/Eloquent | Based on the illuminate/database (Eloquent). |
Laravel/Blade | Based on the illuminate/view (Blade). |
Laravel/Paginate | Based on the illuminate/pagination. |
Session | A simple implementation of the SessionHandlerInterface. |
NOTE: The mentioned integrations above needs to include their required dependencies first.
HTTP Handlers
The following classes below uses the Middleware component of Slytherin:
Handler | Description |
---|---|
AllowCrossOrigin | Adds additional headers for Cross-origin resource sharing (CORS). |
EmptyStringToNull | Converts the empty strings from request as null . |
JsonContentType | Changes content response to application/json . |
MutateRequest | A middleware that can be extended to mutate/transform values from the request. |
SpoofHttpMethod | Replaces the HTTP verb from _method value. |
TrimStringValue | Trims the strings from an incoming request. |
NOTE: In other PHP frameworks, this is also known as Middlewares
.
Mutators
Mutators are classes that mutates (transforms) specified result (e.g., PSR-07 responses, API data, etc.):
Handler | Description |
---|---|
JsonMutator | Mutates the PSR-07 response in JSON format. |
RestMutator | Mutates the result created from Laravel/Paginate based on Paypal's API Style Guide. |
NOTE: The Laravel/Paginate
package must be included to use the parsing capabilities of RestMutator
.
Validation
Weasley also provides a validation class on top of Valitron. Kindly create a class that extends to the Check
class:
use Rougin\Weasley\Check; class UserCheck extends Check { protected $labels = [ 'name' => 'Name', 'email' => 'Email', 'age' => 'Age', ]; protected $rules = [ 'name' => 'required', 'setting' => 'required|email', 'type' => 'required|numeric', ]; }
Once created, the data can be submitted to the said class for validation:
$check = new UserCheck; $data = /* e.g., data from request */; if ($check->valid($data)) { // $data passed from validation } else { // Get the available errors --- $errors = $check->errors(); // ---------------------------- // Or get the first error only --- echo $check->firstError(); // ------------------------------- }
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Credits
License
The MIT License (MIT). Please see LICENSE for more information.