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('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();
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);