All Projects → K-Phoen → Rulerz

K-Phoen / Rulerz

Licence: mit
Powerful implementation of the Specification pattern in PHP

Projects that are alternatives of or similar to Rulerz

Json Rules Engine
A rules engine expressed in JSON
Stars: ✭ 1,159 (+40.15%)
Mutual labels:  rules, rules-engine
Nrules.language
Business rules language for NRules rules engine.
Stars: ✭ 55 (-93.35%)
Mutual labels:  rules, rules-engine
Roulette
A text/template based rules engine
Stars: ✭ 32 (-96.13%)
Mutual labels:  rules, rules-engine
Node Rules
Node-rules is a light weight forward chaining rule engine written in JavaScript.
Stars: ✭ 481 (-41.84%)
Mutual labels:  rules, rules-engine
rools
A small rule engine for Node.
Stars: ✭ 118 (-85.73%)
Mutual labels:  rules, rules-engine
Rules
Generic Rules engine in golang
Stars: ✭ 96 (-88.39%)
Mutual labels:  rules, rules-engine
Nrules
Rules engine for .NET, based on the Rete matching algorithm, with internal DSL in C#.
Stars: ✭ 1,003 (+21.28%)
Mutual labels:  rules, rules-engine
powerflows-dmn
Power Flows DMN - Powerful decisions and rules engine
Stars: ✭ 46 (-94.44%)
Mutual labels:  rules, rules-engine
Rulette
A pragmatic business rule management system
Stars: ✭ 91 (-89%)
Mutual labels:  rules, rules-engine
Rulebook
100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
Stars: ✭ 562 (-32.04%)
Mutual labels:  rules, rules-engine
Android Hot Libraries
收集总结 Android 项目中值得推荐的优秀开源项目
Stars: ✭ 755 (-8.71%)
Mutual labels:  library
Adafruit sensor
Common sensor library
Stars: ✭ 757 (-8.46%)
Mutual labels:  library
M3u8
Parser and generator of M3U8-playlists for Apple HLS. Library for Go language. 🎦
Stars: ✭ 800 (-3.26%)
Mutual labels:  library
Swipeablecard
A simple implementation of swipe card like StreetView
Stars: ✭ 812 (-1.81%)
Mutual labels:  library
Libssh2
the SSH library
Stars: ✭ 743 (-10.16%)
Mutual labels:  library
Slim.js
Fast & Robust Front-End Micro-framework based on modern standards
Stars: ✭ 789 (-4.59%)
Mutual labels:  library
Slug Generator
Slug Generator Library for PHP, based on Unicode’s CLDR data
Stars: ✭ 740 (-10.52%)
Mutual labels:  library
Navigation Toolbar Android
Navigation toolbar is a slide-modeled UI navigation controller made by @Ramotion
Stars: ✭ 732 (-11.49%)
Mutual labels:  library
Stickyswitch
⭐️ beautiful switch widget with sticky animation ⭐️
Stars: ✭ 725 (-12.33%)
Mutual labels:  library
Devtools
The Hoa\Devtools library.
Stars: ✭ 5 (-99.4%)
Mutual labels:  library

RulerZ Build Status Scrutinizer Code Quality

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:

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].