GithubHelp home page GithubHelp logo

surongzhen / rulerz Goto Github PK

View Code? Open in Web Editor NEW

This project forked from k-phoen/rulerz

0.0 2.0 0.0 621 KB

Powerful implementation of the Specification pattern in PHP

License: MIT License

Makefile 0.64% PHP 93.40% Shell 0.60% Pascal 1.76% Gherkin 3.61%

rulerz's Introduction

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 currently supported datasources are:

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.

rulerz's People

Contributors

k-phoen avatar bobdercole avatar iainmckay avatar ikwattro avatar claudusd avatar estelsmith avatar grummfy avatar hywan avatar j0k3r avatar jdeniau avatar jdecool avatar jhuet avatar loicbourg avatar mnapoli avatar mikaelrandy avatar nclshart avatar royopa avatar pioneer32 avatar yvoyer avatar syrm avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.