jijoel / sanitization-filters
Data sanitizer filters for use with waavi/sanitizer
Installs: 1 846
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- illuminate/support: ^5.0
- nesbot/carbon: ~1.0|~2.0
- waavi/sanitizer: ^1.0.8
Requires (Dev)
- phpunit/phpunit: ~8.0
README
This is a collection of custom sanitization filters to be used in conjunction with Waavi/Sanitizer.
Example
Given a data array with the following format:
$data = [ 'first_name' => 'john', 'last_name' => '<strong>DOE</strong>', 'email' => ' JOHn@DoE.com', 'birthdate' => '06/25/1980', 'jsonVar' => '{"name":"value"}', ];
We can easily format it using our Sanitizer and the some of Sanitizer's default filters:
use \Waavi\Sanitizer\Sanitizer; $filters = [ 'first_name' => 'trim|escape|capitalize', 'last_name' => 'trim|escape|capitalize', 'email' => 'trim|escape|lowercase', 'birthdate' => 'trim|format_date:m/d/Y, Y-m-d', 'jsonVar' => 'cast:array', ]; $sanitizer = new Sanitizer($data, $filters); var_dump($sanitizer->sanitize());
Which will yield:
[ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john@doe.com', 'birthdate' => '1980-06-25', 'jsonVar' => '["name" => "value"]', ];
Available Filters
Installation
Install with composer:
composer require jijoel/sanitization-filters
We also include a Laravel service provider, via auto-discovery. For versions of Laravel prior to 5.5, add it to your providers array in config/app.php
:
'providers' => [ ... Waavi\Sanitizer\Laravel\SanitizerServiceProvider::class, Jijoel\Sanitizer\Laravel\SanitizerServiceProvider::class, ];
Usage
It's recommended to sanitize your data in a FormRequest object before applying rules. The Waavi/Sanitizer package includes a SanitizesInput trait, which handles this automatically for you.
use Waavi\Sanitizer\Laravel\SanitizesInput; class MyFormRequest extends FormRequest { use SanitizesInput; public function filters() { return [ 'name' => 'trim|escape|name', 'email' => 'trim|escape|lower', ]; } public function rules() { return [ 'name' => 'required', 'email' => 'required|email', ]; }
Please note that at this time, in order for this to work, the Sanitizer facade must exist.