-
Notifications
You must be signed in to change notification settings - Fork 70
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
Feature/filters #100
Open
srottem
wants to merge
15
commits into
nilportugues:master
Choose a base branch
from
srottem:feature/filters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/filters #100
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8071e11
Implement filters.
srottem 32abfdb
Tidy code to meet PSR-2 recommendation.
srottem c063b1b
Filters were not being URL encoded in response resulting in potential…
srottem 4b2d915
Modified node visitor to accept Query\QueryBuilder instead of Eloquen…
srottem 4487509
Refactored node visitor and got RQL not clause working.
srottem f27fccd
Added documentation on using RQL filters.
srottem fcdb1a8
Documentation corrections and improvements.
srottem 2b66f13
Fixed filter test so it passes. The current output is ugly due to UR…
srottem 99fa354
Controller now uses raw filter query string.
srottem 56c5af3
Update composer to use dev version.
srottem f2fd1d4
Reverted test to use unencoded filter characters.
srottem e3d5571
Updated hacked test to use getRawFilter.
srottem dfc62de
Fixed modified test files for nitpick compliance.
srottem 0756494
Documentation update.
srottem 234cb2b
Updated docs.
srottem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
composer.lock | ||
build/ | ||
.coveralls.yml | ||
.settings | ||
.project | ||
.buildpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
src/NilPortugues/Laravel5/JsonApi/Eloquent/EloquentNodeVisitor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,9 @@ public function getRelationships() | |
{ | ||
return []; | ||
} | ||
|
||
public function getRequiredProperties() | ||
{ | ||
return []; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.