Sanitization library for PHP and the Laravel framework.
You can install the package via composer:
composer require peeyush-budhia/sanitizer
use Peeyush\Sanitizer\Sanitizer;
$data = [
'name' => ' peeyush ',
'birth_date' => '1987-09-10',
'email' => 'Peeyush.Budhia@Gmail.CoM',
'phone' => '(000)-000-00-00',
'json' => '{"name":"Peeyush"}',
];
$filters = [
'name' => 'trim|capitalize',
'birth_date' => 'trim|format_date:"Y-m-d","F j, Y"',
'email' => ['trim', 'lowercase'],
'phone' => 'digit'
'json' => 'cast:array',
];
$sanitizer = new Sanitizer($data, $filters);
var_dump($sanitizer);
Will return:
[
'name' => 'Peeyush',
'birth_date' => 'September 10, 1987',
'email' => 'peeyush.budhia@gmail.com',
'phone' => '0000000000'
'json' => ['name' => 'Peeyush'],
];
- Register the
provider
and update thealias
of Sanitizer inconfig/app.php
as under:
'providers' => [
/*
* Package Service Providers...
*/
\Peeyush\Sanitizer\SanitizerServiceProvider::class,
]
'aliases' => [
/*
* Package aliases...
*/
'Sanitizer' => \Peeyush\Sanitizer\Facade\SanitizerFacade::class,
],
- Now Publish
request.stub
andtests
directory. To publish, run the following command on terminal, in the document root of the application:
php artisan vendor:publish --provider="Peeyush\Sanitizer\SanitizerServiceProvider"
- Use the Sanitizer through the Facade:
$data = Sanitizer::make($data, $filters)->sanitize();
- Use SanitizeInput trait to Sanitize input in your FormRequests. Please note you need to add filters method which returns the filters you want to apply to the input.
namespace App\Http\Requests;
use Peeyush\Sanitizer\SanitizeInput;
class ExampleRequest extends Request
{
use SanitizeInput;
public function filters()
{
return [
'name' => 'trim|capitalize',
];
}
}
- To auto generate the Request file run the following command on terminal, in the document root of the application:
php artisan make:Request ExampleRequest
Filter | Description |
---|---|
trim |
Trims a string |
escape |
Escapes HTML and other special characters |
lowercase |
Converts a string to lowercase |
uppercase |
Converts a string to UPPERCASE |
capitalize |
Converts a string to Title Case |
cast |
Casts a variable into the given type. Options are: int , float , string , bool , object , array and Laravel Collection collection |
format_date |
Converts the date format. It takes two arguments, first date's given format second date's target format |
strip_tags |
Strip HTML and PHP tags |
digit |
Get only digit characters from a string |
- Use Closure or Class that implements
Peeyush\Sanitizer\Contracts\Filter
interface:
class RemoveStringFilter implements \Peeyush\Sanitizer\Contracts\Filter
{
public function apply($value, array $options)
{
return str_replace($options, '', $value);
}
}
$filters = [
'remove_strings' => RemoveStringsFilter::class,
];
$sanitize = new Sanitizer($data, $filters)
The MIT License (MIT). Please see License File for more information.