Download the PHP package flsouto/pipe without Composer
On this page you can find all versions of the php package flsouto/pipe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package pipe
Short Description apply a chain of filters and validations to input data
License
Informations about the package pipe
Pipe
Overview
This library exposes a simple API that allows you to apply a chain of filters and/or validators against arbitrary data.
Installation
Through composer:
Usage
The following example adds a 'trim' filter and a custom filter which replaces 4, 1 and 0 with a,i and o:
The above code will output:
So all your filter function has to do is to accept a value, modify and return it.
Validation
The next example uses the same API but this time makes sure the input value doesn't have a number '4'.
The above snippet outputs:
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.
Using Filters and Validation Together
In the following code snippet you have a filter happening before the validation, so the validation does not failed.
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.
Defining a Fallback
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:
Use the fallback method to change the default value:
The output will be:
Notice: the fallback value is also used when the input value is null. Example:
The output will be:
However this behaviour does not follow on empty strings:
Customizing the fallback behaviour
If you want, for instance, to fallback on null, empty string or zero, you have to provide the second parameter to the fallback method:
The output will be:
Alternative syntax
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:
Final Thoughts
There are no final thoughts - this is just the beginning (: