PHPackages                             flsouto/pipe - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. flsouto/pipe

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

flsouto/pipe
============

apply a chain of filters and validations to input data

1.0.3(5y ago)12291PHP

Since Oct 30Pushed 5y agoCompare

[ Source](https://github.com/flsouto/pipe)[ Packagist](https://packagist.org/packages/flsouto/pipe)[ RSS](/packages/flsouto-pipe/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (5)Used By (1)

Pipe
====

[](#pipe)

Overview
--------

[](#overview)

This library exposes a simple API that allows you to apply a chain of filters and/or validators against arbitrary data.

Installation
------------

[](#installation)

Through composer:

```
composer require flsouto/pipe

```

Usage
-----

[](#usage)

The following example adds a 'trim' filter and a custom filter which replaces 4, 1 and 0 with a,i and o:

```
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:

```
fabio

```

So all your filter function has to do is to accept a value, modify and return it.

Validation
----------

[](#validation)

The next example uses the same API but this time makes sure the input value doesn't have a number '4'.

```
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:

```
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.

Using Filters and Validation Together
-------------------------------------

[](#using-filters-and-validation-together)

In the following code snippet you have a filter happening before the validation, so the validation does not failed.

```
$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.

Defining a Fallback
-------------------

[](#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:

```
$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

```

Use the fallback method to change the default value:

```
$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:

```
default

```

**Notice:** the fallback value is also used when the input value is null. Example:

```
$pipe = new Pipe();
$pipe->fallback('default');
$result = $pipe->run(null);

echo $result->output;
```

The output will be:

```
default

```

However this behaviour does not follow on empty strings:

```
$pipe = new Pipe();
$pipe->fallback('default');
$result = $pipe->run('');

var_dump($result->output);
```

```
string(0) ""

```

### Customizing the fallback behaviour

[](#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:

```
$pipe = new Pipe();
$pipe->fallback('default',[null,'',0]);
$result = $pipe->run('');

echo $result->output;
```

The output will be:

```
default

```

Alternative syntax
------------------

[](#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:

```
  	$pipe = Pipe::create([
  		'trim',
  		function($value){ return str_replace('_','/',$value); }
  	]);
```

Final Thoughts
--------------

[](#final-thoughts)

There are no final thoughts - this is just the beginning (:

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~515 days

Total

4

Last Release

1984d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6875ef6cc1a877629f7133a3b7118a6d5871f5e04c02fa96e9831b4bd58c0926?d=identicon)[flsouto](/maintainers/flsouto)

---

Top Contributors

[![f4bi0](https://avatars.githubusercontent.com/u/6615387?v=4)](https://github.com/f4bi0 "f4bi0 (8 commits)")[![flsouto](https://avatars.githubusercontent.com/u/22966756?v=4)](https://github.com/flsouto "flsouto (1 commits)")

### Embed Badge

![Health badge](/badges/flsouto-pipe/health.svg)

```
[![Health](https://phpackages.com/badges/flsouto-pipe/health.svg)](https://phpackages.com/packages/flsouto-pipe)
```

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
