Skip to content
/ weasley Public

Utility belt for Slytherin micro-framework.

License

Notifications You must be signed in to change notification settings

rougin/weasley

Repository files navigation

Weasley

Latest Version on Packagist Software License Build Status Coverage Status Total Downloads

A package that provides generators, helpers, and utilities for Slytherin.

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.