RQL (Resource Query Language)
, (*1)
- A Resource Query Language to PHP (with all methods covered by php unit tests), with this library you can perform queries using a unique input interface that will be converted and processed using the ProcessorInterface contribute with this project!
Installation
- The package is available on Packagist.
- The source files is PSR-2 compatible.
- Autoloading is PSR-4 compatible.
- Input parser data is PSR-7 compatible.
composer require cekurte/rql
If you liked of this library, give me a star =)., (*2)
Documentation
This library was created to perform queries using a unique interface that will be converted and processed to the correct target. The target data can be an array, a database, a file (such as an ini, a xml or a json), a webservice, and more. Currently this project works only with the Doctrine ORM, then, you can perform queries in all databases that are compatible with the doctrine ORM project., (*3)
We would be happy with their contribution, submit your pull request for new Processors like the Eloquent ORM, File Parser (INI, XML, JSON), API's (Facebook, LinkedIn,...) and more., (*4)
Expressions
Well, now we show how to use this library, to perform the query using the equality expression (more expressions will be show too)., (*5)
<?php
use Cekurte\Resource\Query\Language\Expr\EqExpr;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Processor\DoctrineOrmProcessor;
// ...
$qb = $yourDoctrineEntityRepository->createQueryBuilder('alias');
$queue = new ExprQueue();
$queue->enqueue(new EqExpr('alias.field', 'value'));
// You can enqueue all expressions that implemented
// the Cekurte\Resource\Query\Language\Contract\ExprInterface
(new DoctrineOrmProcessor($qb))->process($queue);
// And now you can use the Doctrine QueryBuilder normally
$results = $qb->getQuery()->getResult();
// ...
Ok, but why do not use the Doctrine QueryBuilder to build the queries and perform it? Because of the flexibility to search in the multiple data sources using the same interface changing the Processor class, that in this case is the DoctrineOrmProcessor., (*6)
Woow, you can see this scenario with various possibilities like me? Then, this library is for you guy. You share your aim with me and contribute with this project., (*7)
But, if i want build a collection of expression, i need to put the expressions using the queue? Yes, it is an valid answer for this question, but you can use the ExprBuilder to perform it too. Like the following example:, (*8)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\Processor\DoctrineOrmProcessor;
// ...
$qb = $yourDoctrineEntityRepository->createQueryBuilder('alias');
$builder = new ExprBuilder();
$builder->eq('field', 'value');
(new DoctrineOrmProcessor($qb))->process($builder);
// And now you can use the Doctrine QueryBuilder normally
$results = $qb->getQuery()->getResult();
// ...
Note that the ExprBuilder class implements a shortcut to all available expressions., (*9)
Currently are available the following expressions:, (*10)
Above was listed the available query expressions, next you can see the use of expressions using the ExprBuilder and the ExprQueue., (*11)
BetweenExpr
The BetweenExpr can be used to query a value that is in an interval of values., (*12)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\BetweenExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->between('field', 1, 10);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new BetweenExpr('field', 1, 10));
EqExpr
The EqExpr can be used to query a value using the equality operator., (*13)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\EqExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->eq('field', 'value');
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new EqExpr('field', 'value'));
GteExpr
The GteExpr can be used to query a value using that must be greater than or equal to one., (*14)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\GteExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->gte('field', 1);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new GteExpr('field', 1));
GtExpr
The GtExpr can be used to query a value using that must be greater than one., (*15)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\GtExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->gt('field', 1);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new GtExpr('field', 1));
InExpr
The InExpr can be used to query a value that can be equal to one, two or three., (*16)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\InExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->in('field', [1, 2, 3]);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new InExpr('field', [1, 2, 3]));
LikeExpr
The LikeExpr can be used to query a value that must be equal (in the end only) "%value" where the % operator is a joker like the SQL commands., (*17)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\LikeExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->like('field', '%value');
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new LikeExpr('field', '%value'));
LteExpr
The LteExpr can be used to query a value using that must be less than or equal to one., (*18)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\LteExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->lte('field', 1);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new LteExpr('field', 1));
LtExpr
The LtExpr can be used to query a value using that must be less than one., (*19)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\LtExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->lt('field', 1);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new LtExpr('field', 1));
NeqExpr
The NeqExpr can be used to query a value using the not equal operator., (*20)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\NeqExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->neq('field', 'value');
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new NeqExpr('field', 'value'));
NotInExpr
The NotInExpr can be used to query a value that must be different of one, two or three., (*21)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\NotInExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->notin('field', [1, 2, 3]);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new NotInExpr('field', [1, 2, 3]));
NotLikeExpr
The NotLikeExpr can be used to query a value that must be different (in the end only) "%value" where the % operator is a joker like the SQL commands., (*22)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\NotLikeExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->notlike('field', '%value');
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new NotLikeExpr('field', '%value'));
OrExpr
The OrExpr can be used to query a value joining the above comparison expressions to perform the query. In the following example all fields that are filled with the value one or two will be returned., (*23)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\OrExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->orx(['field:eq:1', 'field:eq:2']);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new OrExpr(['field:eq:1', 'field:eq:2']));
PaginateExpr
The PaginateExpr can be used to paginate the results, the first parameter is the current page number and the second parameter is the limit of results per page., (*24)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\PaginateExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->paginate(1, 10);
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new PaginateExpr(1, 10));
SortExpr
The SortExpr can be used to sort the results, the first parameter is the field that will be sorted and the second parameter is the direction (can be used the asc and desc like in the SQL commands)., (*25)
<?php
use Cekurte\Resource\Query\Language\ExprBuilder;
use Cekurte\Resource\Query\Language\ExprQueue;
use Cekurte\Resource\Query\Language\Expr\SortExpr;
// Using the ExprBuilder
$expr = new ExprBuilder();
$expr->sort('field', 'asc');
// OR using the ExprQueue...
$expr = new ExprQueue();
$expr->enqueue(new SortExpr('field', 'asc'));
Parser
The parser can be used to parse an input data in different formats to the ExprBuilder. Currently are available three parser classes that implements the ParserInterface., (*26)
ArrayParser
The ArrayParser can be used to parse the array data to ExprBuilder, see the example in the below:, (*27)
<?php
use Cekurte\Resource\Query\Language\Parser\ArrayParser;
$data = [
[
'field' => 'field',
'expression' => 'between',
'value' => '1-3',
],
[
'field' => 'field',
'expression' => 'eq',
'value' => 'value',
],
[
'field' => 'field',
'expression' => 'gte',
'value' => '1',
],
[
'field' => 'field',
'expression' => 'gt',
'value' => '1',
],
[
'field' => 'field',
'expression' => 'in',
'value' => [1, 2, 3],
],
[
'field' => 'field',
'expression' => 'like',
'value' => '%value',
],
[
'field' => 'field',
'expression' => 'lte',
'value' => '1',
],
[
'field' => 'field',
'expression' => 'lt',
'value' => '1',
],
[
'field' => 'field',
'expression' => 'neq',
'value' => 'value',
],
[
'field' => 'field',
'expression' => 'notin',
'value' => [1, 2, 3],
],
[
'field' => 'field',
'expression' => 'notlike',
'value' => '%value',
],
[
'field' => '',
'expression' => 'or',
'value' => 'field:eq:1|field:eq:2',
],
[
'field' => '',
'expression' => 'paginate',
'value' => '1-10',
]
[
'field' => 'field',
'expression' => 'sort',
'value' => 'asc',
],
];
$parser = new ArrayParser($data);
/**
* @var $exprBuilder Cekurte\Resource\Query\Language\ExprBuilder
*/
$exprBuilder = $parser->parse();
RequestParser
The RequestParser can be used to parse an input data that is an instance of RequestInterface (compatible with PSR-7), see the example in the below:, (*28)
<?php
use Cekurte\Resource\Query\Language\Parser\RequestParser;
// Suppose that your URI of the request is:
// http://www.yourdomain.com/?q[]=field:eq:1&q[]=field:eq:2
$parser = new RequestParser(
$yourRequestObjectThatImplementsPSR7RequestInterface
);
// If you need customize the query string parameter key that will
// be used, then you can set the parameter key using the following
// method (key "q" is default)
// $parser->setQueryStringParameter('expr');
/**
* @var $exprBuilder Cekurte\Resource\Query\Language\ExprBuilder
*/
$exprBuilder = $parser->parse();
StringParser
The StringParser can be used to parse an input data that is a string, see the example in the below:, (*29)
<?php
use Cekurte\Resource\Query\Language\Parser\StringParser;
$data = ''
. 'field:eq:1&'
. 'field:eq:2&'
. 'field:eq:3'
;
$parser = new StringParser($data);
// If you need customize the expression separator parameter key that will
// be used, then you can set the parameter key using the following
// method (key "&" is default)
// $parser->setSeparator('SEPARATOR');
/**
* @var $exprBuilder Cekurte\Resource\Query\Language\ExprBuilder
*/
$exprBuilder = $parser->parse();
Contributing
- Give me a star =)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
)
- Make your changes
- Run the tests, adding new ones for your own code if necessary (
vendor/bin/phpunit
)
- Commit your changes (
git commit -am 'Added some feature'
)
- Push to the branch (
git push origin my-new-feature
)
- Create new Pull Request