chronext/rql2dbal

PHP wrapper for xiag/rql-parser. Convert RQL and FIQL to Doctrine DBAL

3.0.1 2022-01-17 09:56 UTC

This package is auto-updated.

Last update: 2024-10-17 16:14:47 UTC


README

This is a wrapper around graviton/rql-parser that combines that parser with a small layer of Doctrine DBAL integration.

Installation

Install it using composer.

composer require chronext/rql2dbal

Usage

<?php declare(strict_types=1);

require 'vendor/autoload.php';

use Chronext\Rql2Dbal\Rql2Builder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;

class Finder
{
    /**
     * @var Connection
     */
    private $connection;
    /**
     * @var Rql2Builder
     */
    private $rql2Builder;

    public function __construct(Connection $connection, Rql2Builder $rql2Builder)
    {
        $this->connection  = $connection;
        $this->rql2Builder = $rql2Builder;
    }

    /**
     * @return \Generator
     */
    public function query(string $query): iterable
    {
        $qb = $this->connection->createQueryBuilder();
        $qb->from('table_name');

        ($this->rql2Builder)(
            $qb,
            $query,
            [
                'uuid',
                'field1',
                'field2',
            ]
        );

        $stmt = $qb->execute();
        if (!($stmt instanceof Statement)) {
            throw new \LogicException('Query builder has to return only Statement instance in this case.');
        }

        while ($row = $stmt->fetch()) {
            yield $row;
        }
    }
}

$query = 'or(eq(field1,foo)&eq(field2,bar))';
$orders = (new Finder(new Connection($params,$driver), new Rql2Builder()))->query($query);