acid-solutions/input-sanitizer

This package is abandoned and no longer maintained. No replacement package was suggested.

Input sanitizer to convert strings to booleans, numbers etc.

1.0.2 2017-11-10 15:26 UTC

This package is not auto-updated.

Last update: 2022-04-30 06:33:14 UTC


README

Source Code Latest Version Build Status Coverage Status Total Downloads License: MIT SensioLabsInsight

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 your app/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 the config/app.phpconfig 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)

Credits