messere/php-value-mask
Composer 安装命令:
composer require messere/php-value-mask
包简介
extract subset of array / object values
README 文档
README
Purpose
Google in their Performance Tips for
APIs, suggest to limit required bandwidth by filtering out unused fields in response. Their APIs
support additional URL parameter fields which asks API to include only specific fields in response.
fields parameter follows a simple syntax, which allows to query for nested keys, multiple keys
or use wildcard to include all fields (see Syntax and Grammar sections below).
This library implements parsing of fields parameter and filtering of arrays / objects.
See also: PSR-15 compatible middleware based on this library: Partial Response PSR-15 Middleware
Usage example
<?php require_once 'vendor/autoload.php'; use messere\phpValueMask\Parser\Parser; use messere\phpValueMask\Parser\ParserException; $parser = new Parser(); $input = [ 'id' => 1, 'resource' => 'book', 'title' => 'Good Omens', 'identifiers' => (object)[ 'isbn' => 'ISBN 83-85100-63-6', 'amazon' => '0060853980', ], 'authors' => [ [ 'firstName' => 'Terry', 'lastName' => 'Pratchett' ], [ 'firstName' => 'Neil', 'lastName' => 'Gaiman' ], ], 'year' => [ 'us' => 1990, 'uk' => 1990, 'pl' => 1992, ], 'publisher' => [ 'us' => 'Workman', 'uk' => 'Gollancz', 'pl' => 'CIA-Books-SVARO', ], ]; $filter = 'title,identifiers/isbn,authors/firstName,*(us,uk),keywords'; try { $filteredInput = $parser->parse($filter)->filter($input); print_r($filteredInput); } catch (ParserException $e) { echo 'Parser error: ' . $e->getMessage(); }
Let's analyze elements of used filter:
titlematches top level element with key title ('Good Omens')identifiers/isbnmatches top level elementidentifiersand then includesisbnelement from matched object ('ISBN 83-85100-63-6')authors/firstNamefinds an array of elements (list) under the keyauthorsand examines all elements, extractingfirstNamefrom each. ('Terry' and 'Neil')*(us,uk)examines all properties and extracts fieldsusanduk. ('1990' and '1990' fromyearelement, 'Workman', 'Gollancz' frompublisher)keywordsdoes not match anything and is silently ignored.
As a result we expect the following output:
Array
(
[title] => Good Omens
[identifiers] => Array
(
[isbn] => ISBN 83-85100-63-6
)
[authors] => Array
(
[0] => Array
(
[firstName] => Terry
)
[1] => Array
(
[firstName] => Neil
)
)
[year] => Array
(
[us] => 1990
[uk] => 1999
)
[publisher] => Array
(
[us] => Workman
[uk] => Gollancz
)
)
ready to serialize to JSON, etc.
Note that library does preserve the structure/nesting of values, but not necessarily types of values - all objects are converted to associative arrays with object's public properties as keys.
Syntax
aselects keyafrom inputa,b,ccomma separated list of elements: selects keysaandbandca/b/cnested elements: selects keycfrom parent elementbwhich in turn has parent elementaa(b,c)multiple elements: selects elementsbandcfrom parent elementaa(b,c/d)multiple elements: from parentaselect elementband elementdnested inca/*/cwildcard: selects elementcfrom all children of elementa*(b,c)wildcard: selects elementsbandcfrom any parent
Etc. See tests for more examples as well as examples of invalid filters.
Grammar
Since Google does not provide detailed grammar for their "fields" language, this package uses the following arbitrarily selected rules, that in author's opinion closely resamble intent of original authors.
In EBNF notation:
Mask = MaskElement | MaskElement , "," , Mask ;
MaskElement = ArrayOfMasks | NestedKeys ;
ArrayOfMasks = Key , "(" , Mask , ")" ;
NestedKeys = Key , [ "/" , NestedKeys ] ;
Key = Wildcard | Identifier ;
Identifier = Letter , { Letter | Digit }
Wildcard = "*" ;
Letter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" |
"N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" |
"a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" |
"n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" |
"_";
Digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "0" ;
Acknowledgements
Library is inspired by:
- Google's API performance tips.
- Similar JavaScript library: nemtsov/json-mask.
License
messere/php-value-mask 适用场景与选型建议
messere/php-value-mask 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47.57k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2018 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「filtering」 「mask」 「partial response」 「array filtering」 「json filtering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 messere/php-value-mask 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 messere/php-value-mask 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 messere/php-value-mask 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Never configure mask permissions for editors manually ever again!
ke_search Indexer for all Mask-Elements
Nova searchable filter for belongsTo relationships.
Small class for checking ip addresses
Symfony DataGridBundle
Modern PHP Sanitization Library
统计信息
- 总下载量: 47.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-24