定制 kennyth01/php-rules-engine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kennyth01/php-rules-engine

最新稳定版本:1.0.5

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.
  • 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

Run the test

./vendor/bin/phpunit tests

License

ISC

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固