2017 © Pedro Peláez
 

library input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

image

acid-solutions/input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

  • Wednesday, February 14, 2018
  • by daniel-acid
  • Repository
  • 3 Watchers
  • 2 Stars
  • 1,000 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 2 % Grown

The README.md

Input Sanitizer

Source Code Latest Version Build Status Coverage Status Total Downloads License: MIT SensioLabsInsight, (*1)

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., (*2)

This package simplifies the process drastically., (*3)

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., (*4)

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., (*5)

// 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), (*6)

Call the sanitizer as following:, (*7)

$data = ['null', 'true'];
$sanitized = InputSanitizer::sanitize($data);

$input can be a string, boolean, number, array, object or JSON string, (*8)

Examples of the cleaned data:, (*9)

''      => 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., (*10)

$default can be used to return a default value if the resulting cleaned input is null or false, (*11)

Example:, (*12)

InputSanitizer::sanitize('', 'hello');
// will return 'hello'

$jsonDecodeAssoc is used for decoding JSON.
See php json_decode documentation, (*13)

$jsonDecodeAssoc = true // default is false
$input = json_decode($input, null, $jsonDecodeAssoc);
// will decode your json as associative array (and as object if false)

Credits

The Versions

14/02 2018

dev-master

9999999-dev https://github.com/ACID-Solutions/input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent
by Daniel Lucas

laravel php codeigniter input sanitize fuelphp package clean acid santizer sanitizer

10/11 2017

1.0.2

1.0.2.0 https://github.com/ACID-Solutions/input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent
by Daniel Lucas

laravel php codeigniter input sanitize fuelphp package clean acid sanitizer

18/10 2016

1.0.1

1.0.1.0 https://github.com/ACID-Solutions/input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent
by Daniel Lucas

laravel php codeigniter input sanitize fuelphp package clean acid santizer

16/10 2016

1.0.0

1.0.0.0 https://github.com/ACID-Solutions/input-sanitizer

Input sanitizer to convert strings to booleans, numbers etc.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arthur Lorent
by Daniel Lucas

laravel php codeigniter input sanitize fuelphp package clean acid santizer