Skip to content

simple helper for sanitize and auto type-casting input data for php

Notifications You must be signed in to change notification settings

brunogab/inputhelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Input Sanitizer with auto Type-Casting

Sanitizer, Filter, Custom Filter, Hepler, Type-Casting for Inputs

  • Apply Filter on your Inputs-data.
  • If no Filter was found/defined, automatic Type-Casting will be apply on Inputs-data.
  • You can defined Keys, witch will returns from Inputs-data.

Table of Contents

Installation

Run composer command:

composer require brunogab/inputhelper

or add to your composer.json and run composer update:

{
	"require": {
		"brunogab/inputhelper": "^1.0"
	}
}

NOTICE:

Some value cannot be decided between bool-type and integer/string type ("1", 1, 0, on, off..)
Bool value check has a higher priority by automatic type-casting:

  • 1 is bool-true (not integer)
  • '1' is bool-true (not string)
  • 'true' is bool-true (not string)
  • 'on' is bool-true (not string)
  • 'yes' is bool-true (not string)
  • 0 is bool-false (not integer)
  • '0' is bool-false (not string)
  • 'false' is bool-false (not string)
  • 'off' is bool-false (not string)
  • 'no' is bool-false (not string)
  • null is bool-false (not null)
  • '' is bool-false (not empty)

Usage

use Brunogab\InputHelper\InputHelper;

$inputhelper = new InputHelper;
$result = $inputhelper->run($inputs, $filters, $keys);

Inputs

Inputs must be an Array:

$inputs = [
	'key_a' => 'Value_A',
	'key_b' => 'value_B',
	'key_c' => 'Value_c',
];

Filters

Filter can be Empty:

$filters = ''; //automatic type-casting will be applied for ALL input value

Filter can be String:

$filters = 'trim'; //trim will be applied for ALL input value

Filter can be String with Pipe:

$filters = 'trim|upper'; //trim and upper will be applied for ALL input value

Filter can be Sequential Array:

$filters = ['trim','upper']; //trim and upper will be applied for ALL input value

Filter can be Associative Array:

$filters = [
	'key_a' => 'trim',
	'key_b' => 'trim|upper',
	'key_c' => ['trim'],
	'key_d' => ['trim', 'upper'],
	'key_e' => ['trim', 'upper', function ($val) {
		return $val.' + closure';
	}],
	'key_f' => function ($val) {
		return $val.' + closure';
	}
];

Notice:
For Inputs where no Filter was found: automatic type-casting will be applied

Keys

Keys can be Empty:

$keys = ''; //Result: (array) Inputs

Keys can be String:

$keys = 'key_a'; //key_a value will be returned, Result: inputs['key_a']

Notice:
If the requested key was not found, Result will be: NULL

Keys can be String with Pipe:

$keys = 'key_a|key_b'; //key_a and key_b value will be returned, Result: array (inputs['key_a'], inputs['key_b'])

Notice:
If none of the requested keys were found, Result will be: NULL

Keys can be Sequential Array:

$keys = [
	'key_a',
	'key_b',
	'key_invalid', //not valid key -> will be ignored
];
//Result: array (inputs['key_a'], inputs['key_b'])

Notice:
If none of the requested keys were found, result will be: NULL

Example

see in example.php

/** Automatic Type-Casting for Boolean Values */
$inputs = [
	'col_bool_1' => '1',
	'col_bool_1_1' => 1,
	'col_bool_true' => 'true',
	'col_bool_on' => 'on',
	'col_bool_yes' => 'yes',

	'col_bool_0' => '0',
	'col_bool_0_0' => 0,
	'col_bool_false' => 'false',
	'col_bool_off' => 'off',
	'col_bool_no' => 'no',

	'col_bool_null' => null,
	'col_bool_empty' => '',
];

/* Result:
array (size=12)
	'col_bool_1' => boolean true
	'col_bool_1_1' => boolean true
	'col_bool_true' => boolean true
	'col_bool_on' => boolean true
	'col_bool_yes' => boolean true

	'col_bool_0' => boolean false
	'col_bool_0_0' => boolean false
	'col_bool_false' => boolean false
	'col_bool_off' => boolean false
	'col_bool_no' => boolean false

	'col_bool_null' => boolean false
	'col_bool_empty' => boolean false
*/

Custom Rule

You can write your custom Code and save into src/Filters/YourcodenameFilter.php

namespace Brunogab\InputHelper\Filters;

class YourcodenameFilter
{
	public function do($val)
	{
		//custom logic
		return is_bool($val) ? 'Y' : 'N';
	}
}
//then simply use your custom filter
$filters = [
	'col_a' => 'yourcodename'
];