承接 superruzafa/rules 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

superruzafa/rules

Composer 安装命令:

composer require superruzafa/rules

包简介

A rule engine following the condition -> action paradigm

README 文档

README

Build Status

A rule engine following the condition -> action paradigm

What a rule is?

A rule is build up by three elements:

One condition

Every rule has a boolean condition. If the condition is satisfied (i.e, its value is not false) then the rule is processed.

Some actions

Every rule has some associated actions. The actions could be performed at different rule's stages:

  • Before the rule's condition being evaluated (regardless being satisfied or not)
  • After its condition has been satisfied but before its subrules have been executed
  • After both its condition has been satisfied and its subrules have been executed.
  • Before leave the rule's, regardless the condition has been satisfied or not.

Zero or more subrules

Each rule could contain subrules. Each subrule is executed only if the parent rule's condition has been satisfied.

When executes, the flow a rule follows could be described as:

Perform pre-rule actions
If the rule's condition is satisfied then
    Perform pre-subrules actions
    Execute subrules
    Perform post-subrules actions
Perform post-rule actions

Quick example

$ctx = new Context();
$ctx['name'] = 'Philip J.';
$ctx['surname'] = 'Fry';

// If the context's name is 'Philip J.' AND the context's surname is 'Fry'...
$condition = new AndOp(
    new EqualTo('{{ name }}', 'Philip J.'),
    new EqualTo('{{ surname }}', 'Fry')
);

// Then print the message
$action = new RunCallback(function (Context $c) {
    echo 'Hello, my name is ' . $c['name'] . $c['surname'];
});

$rule = new Rule();
$rule
    ->setCondition($condition)
    ->setAction($action)
    ->execute($ctx);
    
// Hello, my name is Philip J. Fry

Conditions

Conditions use boolean expressions to check whether the rule must be processed. By default, a rule has a true as a condition.

Primitives

There are four predefined primitives:

  • String
  • Integer
  • Float
  • Boolean

Operators

Operators take one, two or more values (primitives or other operator's output) and return some other value

Logical operators

AndOp($expr [, $expr]*)

AndOp() operator takes one or more arguments and returns true if all of its arguments are evaluated as true:

$and = new AndOp(true, false, false, true);
echo $and->evaluate(); // <-- false

OrOp($expr [, $expr]*)

OrOp() operator takes one or more arguments and returns true if any of its arguments are evaluated as true:

$or = new OrOp(true, false, false, true);
echo $or->evaluate(); // <-- true

NotOp($expr [, $expr]*)

NotOp() operator takes one or more arguments and returns true if all of its arguments are evaluated as false. This is indeed a Nand operation.

$not = new Not(true, false, false, true);
echo $not->evaluate(); // <-- false

Comparison conditions

EqualTo($expr, $expr [, $expr]*)

EqualTo() operator takes two or more arguments and returns true if all of its arguments are the same:

$equalTo = new EqualTo("foo", "bar", "baz");
echo $equalTo->evaluate(); // <-- false

NotEqualTo($expr, $expr [, $expr]*)

EqualTo() operator takes two or more arguments and returns true if any of its arguments is different from aonther one:

$notEqualTo = new EqualTo("foo", "bar", "baz");
echo $notEqualTo->evaluate(); // <-- true

Actions

Override context

This action updates/extends the current context:

$ctx = new Context();
$ctx['name'] = 'Philip J.';

$overrideCtx['surname'] = 'Fry';
$action = new OverrideContext($overrideCtx);
$action->perform($ctx);

var_dump($ctx);
// array(
//     'name' => 'Philip J.',
//     'surname' => 'Fry'
// );

Interpolate context

This action allows to render the current context as a template by feeding itself as the variable replacements:

$ctx = new Context();
$ctx['name'] = 'Philip J.';
$ctx['surname'] = 'Fry';
$ctx['fullname'] = '{{ name }} {{ surname }}';

$action = new InterpolateContext();
$action->perform($ctx);

var_dump($ctx);
// array(
//     'name' => 'Philip J.',
//     'surname' => 'Fry'
//     'fullname' => 'Philip J. Fry'
// );

Filter context

This action filter (either by preserving or discarding) entries in a context:

$ctx = new Context();
$ctx['name'] = 'Philip J.';
$ctx['surname'] = 'Fry';
$ctx['fullname'] = 'Philip J. Fry';

$action = new FilterContext();
$action
    ->setKeys('fullname')
    ->setMode(FilterContext::ALLOW_KEYS)
    ->perform($ctx);

var_dump($ctx);
// array(
//     'fullname' => 'Philip J. Fry'
// );

Run callback

This actions calls a custom user function passing the current context as argument:

$ctx = new Context();
$ctx['name'] = 'Philip J.';
$ctx['surname'] = 'Fry';

$callback = function(Context $ctx) {
    $ctx['fullname'] = $ctx['name'] . ' ' . $ctx['surname'];
    unset($ctx['surname'];
}

$action = new RunCallback($callback);
$action->perform($ctx);

var_dump($ctx);
// array(
//     'name' => 'Philip J.',
//     'fullname' => 'Philip J. Fry'
// );

No action

This actions does nothing :)

$ctx = new Context();
$ctx['name'] = 'Philip J.';
$ctx['surname'] = 'Fry';

$action = new NoAction($callback);
$action->perform($ctx);

var_dump($ctx);
// array(
//     'name' => 'Philip J.',
//     'surname' => 'Fry'
// );

Sequence

This actions allows to define a list of actions to be performed sequentially:

$ctx = new Context();
$ctx['fullname'] = '{{ name }} {{ surname }}';

$override = new OverrideContext(new Context(array('name' => 'Philip J.')));     // Adds name
$callback = new RunCallback(function (Context $c) { $c['surname'] = 'Fry'; } ); // Adds surname
$interpolate = new InterpolateContext();                                        // Interpolates fullname
$filter = new FilterContext(FilterContext::ALLOW_KEYS, 'fullname');             // Filters all except fullname

$sequence = new Sequence($override, $callback);
$sequence->appendAction($interpolate)->appendAction($filter);
$sequence->perform($ctx);

var_dump($ctx);
// array(
//     'fullname' => 'Philip J. Fry'
// );

superruzafa/rules 适用场景与选型建议

superruzafa/rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-11-17