kphoen/rulerz
Composer 安装命令:
composer require kphoen/rulerz
包简介
Powerful implementation of the Specification pattern
关键字:
README 文档
README
⚠️ This project is no longer maintained, reach out to me if you are interested in becoming maintainer ⚠️
The central idea of Specification is to separate the statement of how to match a candidate, from the candidate object that it is matched against.
Specifications, explained by Eric Evans and Martin Fowler
RulerZ is a PHP implementation of the Specification pattern which puts the emphasis on three main aspects:
- an easy and data-agnostic DSL to define business rules and specifications,
- the ability to check if a candidate satisfies a specification,
- the ability to filter or query any datasource to only retrieve candidates matching a specification.
Introduction
Business rules can be written as text using a dedicated language, very close to SQL, in which case we refer to them as rules or they can be encapsulated in single classes and referred to as specifications.
Once a rule (or a specification) is written, it can be used to check if a single candidate satisfies it or directly to query a datasource.
The following datasources are supported natively:
- array of arrays,
- array of objects.
And support for each one of these is provided by an additional library:
- Doctrine DBAL QueryBuilders: rulerz-php/doctrine-dbal,
- Doctrine ORM QueryBuilders: rulerz-php/doctrine-orm,
- Pomm models: rulerz-php/pomm,
- Elasticsearch (using the official client: rulerz-php/elasticsearch,
- Solr (using the solarium: rulerz-php/solarium,
- Laravel's Eloquent ORM: rulerz-php/eloquent.
Killer feature: when working with Doctrine, Pomm, or Elasticsearch, RulerZ is able to convert rules directly in queries and does not need to fetch data beforehand.
That's cool, but why do I need that?
First of all, you get to express business rules in a dedicated, simple language. Then, these business rules can be encapsulated in specification classes, reused and composed to form more complex rules. Specifications are now reusable and testable. And last but not least, these rules can be used both to check if a candidate satisfies it and to filter any datasource.
If you still need to be conviced, you can read the whole reasoning in this article.
Quick usage
As a quick overview, we propose to see a little example that manipulates a simple rule and several datasources.
1. Write a rule
The rule hereafter describes a "high ranked female player" (basically, a female player having more than 9000 points).
$highRankFemalesRule = 'gender = "F" and points > 9000';
2. Define a datasource
We have the following datasources:
// a Doctrine QueryBuilder $playersQb = $entityManager ->createQueryBuilder() ->select('p') ->from('Entity\Player', 'p'); // or an array of arrays $playersArr = [ ['pseudo' => 'Joe', 'gender' => 'M', 'points' => 2500], ['pseudo' => 'Moe', 'gender' => 'M', 'points' => 1230], ['pseudo' => 'Alice', 'gender' => 'F', 'points' => 9001], ]; // or an array of objects $playersObj = [ new Player('Joe', 'M', 40, 2500), new Player('Moe', 'M', 55, 1230), new Player('Alice', 'F', 27, 9001), ];
3. Use a rule to query a datasource
For any of our datasource, retrieving the results is as simple as calling the
filter method:
// converts the rule in DQL and makes a single query to the DB $highRankFemales = $rulerz->filter($playersQb, $highRankFemalesRule); // filters the array of arrays $highRankFemales = $rulerz->filter($playersArr, $highRankFemalesRule); // filters the array of objects $highRankFemales = $rulerz->filter($playersObj, $highRankFemalesRule);
3. (bis) Check if a candidate satisfies a rule
Given a candidate, checking if it satisfies a rule boils down to calling the
satisfies method:
$isHighRankFemale = $rulerz->satisfies($playersObj[0], $highRankFemalesRule);
Going further
Check out the documentation to discover what RulerZ can do for you.
License
This library is under the MIT license.
kphoen/rulerz 适用场景与选型建议
kphoen/rulerz 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.3M 次下载、GitHub Stars 达 877, 最近一次更新时间为 2015 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「doctrine」 「specification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kphoen/rulerz 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kphoen/rulerz 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kphoen/rulerz 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
Specification support for Doctrine
Specifications like JSON schemas and other things for mosparo
Make pimcore migration simple
BDD extension for PHPUnit that allows to write tests as a specifications in a human-readable format.
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
统计信息
- 总下载量: 1.3M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 879
- 点击次数: 22
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-01-31