popphp/pop-filter
Composer 安装命令:
composer require popphp/pop-filter
包简介
Pop Filter Component for Pop PHP Framework
README 文档
README
Overview
pop-filter is a component for applying filtering callbacks to values that need to be
consumed by other areas of an application. It can be used for input security as well
general input scrubbing as well.
pop-filter is a component of the Pop PHP Framework.
Install
Install pop-filter using Composer.
composer require popphp/pop-filter
Or, require it in your composer.json file
"require": {
"popphp/pop-filter" : "^4.0.4"
}
Quickstart
Simple Filter
If you want to create a simple, single filter and use it to filter some values, you can do this:
$filter = new Pop\Filter\Filter('strip_tags'); $values = [ 'username' => '<b>admin</b>', 'email' => '<a href="mailto:test@test.com">test@test.com</a>' ]; $values = $filter->filter($values);
The values above have been filtered and had the tags stripped:
$values = [
'username' => 'admin',
'email' => 'test@test.com'
];
Extending
The Filterable Trait
The component comes with a trait called Pop\Filter\FilterableTrait. If you wish to have the filter
component and its features included in your application, you can create a class that uses this trait.
With it, your class will be able to add filters and call the methods to filter the necessary values.
These filters can either be an instance of Pop\Filter\FilterInterface (e.g., Pop\Filter\Filter)
or a basic callable.
namespace MyApp\Model use Pop\Filter\FilterableTrait; class User { use FilterableTrait; /** * Filter values * * @param array $values * @return array */ public function filter(array $values) { foreach ($this->filters as $filter) { foreach ($values as $key => $value) { $values[$key] = $filter->filter($value, $key); } } return $values; } }
With the above code, you can create a user model, add filters to it and filter values with it:
$user = new User(); $user->addFilters([ 'strip_tags', new Pop\Filter\Filter('htmlentities', [ENT_QUOTES, 'UTF-8']), ]); $values = [ 'username' => '<script>"Admin"</script>', 'first_name' => '<b>John\'s</b>', 'last_name' => '<b>Doe</b>' ]; $values = $user->filter($values);
The values are now filtered and look like:
$values = [
'username' => '"Admin"',
'first_name' => 'John's',
'last_name' => 'Doe'
];
The tags have been stripped and the entities have been converted to HTML. Notice the
first filter added was the callable strip_tags and the second filter added was an
instance of Pop\Filter\Filter with parameters.
Excludes
Fine-Grained Control
Two properties are available to the filter method within the Pop\Filter\AbstractFilter class.
They are excludeByName and excludeByType. With them, you can have fine-tuned control over
what values actually get filtered. For example, if you don't want to filter any values named
username, you can do this:
$filter = new Pop\Filter\Filter('strip_tags', null, 'username'); $values = [ 'username' => '<b>admin</b>', 'email' => '<a href="mailto:test@test.com">test@test.com</a>' ]; foreach ($values as $key => $value) { $values[$key] = $filter->filter($value, $key); }
Because of the third parameter in the above constructor, the username is excluded from being
filtered and the values look like this:
$values = [
'username' => '<b>admin</b>',
'email' => 'test@test.com'
];
The fourth parameter of the filter constructor is $excludeByType and that is useful for
excluding a number of values at once that are all of the same type, for example, textareas
within a form object.
popphp/pop-filter 适用场景与选型建议
popphp/pop-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.52k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「filter」 「string」 「string filter」 「pop」 「pop php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Nova searchable filter for belongsTo relationships.
Encode integer IDs using a preset or customized symbol set
base62 encoder and decoder also for big numbers with Laravel integration
PHP string processing library.
Symfony DataGridBundle
统计信息
- 总下载量: 26.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 20
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-08