flsouto/pipe 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

flsouto/pipe

Composer 安装命令:

composer require flsouto/pipe

包简介

apply a chain of filters and validations to input data

README 文档

README

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:

composer require flsouto/pipe

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

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

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

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

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

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

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

flsouto/pipe 适用场景与选型建议

flsouto/pipe 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 229 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 flsouto/pipe 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 flsouto/pipe 我们能提供哪些服务?
定制开发 / 二次开发

基于 flsouto/pipe 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 229
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 15
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-10-30