reliv/white-rat
Composer 安装命令:
composer require reliv/white-rat
包简介
A whitelisting library for PHP that supports deep arrays
README 文档
README
A whitelisting library for PHP that supports deep arrays
Installation
composer require reliv/white-rat
Use
To use as a stateless service:
use Reliv\WhiteRat\Filter; $filter = new Filter(); $rules = /* White-list rules go here; see further explanation below */; // Optional safety check; throws an exception if $rules is not valid $filter->validate($rules); $subject = [/* Data to be filtered */]; $filteredSubject = $filter($subject, $rules);
To pre-compile a reusable filter as an invokable whitelist:
use Reliv\WhiteRat\Whitelist; $whitelist = new Whitelist([ /* White-list rules go here; see further explanation below */ /* The rules will be validated upon construction */ ]); $subject = [/* Data to be filtered */]; $filteredSubject = $whitelist($subject);
Whitelist Rules
Whitelist rule sets are designed to closely mirror the structure of the data they are applied to. A rule set takes the form of an array that is a mix of associative and indexed values, although the order of indexed values is irrelevant. When a value is indexed, it must be a string. When it is associative, it must be either an array or a boolean. Each string, whether it is a key or a value, correlates with a key in the data.
If a string appears as an indexed value, the correlating key in the data, including all fields below it, are whitelisted.
If a string appears as a key, and the value is a boolean, it indicates whether the associated data is whitelisted or not.
If a string appears as a key, and the value is an array, this indicates a more specific whitelist rule for sub-keys of the associated data. Whitelisting rules then proceed recursively.
It is also possible to whitelist indexed arrays. To do this, create an array within in array, where the sub-array is the only child of its parent and is an indexed child. This looks like a set of double brackets, and we refer to it as the "double-array."
Whitelist rules are validated upon construction of the whitelist. An exception of type Reliv\WhiteRat\WhitelistValidationException will be thrown if there are any problems detected in the rules given, and the path to the rule and an explanation of the error will be provided.
By default, no fields are whitelisted and all data will be filtered out, leaving you with an empty array. However, any fields present in the whitelist but absent in the data being filtered are ignored in the whitelist. This means its safe to whitelist optional data.
Examples
$whitelist = new Whitelist([ 'foo', 'bar' => true, 'bob' => false, 'baz' => [ 'flip' => true, 'flop' => [ ['flummox'] ], 'quux', ] ]); $data = [ 'foo' => 'FOO!', 'bar' => 'BAR!', 'bob' => 'BOB!', 'baz' => [ 'flip' => 'FLIP!', 'flop' => [ ['flimsy' => 111, 'flummox' => 222], ['flummox' => 333, 'flopsy' => 444] ] ] ] $result = $whitelist($data); var_dump($result);
Output:
array(3) {
["foo"] => "FOO!"
["bar"] => "BAR!"
["baz"] => array(2) {
["flip"] => "FLIP!"
["flop"] => array(2) => {
array(1) => {
["flummox"] => int(222)
}
array(1) => {
["flummox"] => int(333)
}
}
}
}
reliv/white-rat 适用场景与选型建议
reliv/white-rat 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.48k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 reliv/white-rat 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 reliv/white-rat 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: ISC
- 更新时间: 2018-05-01