subzeta/ruling
Composer 安装命令:
composer require subzeta/ruling
包简介
An stateless rule engine
README 文档
README
Stateless rule engine to assert statements given a context.
Install
$ composer require "subzeta/ruling" : "^2.0"
Usage example
require '/path/to/vendor/autoload.php'; use subzeta\Ruling\Ruling; $my = new \stdClass(); $my->sensitivity = 80; $my->joyfulness = 10; (new Ruling()) ->given([ 'sensitivity' => $my->sensitivity, 'joyfulness' => $my->joyfulness ])->when( ':sensitivity is greater than 90 or :joyfulness is less than 20' )->then(function() { echo 'Hell yeah, I should listen music right now!'; })->otherwise(function() { echo 'I\'m happy enough, thanks.'; })->execute(); // Outputs: Hell yeah, I should listen music right now!
Calls
There are three main entrances:
interpret
Returns the interpreted rules. Using the example above the output would be: ['80 > 90 || 10 < 20']
assert
Returns a boolean indicating the output. Using the example above the output would be: true
execute
Fires the success or fail callback if defined. Using the example above the output would be: 'Hell yeah, I should listen it!'
Error handling
Different types of exceptions are thrown when something goes wrong:
InvalidContextException
When the provided context isn't valid (accepts: ['string-key-1' => value-1, ..., 'string-key-x' => value-x]).
InvalidRuleException
When the provided rules aren't valid (accepts: 'string' or ['string-1', ..., 'string-x'])
InvalidCallbackException
When the provided success/fail callback isn't callable (accepts: function(){return 'Hey Ho! Let's go!';})
Supported Operators
| Type | Operator | Representation |
|---|---|---|
| Comparison | is greater than | > |
| Comparison | is greater or equal to | >= |
| Comparison | is less than | < |
| Comparison | is less or equal to | <= |
| Comparison | is equal to (alias: is) | == |
| Comparison | is not equal to (aliases: is not, isn't) | != |
| Comparison | same as | === |
| Comparison | not same as | !== |
| Logical | and | && |
| Logical | or | || |
| Containment | contained in (alias: in) | in |
Notes
- It's not necessary to provide callbacks for execute method, it will return a boolean instead as assert does.
- Rules respect the operator precedence and evaluate the parenthesis from right to left.
Testing
$ phpunit
Recursive to do list
- Increase the number of unit tests to prevent bad contexts or bad formatted rules from being executed.
To do
- Improve the interpreted method response.
Changelist
- Allow aliases ("is equal to" can be written as "is" and "is not equal to" as "is not"/"isn't").
- Context values may permit callable functions too.
- Added the strict comparison operators (same as, not same as).
- It can be interesting to implement a kind of dump method to show the interpreted rule.
- Added the "in" operator.
- Context accepts array values.
License
MIT
subzeta/ruling 适用场景与选型建议
subzeta/ruling 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.11k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2016 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「engine」 「assert」 「rule」 「ruler」 「rule engine」 「ruling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 subzeta/ruling 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 subzeta/ruling 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 subzeta/ruling 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convenient array-related routine & better type casting
ContentElement for Contao to generate hr. Simply choose element type "divider" and save.
Magento 2 Discount Amount Limiter
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Business rules engine tools.
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
统计信息
- 总下载量: 12.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-23