PHP code example of brunogab / inputhelper

1. Go to this page and download the library: Download brunogab/inputhelper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brunogab / inputhelper example snippets


use Brunogab\InputHelper\InputHelper;

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

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

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

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

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

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

$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';
	}
];

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

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

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

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

/** 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
*/

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'
];