acid-solutions / input-sanitizer
Input sanitizer to convert strings to booleans, numbers etc.
Installs: 2 562
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^5.6 | ^7.0
- illuminate/support: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~1.5
This package is not auto-updated.
Last update: 2022-04-30 06:33:14 UTC
README
Often when receiving data from a client in an API or from a form request, you'll find yourself running the same data
cleaning operations such as transforming 'false'
to the boolean false
, converting ''
to null
etc. This can be a pain.
This package simplifies the process drastically.
Installation
- Install the package with composer :
composer require acid-solutions/input-sanitizer
Laravel users
- Laravel 5.5+ uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider and the Facade alias.
If you don't use auto-discovery or if you use a Laravel 5.4- version, add the package service provider in the
register()
method from yourapp/Providers/AppServiceProvider.php
:
// input sanitizer // https://github.com/ACID-Solutions/input-sanitizer $this->app->register(AcidSolutions\InputSanitizer\Laravel\InputSanitizerServiceProvider::class);
- Then, add the package facade alias in the
$aliases
array from theconfig/app.php
config file.
'aliases' => [ '...', 'InputSanitizer' => AcidSolutions\InputSanitizer\Laravel\Facades\InputSanitizer::class ]
When this provider is booted, you'll gain access to a InputSanitizer
facade, which you may use in your controllers.
public function index() { $inputs = $request->all(); $sanitizedInputs = \InputSanitizer::sanitize($inputs); }
Without Laravel
InputSanitizer ships with native implementations of the bootloader and facade. In order to use it import class.
// import the package facade use Acid\InputSanitizer\Native\Facades\InputSanitizer; // sanitize your entries $input = ['false', '3', '']; $sanitizedInput = InputSanitizer::sanitize($input); // produces [false, 3, null]
Usage
The only public method in the package is sanitize($input, $default = null, $jsonDecodeAssoc = false)
Call the sanitizer as following:
$data = ['null', 'true']; $sanitized = InputSanitizer::sanitize($data);
$input
can be a string, boolean, number, array, object or JSON string
Examples of the cleaned data:
'' => null 'null' => null 'false' => false 'true' => true 'on' => true '3' => 3 '5.07' => 5.07
When using arrays and objects, the method will sanitize each element in the given input and return an array (or object) with the cleaned values.
$default
can be used to return a default value if the resulting cleaned input is null
or false
Example:
InputSanitizer::sanitize('', 'hello'); // will return 'hello'
$jsonDecodeAssoc
is used for decoding JSON.
See php json_decode documentation
$jsonDecodeAssoc = true // default is false
$input = json_decode($input, null, $jsonDecodeAssoc);
// will decode your json as associative array (and as object if false)