Wallogit.com
2017 © Pedro Peláez
apply a chain of filters and validations to input data
This library exposes a simple API that allows you to apply a chain of filters and/or validators against arbitrary data., (*1)
Through composer:, (*2)
composer require flsouto/pipe
The following example adds a 'trim' filter and a custom filter which replaces 4, 1 and 0 with a,i and o:, (*3)
require_once('vendor/autoload.php');
use FlSouto\Pipe;
$pipe = new Pipe();
$pipe->add('trim')->add(function($value){
return str_replace(['4','1','0'],['a','i','o'],$value);
});
$result = $pipe->run(' f4b10 ');
echo $result->output;
The above code will output:, (*4)
fabio
So all your filter function has to do is to accept a value, modify and return it., (*5)
The next example uses the same API but this time makes sure the input value doesn't have a number '4'., (*6)
use FlSouto\Pipe;
$pipe = new Pipe();
$pipe->add(function($value){
if(strstr($value,'4')){
echo 'The value cannot contain the number 4.';
}
});
$result = $pipe->run('f4b10');
echo $result->error;
The above snippet outputs:, (*7)
The value cannot contain the number 4.
So, if your filter prints something out, then it is considered to be a validator instead of a regular filter. The printed error message will be available in the $result->error property., (*8)
In the following code snippet you have a filter happening before the validation, so the validation does not failed., (*9)
$pipe = new Pipe(); $pipe->add(function($value){ return str_replace('4','a',$value); }); $pipe->add(function($value){ if(strstr($value,'4')){ echo 'The value cannot contain the number 4.'; } }); $result = $pipe->run('f4b10');
This is just to ilustrate that filters and validators can work together. Notice the order they are applied is the same order they are added to the pipe., (*10)
The fallback method allows you to define a default value to be returned in case any error occurs. The default fallback is always the input itself:, (*11)
$pipe = new Pipe(); $pipe->add(function($v){ iF(preg_match("/\d/",$v)){ echo "The value cannot contain digits."; } }); $result = $pipe->run($input="My name is 12345"); echo $result->output;
``` My name is 12345, (*12)
Use the fallback method to change the default value: ```php $pipe = new Pipe(); $pipe->fallback('default'); $pipe->add(function($v){ iF(preg_match("/\d/",$v)){ echo "The value cannot contain digits."; } }); $result = $pipe->run('My name is 12345'); echo $result->output;
The output will be:, (*13)
default
Notice: the fallback value is also used when the input value is null. Example:, (*14)
$pipe = new Pipe(); $pipe->fallback('default'); $result = $pipe->run(null); echo $result->output;
The output will be:, (*15)
default
However this behaviour does not follow on empty strings:, (*16)
$pipe = new Pipe(); $pipe->fallback('default'); $result = $pipe->run(''); var_dump($result->output);
string(0) ""
If you want, for instance, to fallback on null, empty string or zero, you have to provide the second parameter to the fallback method:, (*17)
$pipe = new Pipe(); $pipe->fallback('default',[null,'',0]); $result = $pipe->run(''); echo $result->output;
The output will be:, (*18)
default
You can use the addArray method to add an array of filters at once or you can instantiate the Pipe class through the create method which accepts an array of filters too:, (*19)
$pipe = Pipe::create([ 'trim', function($value){ return str_replace('_','/',$value); } ]);
There are no final thoughts - this is just the beginning (:, (*20)