di / expression-parser
此包允许以灵活的方式评估(解析并映射)大量数据,提供各种处理函数
0.2.2
2018-04-19 08:39 UTC
Requires
- localheinz/json-normalizer: dev-master
- nikic/php-parser: ^4.0@dev
- phpunit/phpunit: ^7.1
- tightenco/collect: ^5.6
Requires (Dev)
- phpmd/phpmd: dev-master
- squizlabs/php_codesniffer: ^3.0@dev
README
此包允许以灵活的方式评估(解析并映射)大量数据,提供各种处理函数
🔩 安装
composer install di/expression-parser
⚒ 使用
// Signature
$expression = new Expression(string $expression[, array $mappings = []]);
👀 示例
use DI\ExpressionParser\Expression;
$expression = 'or_x(equal([attr1], 1), in_array(explode([keywords]), "hello"))';
$mappings = [
'attr1' => 1,
'keywords' => 'hello,world',
];
$ex = new Expression($expression, $mappings);
echo $ex->value(); // true
标准函数处理器
🔗 参数替换
📥 输入
new Expression(
'[attr1]',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: 1
🔗 使用 has() 函数进行参数替换
📥 输入
new Expression(
'has([attr1])',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: true
🔗 使用 in_array() 函数和标量值进行替换
📥 输入
new Expression(
'in_array([keywords], "hello")',
[
'keywords' => [
'hello',
'world',
],
]
)
📤 输出: true
🔗 嵌套 in_array() 和 explode() 函数及标量值
📥 输入
new Expression(
'in_array(explode([keywords]), "hello")',
[
'keywords' => 'hello,world',
]
)
📤 输出: true
🔗 使用 matches_in_array() 函数进行替换
📥 输入
new Expression(
'matches_in_array([keywords], "pool")',
[
'keywords' => [
'swimming pool',
],
]
)
📤 输出: true
🔗 嵌套 explode()、is_empty() 和函数
📥 输入
new Expression(
'is_empty(explode([keywords]))',
[
'keywords' => '',
]
)
📤 输出: true
🔗 使用内联参数替换的 implode()
📥 输入
new Expression(
'implode(([attr1],[attr2]))',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: hello world
🔗 使用内联参数替换和分隔符标志的 implode()
📥 输入
new Expression(
'implode(([attr1],[attr2]), ",")',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: hello,world
🔗 使用数组替换的 explode()
📥 输入
new Expression(
'explode([Rooms])',
[
'Rooms' => 'Pantry,Study',
]
)
📤 输出: ['Pantry', 'Study']
带有一个或多个标志的标准处理器
🔗 使用数组替换和分隔符标志的 explode()
📥 输入
new Expression(
'explode([Rooms], ";")',
[
'Rooms' => 'Pantry;Study',
]
)
📤 输出: ['Pantry', 'Study']
🔗 使用 count 和 nullable 标志的 get() 函数
📥 输入
new Expression(
'get([attr1], {"count":true, "nullable":false})',
[
'attr1' => [
'a',
'b',
'c',
],
]
)
📤 输出: 3
🔗 在 get() 函数中带有 map 标志的嵌套映射
📥 输入
new Expression(
'get([attr1], {"map":{"a":1, "b": 2, "c": 3}})',
[
'attr1' => 'b',
]
)
📤 输出: 2
使用 sensitive 标志在数组中进行大小写敏感匹配
📥 输入
new Expression(
'matches_in_array([keywords], "pool", {"sensitive":true})',
[
'keywords' => [
'Swimming Pool',
],
]
)
📤 输出: false
逻辑处理器
🔗 equal() 函数
📥 输入
new Expression(
'equal([attr1], 1)',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: true
🔗 great_than() 函数
📥 输入
new Expression(
'great_than([attr1], 0)',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: true
🔗 嵌套 not() 和 equal() 函数
📥 输入
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: true
🔗 嵌套 not() 和 equal() 函数
📥 输入
new Expression(
'not(equal([attr1], 2))',
[
'attr1' => 1,
'attr2' => 2,
]
)
📤 输出: true
具有无限嵌套级别的复杂函数
🔗 使用嵌套 and_x() 的多个函数参数替换
📥 输入
new Expression(
'and_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
📤 输出: true
🔗 使用嵌套 or_x() 的多个函数参数替换
📥 输入
new Expression(
'or_x(equal([attr1], 1), in_array(explode([attr2]), "hello"))',
[
'attr1' => 1,
'attr2' => 'hello,world',
]
)
📤 输出: true
🔗 使用嵌套 or_x() 和 not() 的多个函数参数替换
📥 输入
new Expression(
'not(or_x(equal([attr1], 1), in_array(explode([attr2]), "word")))',
[
'attr1' => 2,
'attr2' => 'hello,world',
]
)
📤 输出: true
😳 使用闭包进行多次嵌套
📥 输入
new Expression(
'first(take(sort(filter([attr1], [filter_func]), [dir]), [offset]))',
[
'attr1' => [
10,
30,
20,
],
'filter_func' => function (ExpressionParser $context, $value) {
return array_filter($value, function ($item) use ($context) {
return $item < $context->getMappings('filter_attr');
});
},
'filter_attr' => 30,
'dir' => 'desc',
'offset' => 1,
]
)
📤 输出: 20
Laravel Collection 辅助函数
该包已经内置了对 Laravel Collection 辅助函数的支持。有关它支持的函数的更多信息,请参阅原始 Laravel Collection 文档页面。
🔗 Laravel Collection 函数的示例用法
📥 输入
new Expression(
'first(collect([attr1], [attr2]))',
[
'attr1' => 'value 1',
'attr2' => 'value 2',
]
)
📤 输出: 'value 1'
通过自定义处理器扩展
为了扩展或覆盖当前功能,您需要将您自己的处理器类名添加到 config/handlers.php 文件中
use DI\ExpressionParser\Handlers\Logical;
use DI\ExpressionParser\Handlers\Standard;
use DI\ExpressionParser\Handlers\LaravelCollectionAdapter;
return [
Standard::class,
Logical::class,
LaravelCollectionAdapter::class,
// Add custom expression handlers here:
// \Acme\Handlers\CustomHandler::class,
// 'Acme\Handlers\CustomHandler',
];
😍 贡献
请随意分支并帮助开发。