2017 © Pedro Peláez
 

library sanitizer

Data sanitizer for PHP with built-in Laravel support.

image

kalfheim/sanitizer

Data sanitizer for PHP with built-in Laravel support.

  • Friday, March 25, 2016
  • by kalfheim
  • Repository
  • 4 Watchers
  • 10 Stars
  • 2,443 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 3 Versions
  • 12 % Grown

The README.md

sanitizer

Build Status StyleCI Code Coverage Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License, (*1)

Data sanitizer for PHP with built-in Laravel support., (*2)

Installation

composer require kalfheim/sanitizer

Usage

``` php use Alfheim\Sanitizer\Sanitizer;, (*3)

// Create a new sanitizer instance by passing an array of rules to the Sanitizer::make method... $sanitizer = Sanitizer::make([ 'email' => 'trim', ]);, (*4)

// Simulate some user input... $input = [ 'email' => 'name@example.com ', // Notice the space. ];, (*5)

// Now we will sanitize some data by passing an array to the sanitize method... var_dump($sanitizer->sanitize($input)); // ['email' => 'name@example.com'], (*6)

// It is also possible to pass the data by reference using the sanitizeByRef method... $sanitizer->sanitizeByRef($input); var_dump($input); // ['email' => 'name@example.com'], (*7)


### Examples ``` php // Wildcard example... $input = [ 'name' => 'Ola nordmann', 'email' => 'Name@example.com ', ]; $sanitizer = Sanitizer::make([ '*' => 'trim', // `*` is a wildcard which will apply to all fields. 'name' => 'ucwords', // Uppercase first char of each word in the name field. 'email' => 'mb_strtolower', // Lowercase each letter in the email field. ]); var_dump($sanitizer->sanitize($input)); // ['name' => 'Ola Nordmann', 'email' => 'name@example.com']

``` php // Multiple rules and arguments..., (*8)

$sanitizer = Sanitizer::make([ 'name' => 'trim|ucwords', // Trim, then uppercase first char of each word. 'email' => 'preg_replace:/+\w+/::{{ VALUE }}', ]);, (*9)

// The email rule might be a handful, but it is really quite simple. // The rule translates to $sanitizedValue = preg_replace('/\+\w+/', '', $value). // It will sanitize an email like name+foo@example.com to name@example.com., (*10)

// The {{ VALUE }} string is a magic constant that the sanitizer will replace // with the value currently being sanitized., (*11)

// By default, the value will be implicitly bound to the first argument in the list, // however, you can place it where ever you need to satisfy the function being called., (*12)

$sanitizer = Sanitizer::make([ 'foo' => 'mb_substr:0:1', 'bar' => 'mb_substr:{{ VALUE }}:0:1', ]);, (*13)

// In the example above, both rules will achieve the same end result., (*14)


## Registrars A registrar allows you to bind custom sanitizer functions to the sanitizer. ``` php use Alfheim\Sanitizer\Sanitizer; use Alfheim\Sanitizer\Registrar\BaseRegistrar; // Create a new registrar instance... $registrar = new BaseRegistrar; // Add custom sanitation rules to the registrar... $registrar->register('palindromify', function (string $value) { return sprintf('%s%s', $value, strrev($value)); }); // Create a new sanitizer and bind the registrar... $sanitizer = Sanitizer::make([ 'number' => 'palindromify', ])->setRegistrar($registrar); $input = $sanitizer->sanitize([ 'number' => '123', ]); var_dump($input); // ['number' => '123321']

Laravel Support

Register the service provider in your config/app.php as per usual..., (*15)

Alfheim\Sanitizer\SanitizerServiceProvider::class,

Extending the FormRequest

This is where the package shines. By extending the Alfheim\Sanitizer\Laravel\FormRequest on your base App\Http\Requests\Request class (instead of the default Illuminate\Foundation\Http\FormRequest), you'll be able to define sanitation rules in a sanitize method on the given form request, similar to how you define validation rules in the rules method., (*16)

Let me show you in code..., (*17)

``` php // app/Http/Requests/Request.php, (*18)

namespace App\Http\Requests;, (*19)

use Alfheim\Sanitizer\Laravel\FormRequest; // Instead of Illuminate\Foundation\Http\FormRequest, (*20)

abstract class Request extends FormRequest { // }, (*21)


That's it! Now it's trivial to define sanitation rules on your form requests... ``` php // app/Http/Requests/FooRequest.php namespace App\Http\Requests; class FooRequest extends Request { // Sanitation rules... public function sanitize() { return [ 'name' => 'trim|ucwords', 'email' => 'trim|mb_strtolower', ]; } // And of course, validation is defined as per usual... public function rules() { return [ 'name' => 'required', 'email' => 'required|email', ]; } }

For completeness, I'll show you the controller..., (*22)

``` php namespace App\Http\Controllers;, (*23)

use App\Http\Requests\FooRequest;, (*24)

class FooController extends Controller { public function create(FooRequest $request) { // At this point, the $request will be both sanitized and validated. // You may go ahead and access the input as usual:, (*25)

    $request->all();
    $request->input('name');
    $request->only(['name', 'email']);
    // etc...
}

}, (*26)


### Helper Trait #### `Alfheim\Sanitizer\Laravel\SanitizesRequests` This trait adds a `sanitize` method on the class. May be useful if you want to sanitize user input in a controller without setting up a custom request class (however, it _can_ be used from anywhere.) ``` php public function sanitize(Illuminate\Http\Request $request, array $ruleset): array

Example usage..., (*27)

``` php namespace App\Http\Controllers\FooController;, (*28)

use Illuminate\Http\Request; use Alfheim\Sanitizer\Laravel\SanitizesRequests;, (*29)

class FooController extends Controller { use SanitizesRequests;, (*30)

public function store(Request $request)
{
    $input = $this->sanitize($request, [
        'name'  => 'trim|ucwords',
        'email' => 'trim|mb_strtolower',
    ]);

    // $input now contains the sanitized request input.
}

}, (*31)


### Registering custom sanitizer functions with Laravel The service provider will register a shared `Alfheim\Sanitizer\Registrar\RegsitrarInterface` instance with the IoC container, which will then be set on subsequent `Alfheim\Sanitizer\Sanitizer` instances. This means you can easily register custom sanitizer functions... ``` php use Alfheim\Sanitizer\Registrar\RegistrarInterface; // Standalone... app(RegistrarInterface::class)->register('yell', $callable); // In a method resolved by the container, perhaps a service provider... public function registerSanitizers(RegistrarInterface $registrar) { $registrar->register('yell', function (string $value) { return mb_strtoupper($value); }); } // You may also resolve an object from the IoC container using `class@method` notation... app(RegistrarInterface::class)->register('foo', 'some.service@sanitizerMethod');

License

MIT © Kristoffer Alfheim, (*32)

The Versions

25/03 2016

dev-master

9999999-dev http://github.com/kalfheim/sanitizer

Data sanitizer for PHP with built-in Laravel support.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Kristoffer Alfheim

laravel filter filtering sanitation sanitizer

25/03 2016

v1.0.1

1.0.1.0 http://github.com/kalfheim/sanitizer

Data sanitizer for PHP with built-in Laravel support.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Kristoffer Alfheim

laravel filter filtering sanitation sanitizer

24/03 2016

v1.0.0

1.0.0.0 http://github.com/kalfheim/sanitizer

Data sanitizer for PHP with built-in Laravel support.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Kristoffer Alfheim

laravel filter filtering sanitation sanitizer