kennyth01/php-rules-engine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kennyth01/php-rules-engine

Composer 安装命令:

composer require kennyth01/php-rules-engine

包简介

A PHP rules engine inspired by CacheControl/json-rules-engine, enabling dynamic rule evaluation based on conditions and facts.

README 文档

README

Run tests

kennyth01/php-rules-engine is a lightweight and flexible PHP rules engine that evaluates complex conditional logic using JSON-based rule configurations. It is designed to handle dynamic, reusable, and maintainable rule logic, making it ideal for applications with complex business requirements that must adapt to changing conditions.

This library, inspired by the json-rules-engine, (link) enables developers to define rules with nested conditions, logical operators (all, any, not), and rule dependencies.

Features

  • JSON-Configurable Rules: Easily define rules and conditions in JSON format.
  • Fact-to-Fact Comparison: Compare two dynamic facts at runtime instead of comparing to static values.
  • Rule Dependencies: Reference other rules as conditions to create complex evaluations.
  • Logical Operators: Supports all (AND), any (OR), and not operators, allowing for nested conditions.
  • Custom Events and Failure Messages: Attach custom messages for success or failure, making evaluations easy to interpret.
  • Interpret Rules: Outputs a human readable English interpretation of the condition using logical operators.

Installation

Install via Composer:

composer require kennyth01/php-rules-engine

Basic Example

This example demonstrates an engine for detecting whether a basketball player has fouled out (a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out).

  1. Define the rule (lets assume you store this in rule.player.isFouledOut.json)
{
   "name":"rule.player.isFouledOut",
   "conditions": {
     "any": [
       {
         "all": [
           {
             "fact": "gameDuration",
             "operator": "equal",
             "value": 40
           },
           {
             "fact": "personalFoulCount",
             "operator": "greaterThanInclusive",
             "value": 5
           }
         ],
         "name": "short foul limit"
       },
       {
         "all": [
           {
             "fact": "gameDuration",
             "operator": "equal",
             "value": 48
           },
           {
             "not": {
               "fact": "personalFoulCount",
               "operator": "lessThan",
               "value": 6
             }
           }
         ],
         "name": "long foul limit"
       }
     ]
   },
   "event": {
     "type": "fouledOut",
     "params": {
       "message": "Player has fouled out!"
     }
   },
   "failureEvent": {
      "type": "fouledOut",
      "params": {
         "message": "Player has not fouled out"
      }
    }
 }

  1. Trigger the engine and evaluate
$engine = new Engine();
$rule = json_decode(file_get_contents('rule.player.isFouledOut.json'), true);

$engine->addRule(new Rule($rule));
$engine->addFact('personalFoulCount', 6);
$engine->addFact('gameDuration', 40);

$engine->setTargetRule('rule.player.isFouledOut');

$result = $engine->evaluate();
print_r($result);

  1. Output Example
[
    'type' => 'fouledOut',
    'params' => [
        'message' => 'Player has fouled out!'
    ],
    'facts' => [
        'personalFoulCount' => 6,
        'gameDuration' => 40

    ],
    'interpretation' => '((gameDuration is equal to 40 AND personalFoulCount is >= 5) OR (gameDuration is equal to 48 AND NOT (personalFoulCount is less than 6)))'
]

Advanced Examples

For other examples, refer to the tests directory

Fact-to-Fact Comparison

The engine supports comparing two facts dynamically at runtime. This allows for flexible rule evaluation where comparison values can change based on context.

Example: Speed Limit Check

$engine = new Engine();

$ruleConfig = [
    "name" => "speed.check",
    "conditions" => [
        "all" => [
            [
                "fact" => "currentSpeed",
                "operator" => "lessThanInclusive",
                "value" => ["fact" => "speedLimit"]  // Compare to another fact
            ]
        ]
    ],
    "event" => ["type" => "withinLimit", "params" => ["message" => "Speed is within limit"]],
    "failureEvent" => ["type" => "speeding", "params" => ["message" => "Exceeding speed limit"]]
];

$engine->addRule(new Rule($ruleConfig));
$engine->setTargetRule('speed.check');

// Add both facts dynamically
$engine->addFact('currentSpeed', 55);
$engine->addFact('speedLimit', 60);

$result = $engine->evaluate();
// Result: withinLimit - Speed is within limit

Example: Nested Fact Comparison

$ruleConfig = [
    "name" => "age.verification",
    "conditions" => [
        "all" => [
            [
                "fact" => "user",
                "path" => "$.age",
                "operator" => "greaterThanInclusive",
                "value" => [
                    "fact" => "requirements",
                    "path" => "$.minimumAge"
                ]
            ]
        ]
    ],
    "event" => ["type" => "ageVerified", "params" => []],
    "failureEvent" => ["type" => "ageFailed", "params" => []]
];

$engine->addFact('user', ['age' => 25]);
$engine->addFact('requirements', ['minimumAge' => 18]);

Run the test

./vendor/bin/phpunit tests

License

ISC

kennyth01/php-rules-engine 适用场景与选型建议

kennyth01/php-rules-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.39k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kennyth01/php-rules-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kennyth01/php-rules-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 kennyth01/php-rules-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-08