PHP code example of fivelab / ruler

1. Go to this page and download the library: Download fivelab/ruler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fivelab / ruler example snippets


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

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

$ruler = new Ruler(new DoctrineOrmTarget());

$qb = $entityManager->createQueryBuilder()
    ->select('products')
    ->from(Product::class, 'products');

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

// The query builder now has the WHERE condition, the parameters and the joins applied.
$products = $qb->getQuery()->getResult();

use Elastica\Query;
use FiveLab\Component\Ruler\Ruler;
use FiveLab\Component\Ruler\Target\ElasticaTarget;

$ruler = new Ruler(new ElasticaTarget());

$query = new Query();
$query->setSize(20);

$ruler->apply($query, 'price > :price and tag = :tag', [
    'price' => 100,
    'tag'   => 'sale',
]);

$results = $index->search($query);

use FiveLab\Component\Ruler\Query\RawSearchQuery;

$query = new RawSearchQuery();
$ruler->apply($query, 'price > :price', ['price' => 100]);

$response = $client->search([
    'index' => 'products',
    'body'  => $query->toArray(),
]);

use FiveLab\Component\Ruler\Query\ClickHouseQuery;
use FiveLab\Component\Ruler\Ruler;
use FiveLab\Component\Ruler\Target\ClickHouseTarget;

$ruler = new Ruler(new ClickHouseTarget());

$query = new ClickHouseQuery();
$ruler->apply($query, 'shop = :shop and amount > :amount', [
    'shop'   => 'foo',
    'amount' => 100,
]);

$where      = $query->getWhere();       // ((shop = :shop) AND (amount > :amount))
$parameters = $query->getParameters();  // ['shop' => 'foo', 'amount' => 100]

use FiveLab\Component\Ruler\Target\Targets;

$ruler = new Ruler(new Targets(
    new DoctrineOrmTarget(),
    new ElasticaTarget(),
    new ClickHouseTarget()
));

$ruler->apply($doctrineQb, $rule, $params);
$ruler->apply($elasticaQuery, $rule, $params);

use FiveLab\Component\Ruler\Specification\AndX;
use FiveLab\Component\Ruler\Specification\SimpleSpecification;

$specification = new AndX(
    new SimpleSpecification('shop = :shop', ['shop' => 'foo']),
    new SimpleSpecification('amount > :amount', ['amount' => 100])
);

$ruler->applySpec($query, $specification);