Skip to content

Powerful implementation of the Specification pattern in PHP

License

Notifications You must be signed in to change notification settings

andersonamuller/rulerz

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RulerZ Build Status Scrutinizer Code Quality

This library allows to filter multiple types of targets using the same rule engine.

Rules can be written by using a dedicated language, very close to SQL. Therefore, they can be written by a user and saved in a database.

The rule engine used by RulerZ is hoa/ruler.

Currently supported target types:

  • array of arrays ;
  • array of objects ;
  • Doctrine ORM QueryBuilder.

There also is a blog post explaining the motivation behind this library.

Installation

composer require 'kphoen/rulerz'

Usage

$rulerz = new \RulerZ\RulerZ(
    new \RulerZ\Interpreter\HoaInterpreter(), [
        new \RulerZ\Executor\ArrayExecutor([
            'length' => 'strlen',
        ]),
        new \RulerZ\Executor\DoctrineQueryBuilderExecutor(),
    ]
);

// 1. Write a rule.
$rule  = 'group in :groups and points > :points and length(name) > 2';

// 2. Filter a collection
$usersQb = $entityManager
    ->createQueryBuilder()
    ->select('u')
    ->from('Entity\User', 'u');

// or an array of arrays
$usersArr = [
    ['name' => 'Joe', 'group' => 'guest', 'points' => 40],
    ['name' => 'Moe', 'group' => 'guest', 'points' => 20],
    ['name' => 'Al',  'group' => 'guest', 'points' => 40],
];

// or an array of objects
$usersObj = [
    new User('Joe', 'guest', 40),
    new User('Moe', 'guest', 20),
    new User('Al',  'guest', 40),
];

// 3. Enjoy!
$parameters = array(
    'points' => 30,
    'groups' => ['customer', 'guest'],
);

var_dump($rulerz->filter($usersQb, $rule, $parameters));
var_dump($rulerz->filter($usersArr, $rule, $parameters));
var_dump($rulerz->filter($usersObj, $rule, $parameters));

Documentation

The documentation can be found in the doc directory. Have a look at the index.md if you don't know what you are looking for.

Licence

This library is under the MIT licence.

About

Powerful implementation of the Specification pattern in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%