fivelab/ruler

Add rule for satisfy objects and generate queries based on specifications.

v1.2.0 2023-02-15 18:02 UTC

This package is auto-updated.

Last update: 2024-04-15 20:40:33 UTC


README

 #StandWithUkraine 

Ruler

The library for apply the string rule to any query builders (Doctrine ORM, Elastica, etc...).

For start use ruler, you should create the target factories for all supported targets for you. After, you can create ruler and apply any rules:

<?php

use FiveLab\Component\Ruler\Ruler;
use FiveLab\Component\Ruler\Target\Targets;
use FiveLab\Component\Ruler\Target\DoctrineOrmTarget;
use FiveLab\Component\Ruler\Target\ElasticaTarget;

$targets = new Targets(
    new DoctrineOrmTarget(),
    new ElasticaTarget() 
);

$ruler = new Ruler($targets);

// Apply rules
$qb = $entityManager->createQueryBuilder()
    ->from('Product', 'products')
    ->select('products');

$ruler->apply($qb, 'category.key in (:categories) and enabled = :enabled and price > :price', [
    'categories' => ['cat1', 'cat2'], 
    'enabled' => true,
    'price' => 100
]);

Note: system auto-detect joins based on dot (.) for SQL targets and nested for document targets. If column contain dot (in ES as an example), you can escape dot via \ (money\.amount).

Development

For easy development you can use the Docker.

$ docker build -t ruler .
$ docker run -it -v $(pwd):/code --name ruler ruler bash

After success run and attach to container you must install vendors:

$ composer update

Before create the PR or merge into develop, please run next commands for validate code:

$ ./bin/phpunit

$ ./bin/phpcs --config-set show_warnings 0
$ ./bin/phpcs --standard=vendor/escapestudios/symfony2-coding-standard/Symfony/ src/
$ ./bin/phpcs --standard=tests/phpcs-ruleset.xml tests/