PHP code example of flsouto / param

1. Go to this page and download the library: Download flsouto/param 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/ */

    

flsouto / param example snippets



use FlSouto\Param;
t');

echo $param->name();

use FlSouto\Param;

$param1 = Param::get('country');
$param2 = Param::get('country');

var_dump($param1===$param2);

use FlSouto\Param;

$param = Param::get('user_id');
$result = $param->process(['user_id'=>5]);

echo $result->output;

use FlSouto\Param;

$param = Param::get('language');
$param->context([
	'var1' => '...',
	'var2' => '...',
	'language' => 'en'
]);
$result = $param->process();

echo $result->output;

use FlSouto\Param;

$context = new ArrayObject;
$param = Param::get('lang_code');
$param->context($context);
// modify context later
$context['lang_code'] = 'en';
$result = $param->process();

echo $result->output;

use FlSouto\Param;

$param = Param::get('user[contact][email]');
$param->context([
	'user' => [
		'contact' => [
			'email' => '[email protected]'
		]
	]
]);
$result = $param->process();

echo $result->output;

use FlSouto\Param;

$param = Param::get('user');
$param->pipe()->add('trim')->add(function($value){
	if(empty($value)){
		echo 'Cannot be empty';
	}
});
$result = $param->process(['user'=>'  ']);

echo $result->error;

use FlSouto\Param;

$param = Param::get('lang');
$param->fallback('en');
$param->pipe()->add(function($value){
	if(empty($value)){
		echo 'No language selected';
	}
});
// pass an empty context
$result = $param->process([]);

echo $result->output;

use FlSouto\Param;

// place this in a global bootstrap script
Param::setup(function(Param $param){
	$param->context($_REQUEST)->filters()->trim();
});

// Processing some request
$_REQUEST = ['name'=>'Fabio ','age'=>' 666 '];

$name = (new Param('name'))->process()->output;
$age = (new Param('age'))->process()->output;

echo "$name is $age years old";


use FlSouto\Param;
';

Param::get('lang')
	->fallback('en')
	->context($_REQUEST)
	->pipe()
		->add('trim')
		->add(function($value){
			if(empty($value)) echo 'Cannot be empty';
		});

$result = Param::get('lang')->process();




lSouto\ParamContext;

$context = new ParamContext();
$context->param('user_id');
$context->param('lang_code')->filters()->trim();

$context['user_id'] = 5;
$context['lang_code'] = 'en ';

$result = $context->process();

print_r($result->output);


use FlSouto\ParamContext;

$context = new ParamContext();
$context->param('lang_code')->filters()->trim();

$context['lang_code'] = 'en ';
// process individual param:
$result = $context->param('lang_code')->process();

var_dump($result->output);


use FlSouto\ParamContext;

$context = new ParamContext();
$xt->param('user_id')->pipe()->add($($result->errors);


lSouto\Param;

Param::get('name')
	->filters()
	->trim();

$result = Param::get('name')->process(['name'=>' maria ']);

var_dump($result->output);

use FlSouto\Param;

Param::get('money')
	->context(['money'=>'3,50'])
	->filters()
	->replace(',','.');

$output = Param::get('money')->process()->output;

var_dump($output);

use FlSouto\Param;

Param::get('file_name')
	->context(['file_name'=>'My  untitled document.pdf'])	
	->filters()
	->replace('/\s+/', '-');

$output = Param::get('file_name')->process()->output;

var_dump($output);

use FlSouto\Param;

Param::get('style')
	->context(['style'=>'CamelCase'])	
	->filters()
	->replace('/camelcase/i', 'under_scores');

$output = Param::get('style')->process()->output;

var_dump($output);

use FlSouto\Param;

Param::get('name')->filters()->strip('/[^\d]/');

$result = Param::get('name')->process(['name'=>'f4b10']);

var_dump($result->output);

use FlSouto\Param;

Param::get('name')
	->context(['name'=>''])
	->filters()
		->

use FlSouto\Param;

FlSouto\ParamFilters::$errmsg_ilters()
		->or);

use FlSouto\Param;

Param::get('name')
	->filters()
	->ifmatch('/\d/', 'Name cannot contain digits');

$error = Param::get('name')->process(['name'=>'M4ry'])->error;

var_dump($error);

use FlSouto\Param;

Param::get('phone')
	->filters()
	->ifmatch('/[a-z]/i', 'Phone cannot contain letters');

$error = Param::get('phone')->process(['phone'=>'9829574K'])->error;

var_dump($error);

use FlSouto\Param;

Param::get('phone')
	->filters()
	->ifmatch('/[a-z]/i', 'Phone cannot contain letters');

$error = Param::get('phone')->process(['phone'=>''])->error;

var_dump($error);

use FlSouto\Param;

Param::get('date')->filters()->ifnot('/^\d{4}-\d{2}-\d{2}$/','Date is expected to be in the format yyyy-mm-dd');
$error = Param::get('date')->process(['date'=>'10/12/1992'])->error;

var_dump($error);

use FlSouto\Param;

Param::get('login')->filters()->ifnot('\w','Login must contain at least one letter');
$error = Param::get('login')->process([])->error;

var_dump($error);

use FlSouto\Param;

Param::get('description')->filters()->maxlen(30, "Description must be less than %d characters long!");
$error = Param::get('description')
	->process(['description'=>str_repeat('lorem ipsum', 10)])
	->error;


var_dump($error);

use FlSouto\Param;

Param::get('description')->filters()->minlen(10, "Description must be at least %d characters long!");

$error = Param::get('description')->process(['description'=>'Test'])->error;

var_dump($error);

use FlSouto\Param;

Param::get('description')->filters()->minlen(10, "Description must be at least %d characters long!");

$error = Param::get('description')->process(['description'=>''])->error;

var_dump($error);

use FlSouto\Param;

Param::get('age')->filters()->maxval(150, "Age cannot be more than %d!");
$error = Param::get('age')->process(['age'=>200])->error;

var_dump($error);

use FlSouto\Param;

Param::get('age')->filters()->minval(1, "Age cannot be less than %d!");
$error = Param::get('age')->process(['age'=>0])->error;

var_dump($error);

use FlSouto\Param;

Param::get('age')->filters()->minval(1, "Age cannot be less than %d!");
$error = Param::get('age')->process(['age'=>null])->error;

var_dump($error);


lSouto\Param;

Param::get('number')
	->filters()
		->strip('/[^\d]/')
		->er' => '203-40-10/80'
]);

var_dump($result->output);

en

en

en