Download the PHP package danielebuso/sanitizer without Composer
On this page you can find all versions of the php package danielebuso/sanitizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danielebuso/sanitizer
More information about danielebuso/sanitizer
Files in danielebuso/sanitizer
Package sanitizer
Short Description Data sanitizer and Laravel 7 form requests with input sanitation.
License MIT
Informations about the package sanitizer
Sanitizer
Data sanitizer and Laravel 7/8 form requests with input sanitation
Introduction
Sanitizer provides an easy way to format user input, both through the provided filters or through custom ones that can easily be added to the sanitizer library.
Although not limited to Laravel users, there are some extensions provided for this framework, like a way to easily Sanitize user input through a custom FormRequest and easier extensibility.
Example
Given a data array with the following format:
We can easily format it using our Sanitizer and the some of Sanitizer's default filters:
Which will yield:
It's usage is very similar to Laravel's Validator module, for those who are already familiar with it, although Laravel is not required to use this library.
Filters are applied in the same order they're defined in the $filters array. For each attribute, filters are separered by | and options are specified by suffixing a comma separated list of arguments (see format_date).
Available filters
The following filters are available out of the box:
Filter | Description |
---|---|
trim | Trims a string |
escape | Escapes HTML and special chars using php's filter_var |
lowercase | Converts the given string to all lowercase |
uppercase | Converts the given string to all uppercase |
capitalize | Capitalize a string |
cast | Casts a variable into the given type. Options are: integer, float, string, boolean, object, array and Laravel Collection. |
format_date | Always takes two arguments, the date's given format and the target format, following DateTime notation. |
strip_tags | Strip HTML and PHP tags using php's strip_tags |
digit | Get only digit characters from the string |
filter_if | Applies filters if an attribute exactly matches value |
Adding custom filters
You can add your own filters by passing a custom filter array to the Sanitize constructor as the third parameter. For each filter name, either a closure or a full classpath to a Class implementing the danielebuso\sanitizer\Contracts\Filter interface must be provided. Closures must always accept two parameters: $value and an $options array:
Install
To install, just run:
composer require danielebuso/sanitizer
And you're done! If you're using Laravel, in order to be able to access some extra functionality you must register both the Service provider in the providers array in config/app.php, as well as the Sanitizer Facade:
Laravel goodies
If you are using Laravel, you can use the Sanitizer through the Facade:
You can also easily extend the Sanitizer library by adding your own custom filters, just like you would the Validator library in Laravel, by calling extend from a ServiceProvider like so:
You may also Sanitize input in your own FormRequests by using the SanitizesInput trait, and adding a filters method that returns the filters that you want applied to the input.
To generate a Sanitized Request just execute the included Artisan command:
php artisan make:sanitized-request TestSanitizedRequest
The only difference with a Laravel FormRequest is that now you'll have an extra 'fields' method in which to enter the input filters you wish to apply, and that input will be sanitized before being validated.
License
Sanitizer is open-sourced software licensed under the MIT license
All versions of sanitizer with dependencies
illuminate/validation Version ~5.3|~6.0|~7.0|^8.0
nesbot/carbon Version ~1.0|~2.0