peeyush-budhia / sanitizer
Sanitization library for PHP and the Laravel framework.
v1.5
2023-02-17 07:23 UTC
Requires
- php: ^8.0
- illuminate/support: ^9.0|^10.0
- illuminate/validation: ^9.0|^10.0
- nesbot/carbon: ^1.0|^2.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Sanitization library for PHP and the Laravel framework.
Installation
You can install the package via composer:
composer require peeyush-budhia/sanitizer
Core PHP Usage
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'], ];
Laravel Usage
- 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"
After the above process you can use Sanitizer as follows
- 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
Available Filters
Custom Filters
- 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)
Credits
License
The MIT License (MIT). Please see License File for more information.