Skip to content

support for wildcards inside -lk params #57

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function parse($options, $multiple = false)
} else {
$primaryKey = $this->getQualifiedColumnName('id');
}

$this->query->where($primaryKey, $identification);
}
}
Expand Down Expand Up @@ -377,7 +377,10 @@ protected function parseWith($withParam)
$relationType = $this->getRelationType($relation);

if ($relationType === 'BelongsTo') {
$firstKey = $relation->getQualifiedForeignKey();
// Compatibility for Laravel < 5.8
$firstKey = (method_exists($relation, 'getQualifiedForeignKeyName'))
? $relation->getQualifiedForeignKeyName()
: $relation->getQualifiedForeignKey();
$secondKey = $relation->getQualifiedParentKeyName();
} else if ($relationType === 'HasMany' || $relationType === 'HasOne') {
$firstKey = $relation->getQualifiedParentKeyName();
Expand Down Expand Up @@ -553,7 +556,7 @@ protected function parseFilter($filterParams)
$this->query->where(function ($query) use ($column, $comparator, $values) {
foreach ($values as $value) {
if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
$value = preg_replace('/(^\*|\*$)/', '%', $value);
$value = str_replace('*','%',$value);
}

//Link the filters with AND of there is a "not" and with OR if there's none
Expand All @@ -568,7 +571,7 @@ protected function parseFilter($filterParams)
$value = $values[0];

if ($comparator == 'LIKE' || $comparator == 'NOT LIKE') {
$value = preg_replace('/(^\*|\*$)/', '%', $value);
$value = str_replace('*','%',$value);
}

if ($comparator == 'NULL' || $comparator == 'NOT NULL') {
Expand Down Expand Up @@ -736,7 +739,7 @@ protected function isRelation($model, $relationName)
*/
protected function getQualifiedColumnName($column, $table = null)
{
//Check whether there is a matching column expression that contains an
//Check whether there is a matching column expression that contains an
//alias and should therefore not be turned into a qualified column name.
$isAlias = count(array_filter($this->query->columns ?: [], function($queryColumn) use ($column) {
return preg_match('/.*[\s\'"`]as\s*[\s\'"`]' . $column . '[\'"`]?$/', trim($queryColumn));
Expand Down
11 changes: 10 additions & 1 deletion tests/ApiHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use Mockery as m;
use \Illuminate\Database\Eloquent\Collection;
use \Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Builder as BaseBuilder;
use \Illuminate\Http\JsonResponse;
use \Illuminate\Support\Facades\Config;
use \Illuminate\Support\Facades\Input;
Expand All @@ -20,6 +21,7 @@ public function setUp()
'_fields' => 'title,description,comments.title,user.first_name',
//Filters
'title-lk' => 'Example Title|Another Title',
'description-lk' => '*aaa*bbb*',
'title' => 'Example Title',
'title-not-lk' => 'Example Title',
'title-not' => 'Example Title|Another Title',
Expand Down Expand Up @@ -88,7 +90,7 @@ public function setUp()
->with('Something to search')->andReturn('Something to search');

//Mock the connection the same way as laravel does:
//tests/Database/DatabaseEloquentBuilderTest.php#L408-L418 (mockConnectionForModel($model, $database))
//tests/Database/DatabaseEloquentBuilderTest.php#L1187-L1198 (mockConnectionForModel($model, $database))
$grammar = new Illuminate\Database\Query\Grammars\MySqlGrammar;
$processor = new Illuminate\Database\Query\Processors\MySqlProcessor;
$connection = m::mock('Illuminate\Database\ConnectionInterface', ['getQueryGrammar' => $grammar, 'getPostProcessor' => $processor]);
Expand All @@ -97,6 +99,10 @@ public function setUp()
$connection->shouldReceive('raw')->once()->with('MATCH(posts.title,posts.description) AGAINST("Something to search" IN BOOLEAN MODE) as `_score`')
->andReturn($this->fulltextSelectExpression);
$connection->shouldReceive('getPdo')->once()->andReturn($pdo);
$connection->shouldReceive('query')->andReturnUsing(function () use ($connection, $grammar, $processor) {
return new BaseBuilder($connection, $grammar, $processor);
});
$connection->shouldReceive('getName')->andReturn('myConnection');

$resolver = m::mock('Illuminate\Database\ConnectionResolverInterface', ['connection' => $connection]);

Expand Down Expand Up @@ -168,6 +174,9 @@ public function testGetBuilder()
//assert for title-not-lk
$this->assertContains(['type' => 'Basic', 'column' => 'posts.title', 'operator' => 'NOT LIKE', 'value' => 'Example Title', 'boolean' => 'and'], $wheres);

//assert for description-lk
$this->assertContains(['type' => 'Basic', 'column' => 'posts.description', 'operator' => 'LIKE', 'value' => '%aaa%bbb%', 'boolean' => 'and'], $wheres);

//assert for id-min
$this->assertContains(['type' => 'Basic', 'column' => 'posts.id', 'operator' => '>=', 'value' => 5, 'boolean' => 'and'], $wheres);

Expand Down