-
Notifications
You must be signed in to change notification settings - Fork 70
Feature/filters #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feature/filters #100
Changes from 11 commits
8071e11
32abfdb
c063b1b
4b2d915
4487509
f27fccd
fcdb1a8
2b66f13
99fa354
56c5af3
f2fd1d4
e3d5571
dfc62de
0756494
234cb2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
composer.lock | ||
build/ | ||
.coveralls.yml | ||
.settings | ||
.project | ||
.buildpath |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
namespace NilPortugues\Laravel5\JsonApi\Eloquent; | ||
|
||
use Illuminate\Database\Query\Builder; | ||
use Xiag\Rql\Parser\Glob; | ||
use Xiag\Rql\Parser\Node\AbstractQueryNode; | ||
use Xiag\Rql\Parser\Node\Query\AbstractArrayOperatorNode; | ||
use Xiag\Rql\Parser\Node\Query\AbstractLogicalOperatorNode; | ||
use Xiag\Rql\Parser\Node\Query\AbstractScalarOperatorNode; | ||
use Xiag\Rql\Parser\Query; | ||
|
||
/** | ||
* RQL node visitor for constructing Eloquent queries. | ||
* | ||
* @author srottem | ||
*/ | ||
class EloquentNodeVisitor | ||
{ | ||
/** | ||
* Populates the provided builder from the provided RQL query instance. | ||
* | ||
* @param Query $query The RQL query to populate the Eloquent builder from | ||
* @param Builder $builder The Eloquent query builder to populate | ||
*/ | ||
public function visit(Query $query, Builder $builder) | ||
{ | ||
if ($query->getQuery() !== null) { | ||
$this->visitQueryNode($query->getQuery(), $builder); | ||
} | ||
} | ||
|
||
/** | ||
* Processes a query node. | ||
* | ||
* @param AbstractQueryNode $node The node to process | ||
* @param Builder $builder The Eloquent builder to populate | ||
* @param string $boolean The operator to use when appending where clauses | ||
* | ||
* @throws \LogicException Thrown if the node is of an unknown type | ||
*/ | ||
private function visitQueryNode(AbstractQueryNode $node, Builder $builder, $boolean = 'and') | ||
{ | ||
if ($node instanceof AbstractScalarOperatorNode) { | ||
$this->visitScalarNode($node, $builder, $boolean); | ||
} elseif ($node instanceof AbstractArrayOperatorNode) { | ||
$this->visitArrayNode($node, $builder, $boolean); | ||
} elseif ($node instanceof AbstractLogicalOperatorNode) { | ||
$this->visitLogicalNode($node, $builder, $boolean); | ||
} else { | ||
throw new \LogicException(sprintf('Unknown node "%s"', $node->getNodeName())); | ||
} | ||
} | ||
|
||
/** | ||
* Processes a scalar node. | ||
* | ||
* @param AbstractScalarOperatorNode $node The node to process | ||
* @param Builder $builder The Eloquent builder to populate | ||
* @param unknown $boolean The operator to use when appending where clauses | ||
* | ||
* @throws \LogicException Thrown if the node cannot be processed | ||
*/ | ||
private function visitScalarNode(AbstractScalarOperatorNode $node, Builder $builder, $boolean) | ||
{ | ||
static $operators = [ | ||
'like' => 'LIKE', | ||
'eq' => '=', | ||
'ne' => '<>', | ||
'lt' => '<', | ||
'gt' => '>', | ||
'le' => '<=', | ||
'ge' => '>=', | ||
]; | ||
|
||
if (!isset($operators[$node->getNodeName()])) { | ||
throw new \LogicException(sprintf('Unknown scalar node "%s"', $node->getNodeName())); | ||
} | ||
|
||
$value = $node->getValue(); | ||
|
||
if ($value instanceof Glob) { | ||
$value = $value->toLike(); | ||
} elseif ($value instanceof \DateTimeInterface) { | ||
$value = $value->format(DATE_ISO8601); | ||
} | ||
|
||
if ($value === null) { | ||
if ($node->getNodeName() === 'eq') { | ||
$builder->whereNull($node->getField(), $boolean); | ||
} elseif ($node->getNodeName() === 'ne') { | ||
$builder->whereNotNull($node->getField(), $boolean); | ||
} else { | ||
throw new \LogicException(sprintf("Only the 'eq' an 'ne' operators can be used when comparing to 'null()'.")); | ||
} | ||
} else { | ||
$builder->where( | ||
$node->getField(), | ||
$operators[$node->getNodeName()], | ||
$value, | ||
$boolean | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Processes an array node. | ||
* | ||
* @param AbstractArrayOperatorNode $node The node to process | ||
* @param Builder $builder The Eloquent builder to populate | ||
* @param unknown $boolean The operator to use when appending where clauses | ||
* | ||
* @throws \LogicException Thrown if the node cannot be processed | ||
*/ | ||
private function visitArrayNode(AbstractArrayOperatorNode $node, Builder $builder, $boolean) | ||
{ | ||
static $operators = [ | ||
'in', | ||
'out', | ||
]; | ||
|
||
if (!in_array($node->getNodeName(), $operators)) { | ||
throw new \LogicException(sprintf('Unknown array node "%s"', $node->getNodeName())); | ||
} | ||
|
||
$negate = false; | ||
|
||
if ($node->getNodeName() === 'out') { | ||
$negate = true; | ||
} | ||
|
||
$builder->whereIn( | ||
$node->getField(), | ||
$node->getValues(), | ||
$boolean, | ||
$negate | ||
); | ||
} | ||
|
||
/** | ||
* Processes a logical node. | ||
* | ||
* @param AbstractLogicalOperatorNode $node The node to process | ||
* @param Builder $builder The Eloquent builder to populate | ||
* @param unknown $boolean The operator to use when appending where clauses | ||
* | ||
* @throws \LogicException Thrown if the node cannot be processed | ||
*/ | ||
private function visitLogicalNode(AbstractLogicalOperatorNode $node, Builder $builder, $boolean) | ||
{ | ||
if ($node->getNodeName() === 'and' || $node->getNodeName() === 'or') { | ||
$builder->where(\Closure::bind(function ($constraintGroupBuilder) use ($node) { | ||
foreach ($node->getQueries() as $query) { | ||
$this->visitQueryNode($query, $constraintGroupBuilder, $node->getNodeName()); | ||
} | ||
}, $this), null, null, $boolean); | ||
} elseif ($node->getNodeName() === 'not') { | ||
$builder->where(\Closure::bind(function ($constraintGroupBuilder) use ($node, $boolean) { | ||
foreach ($node->getQueries() as $query) { | ||
$this->visitQueryNode($query, $constraintGroupBuilder, $boolean); | ||
} | ||
}, $this), null, null, $boolean.' not'); | ||
} else { | ||
throw new \LogicException(sprintf('Unknown or unsupported logical node "%s"', $node->getNodeName())); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,18 @@ public function getOrdersByEmployee($id) | |
$sorting = $apiRequest->getSort(); | ||
$included = $apiRequest->getIncludedRelationships(); | ||
$filters = $apiRequest->getFilters(); | ||
|
||
//HACK: JsonAPI is specifying this as an array but it should be a string at this point for RQL processing. | ||
switch(count($filters)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
case 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
$filters = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
case 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
$filters = $filters[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
throw new \Exception('Only a single filter is supported at present.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces must be used to indent lines; tabs are not allowed |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace found at end of line |
||
|
||
$resource = new ListResource($this->serializer, $page, $fields, $sorting, $included, $filters); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getRawFilter has been implemented on a modified version of the php-json-api package that I've rolled which returns the entire and unmodified text of the filter parameter in the URL. The method uses the $_SERVER['QUERY_STRING'] value to obtain the value as the ServerRequestInterface object's getQueryParams() method has run the values through urldecode which makes returning original filters provided by the user in URLs generated by the controller near impossible.