2017 © Pedro Peláez
 

library rulerz

Powerful implementation of the Specification pattern

image

kphoen/rulerz

Powerful implementation of the Specification pattern

  • Tuesday, May 8, 2018
  • by K-Phoen
  • Repository
  • 39 Watchers
  • 629 Stars
  • 115,736 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 55 Forks
  • 19 Open issues
  • 51 Versions
  • 9 % Grown

The README.md

RulerZ Build Status Scrutinizer Code Quality

:warning: This project is no longer maintained, reach out to me if you are interested in becoming maintainer :warning:, (*1)

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., (*2)

Specifications, explained by Eric Evans and Martin Fowler, (*3)

RulerZ is a PHP implementation of the Specification pattern which puts the emphasis on three main aspects:, (*4)

  • 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., (*5)

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., (*6)

The following datasources are supported natively:, (*7)

  • array of arrays,
  • array of objects.

And support for each one of these is provided by an additional library:, (*8)

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., (*9)

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., (*10)

If you still need to be conviced, you can read the whole reasoning in this article., (*11)

Quick usage

As a quick overview, we propose to see a little example that manipulates a simple rule and several datasources., (*12)

1. Write a rule

The rule hereafter describes a "high ranked female player" (basically, a female player having more than 9000 points)., (*13)

$highRankFemalesRule = 'gender = "F" and points > 9000';

2. Define a datasource

We have the following datasources:, (*14)

// 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:, (*15)

// 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:, (*16)

$isHighRankFemale = $rulerz->satisfies($playersObj[0], $highRankFemalesRule);

Going further

Check out the documentation to discover what RulerZ can do for you., (*17)

License

This library is under the MIT license., (*18)

The Versions

21/03 2015

0.5.3

0.5.3.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

11/03 2015

0.5.2

0.5.2.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

10/03 2015

0.5.1

0.5.1.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

09/03 2015

0.5.0

0.5.0.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

09/03 2015

0.4.4

0.4.4.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

24/02 2015

0.4.3

0.4.3.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

23/02 2015

0.4.2

0.4.2.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

23/02 2015

0.4.1

0.4.1.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

23/02 2015

0.4.0

0.4.0.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

18/02 2015

0.3.0

0.3.0.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

10/02 2015

0.2.0

0.2.0.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification

07/02 2015

0.1.0

0.1.0.0 https://github.com/K-Phoen/RulerZ

Powerful implementation of the Specification pattern

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine specification