PHP code example of linna / filter
1. Go to this page and download the library: Download linna/filter 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/ */
linna / filter example snippets
//using rule name
$rule = 'age: number, numbercompare < 30';
//using alias
$rule = 'age: n, nc < 30';
use Linna\Filter\Filter;
$f = new Filter();
$f->filter(20, 'number numberinterval >< 15 25');
//int 0
var_dump($f->getErrors());
//array (size=1)
// 'data' =>
// array (size=0)
// empty
var_dump($f->getMessages());
//array (size=1)
// 'data' => int 20
var_dump($f->getData());
use Linna\Filter\Filter;
//override $_POST superglobal for simulate data from user form
$_POST = [
'email' => '[email protected] ',
'password' => 'p4ssw0rd200!',
'age' => '25',
'born' => '1980-06-01',
];
//create instance
$fm = new Filter();
$fm->filter($_POST, [
'email: ize=0)
// empty
// 'age' =>
// array (size=0)
// empty
// 'born' =>
// array (size=0)
// empty
var_dump($fm->getMessages());
//array (size=4)
// 'email' => string '[email protected] ' (length=15)
// 'password' => string 'p4ssw0rd200!' (length=12)
// 'age' => int 25
// 'born' => string '1980-06-01' (length=10)
var_dump($fm->getData());
use Linna\Filter\Filter;
$f = new Filter();
$f->filter(20, 'number numberinterval >< 15 25');
$errors = $filter->getErrors();
$messages = $filter->getMessages();
$data = $filter->getData();
use Linna\Filter\Filter;
$f = new Filter();
$result = $f->filter(20, 'number numberinterval >< 15 25');
//or with a single expression
$result = (new Filter())->filter(20, 'number numberinterval >< 15 25');
$errors = $result->errors();
$messages = $result->messages();
$data = $result->data();
//override $_POST superglobal for simulate data from user form
$_POST = [
'email' => '[email protected] ',
'password' => 'p4ssw0rd200!',
'age' => '25',
'born' => '1980-06-01',
];
$rules = [
'email
$rules = [
'email: d stringlencompare >= 12',
'age: number numberinterval >=< 20 30',
'born: date Y-m-d datecompare <= Y-m-d 1990-12-31',
];
$rules = [
'email: d, stringlencompare >= 12',
'age: number, numberinterval >=< 20 30',
'born: date Y-m-d, datecompare <= Y-m-d 1990-12-31',
];
$rules = [
'email: d; stringlencompare >=, 12',
'age: number; numberinterval >=<, 20, 30',
'born: date Y-m-d; datecompare <=, Y-m-d, 1990-12-31',
];
$rules = [
'email: d stringlencompare >= 12',
'age: number numberinterval >=< 20 30',
'born: date "Y m d" datecompare <= "Y m d" "1990 12 31"',
];
$rules = [
"email: mail: nglencompare >= 12',
'age: number numberinterval >=< 20 30',
'born: date \'Y m d\' datecompare <= \'Y m d\' \'1990 12 31\'',
];
$customRules = [];
$customRules[] = new CustomRule(
//alias
['hellocheck'],
//callback
//check if word hello is inside of a phrase
function (string $received): bool {
if (strpos(strtolower($received), 'hello') === false) {
return false;
}
return true;
}
);
$filter = new Filter();
$filter->addCustomRules($customRules);
//test passed
$r = $filter->filter('Hello World', 'hellocheck');
//array (size=1)
// 'data' => string 'Hello World' (length=11)
var_dump($r->data());
//int 0
var_dump($r->errors());
//array (size=1)
// 'data' =>
// array (size=0)
// empty
var_dump($r->messages());
//test fails
$r = $filter->filter('Heo World', 'hellocheck');
//array (size=1)
// 'data' => string 'Heo World' (length=9)
var_dump($r->data());
//int 1
var_dump($r->errors());
//array (size=1)
// 'data' =>
// array (size=1)
// 0 => string 'Value provided not pass CustomRule (hellocheck) test' (length=52)
var_dump($r->messages());
$customRules = [];
$customRules[] = new CustomRule(
//alias
['emailtoletters'],
//callback
//replace dot and at chars with literal name
function (string &$received): void {
$received = str_replace('@', ' at ', $received);
$received = str_replace('.', ' dot ', $received);
}
);
$filter = new Filter();
$filter->addCustomRules($customRules);
$r = $filter->filter('[email protected] ', 'emailtoletters');
//array (size=1)
// 'data' => string 'sebastian dot rapetti at alice dot it' (length=37)
var_dump($r->data());
//int 0
var_dump($r->errors());
//array (size=1)
// 'data' =>
// array (size=0)
// empty
var_dump($r->messages());