Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 24, 2021
2 parents 37a649a + 20b93ca commit 9268a47
Show file tree
Hide file tree
Showing 52 changed files with 1,385 additions and 340 deletions.
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ return PhpCsFixer\Config::create()
'no_extra_consecutive_blank_lines' => false,
'no_unneeded_control_parentheses' => false,
'not_operator_with_successor_space' => true,
'ordered_imports' => ['sortAlgorithm' => 'length'],
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'phpdoc_align' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_order' => true,
Expand Down
80 changes: 42 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Database/Eloquent Query Builder filters for Laravel
==============

[![tests](https://github.com/laravie/query-filter/workflows/tests/badge.svg?branch=2.x)](https://github.com/laravie/query-filter/actions?query=workflow%3Atests+branch%3A2.x)
[![tests](https://github.com/laravie/query-filter/workflows/tests/badge.svg?branch=3.x)](https://github.com/laravie/query-filter/actions?query=workflow%3Atests+branch%3A3.x)
[![Latest Stable Version](https://poser.pugx.org/laravie/query-filter/v/stable)](https://packagist.org/packages/laravie/query-filter)
[![Total Downloads](https://poser.pugx.org/laravie/query-filter/downloads)](https://packagist.org/packages/laravie/query-filter)
[![Latest Unstable Version](https://poser.pugx.org/laravie/query-filter/v/unstable)](https://packagist.org/packages/laravie/query-filter)
[![License](https://poser.pugx.org/laravie/query-filter/license)](https://packagist.org/packages/laravie/query-filter)
[![Coverage Status](https://coveralls.io/repos/github/laravie/query-filter/badge.svg?branch=2.x)](https://coveralls.io/github/laravie/query-filter?branch=2.x)
[![Coverage Status](https://coveralls.io/repos/github/laravie/query-filter/badge.svg?branch=3.x)](https://coveralls.io/github/laravie/query-filter?branch=3.x)

* [Installation](#installation)
- [Quick Installation](#quick-installation)
Expand Down Expand Up @@ -42,7 +42,7 @@ The class provides a simple interface to handle `ORDER BY` queries to Laravel El
use App\User;
use Laravie\QueryFilter\Orderable;

$query = App\User::query();
$query = User::query();

$orderable = new Orderable(
'name', 'desc'
Expand All @@ -69,7 +69,7 @@ The class provides a simple interface to `LIKE` queries to Laravel Eloquent/Quer
use App\User;
use Laravie\QueryFilter\Searchable;

$query = App\User::query();
$query = User::query();

$searchable = new Searchable(
'crynobone', ['name', 'email']
Expand Down Expand Up @@ -103,7 +103,7 @@ Set specific `%` or `*` wildcard to reduce the possible `LIKE`s variations.
use App\User;
use Laravie\QueryFilter\Searchable;

$query = App\User::query();
$query = User::query();

$searchable = new Searchable(
'crynobone*gmail', ['name', 'email']
Expand Down Expand Up @@ -131,7 +131,7 @@ Use `withoutWildcardSearching()` to disable adding additional search condition.
use App\User;
use Laravie\QueryFilter\Searchable;

$query = App\User::query();
$query = User::query();

$searchable = (new Searchable(
'crynobone@gmail', ['name', 'email']
Expand All @@ -153,13 +153,13 @@ where (

#### Search with JSON path

This would allow you to query JSON path using `LIKE` with case insensitive (JSON path in MySQL is case-sensitive by default).
This would allow you to query JSON path using `LIKE` with case insensitive.

```php
use App\User;
use Laravie\QueryFilter\Searchable;

$query = App\User::query();
$query = User::query();

$searchable = new Searchable(
'Malaysia', ['address->country']
Expand All @@ -172,10 +172,10 @@ return $searchable->apply($query)->get();
select * from `users`
where (
(
lower(`address`->'$.country') like 'malaysia'
or lower(`address`->'$.country') like 'malaysia%'
or lower(`address`->'$.country') like '%malaysia'
or lower(`address`->'$.country') like '%malaysia%'
lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia'
or lower(json_unquote(json_extract(`meta`, '$."country"'))) like 'malaysia%'
or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia'
or lower(json_unquote(json_extract(`meta`, '$."country"'))) like '%malaysia%'
)
);
```
Expand All @@ -188,7 +188,7 @@ This would make it easy to search results not only in the current model but also
use App\User;
use Laravie\QueryFilter\Searchable;

$query = App\User::query();
$query = User::query();

$searchable = new Searchable(
'Administrator', ['name', 'roles.name']
Expand Down Expand Up @@ -222,6 +222,25 @@ where (

> Relations search can only be applied to `Illuminate\Database\Eloquent\Builder` as it need to ensure that the relationship exists via `whereHas()` queries.
#### Search with Morph Relations

You can use polymorphic relationship search using the following options:

```php
use App\Comment;
use Laravie\QueryFilter\Searchable;
use Laravie\QueryFilter\Filters\MorphRelationSearch;

$query = Comment::query();

$searchable = new Searchable(
'Administrator', ['name', new MorphRelationSearch('commentable', 'name')]
);

return $searchable->apply($query)->get();
```


### Taxonomy Queries

```php
Expand All @@ -234,7 +253,7 @@ Taxonomy always developers to create a set of rules to group the search keywords
use App\User;
use Laravie\QueryFilter\Taxonomy;

$query = App\User::query();
$query = User::query();

$taxonomy = new Taxonomy(
'is:admin email:crynobone@gmail.com', [
Expand Down Expand Up @@ -310,7 +329,7 @@ namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource as NovaResource;
use Laravie\QueryFilter\Taxonomy;
use Laravie\QueryFilter\Searchable;

abstract class Resource extends NovaResource
{
Expand All @@ -326,36 +345,21 @@ abstract class Resource extends NovaResource
*/
protected static function applySearch($query, $search)
{
return $query->where(function ($query) use ($search) {
static::applyResourceSearch($query, $search);
});
}

/**
* Apply the search query to the query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $search
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected static function applyResourceSearch($query, $search)
{
(new Taxonomy(
$search, static::taxonomiesRules(), static::searchableColumns()
))->apply($query);
$searchColumns = static::searchableColumns() ?? [];

return $query;
return static::initializeSearch($search, $searchColumns)->apply($query);
}

/**
* Taxonomies Rules.
* Initialize Search.
*
* @return array
* @param string $search
* @param array $searchColumns
* @return \Laravie\QueryFilter\Searchable
*/
public static function taxonomiesRules()
protected static function initializeSearch($search, $searchColumns)
{
return [];
return new Searchable($search, $searchColumns);
}
}
```
21 changes: 21 additions & 0 deletions canvas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
preset: package

namespace: Laravie\QueryFilter

factory:
path: tests/Factories

console:
namespace: Laravie\QueryFilter\Console

model:
namespace: Laravie\QueryFilter

provider:
namespace: Laravie\QueryFilter

testing:
namespace: Laravie\QueryFilter\Tests
extends:
unit: PHPUnit\Framework\TestCase
feature: Laravie\QueryFilter\Tests\TestCase
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"autoload": {
"psr-4": {
"Laravie\\QueryFilter\\" : "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -25,8 +28,8 @@
"illuminate/support": "^8.0 || ^9.0"
},
"require-dev": {
"mockery/mockery": "^1.3.3 || ^1.4.2",
"orchestra/testbench": "^6.13 || ^7.0"
"orchestra/canvas": "^6.1 || ^7.0",
"orchestra/testbench": "^6.15 || ^7.0"
},
"config": {
"sort-packages": true
Expand Down
35 changes: 21 additions & 14 deletions src/Value/Column.php → src/Column.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Laravie\QueryFilter\Value;
namespace Laravie\QueryFilter;

use Illuminate\Database\Query\Expression;

Expand Down Expand Up @@ -29,7 +29,7 @@ class Column
protected $name;

/**
* Construct a new Field value object.
* Construct a new Column value object.
*
* @param \Illuminate\Database\Query\Expression|string $name
*/
Expand All @@ -42,17 +42,32 @@ public function __construct($name)
* Make a new Field value object.
*
* @param static|\Illuminate\Database\Query\Expression|string $name
*
* @return static
*/
public static function make($name)
{
if ($name instanceof static) {
return $name;
}

return new static($name);
}

/**
* Get original value.
*
* @return \Illuminate\Database\Query\Expression|string
*/
public function getOriginalValue()
{
return $this->name;
}

/**
* Get expression value.
*/
public function getValue(): string
{
return $this->isExpression() ? $this->name->getValue() : $this->name;
}

/**
* Is an instance of Illuminate\Database\Query\Expression.
*/
Expand Down Expand Up @@ -89,14 +104,6 @@ public function accepted(array $only = [], array $except = []): bool
return true;
}

/**
* Get expression value.
*/
public function getValue(): string
{
return $this->isExpression() ? $this->name->getValue() : $this->name;
}

/**
* Validate column name.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Concerns/ConditionallySearchingWildcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ trait ConditionallySearchingWildcard
*/
public $wildcardSearching = null;

/**
* Set wildcard searching status.
*
* @return $this
*/
public function wildcardSearching(?bool $wildcardSearching)
{
$this->wildcardSearching = $wildcardSearching;

return $this;
}

/**
* Enable using wildcard search.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/SearchingWildcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait SearchingWildcard
*
* @var string|null
*/
public $wildcardCharacter = '*';
public $wildcardCharacter = null;

/**
* Wildcard search character replacement.
Expand Down
21 changes: 21 additions & 0 deletions src/Contracts/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Laravie\QueryFilter\Contracts;

interface Field
{
/**
* Validate column.
*/
public function validate(): bool;

/**
* Is relation selector.
*/
public function isRelationSelector(): bool;

/**
* Is JSON path selector.
*/
public function isJsonPathSelector(): bool;
}
26 changes: 26 additions & 0 deletions src/Contracts/Keyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Laravie\QueryFilter\Contracts;

interface Keyword
{
/**
* Validate keyword value.
*/
public function validate(): bool;

/**
* Get keyword value.
*/
public function getValue(): string;

/**
* Get searchable strings.
*/
public function all(): array;

/**
* Handle resolving keyword for filter.
*/
public function handle(SearchFilter $filter): array;
}
8 changes: 8 additions & 0 deletions src/Contracts/Keyword/AsExactValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravie\QueryFilter\Contracts\Keyword;

interface AsExactValue
{
//
}
8 changes: 8 additions & 0 deletions src/Contracts/Keyword/AsLowerCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravie\QueryFilter\Contracts\Keyword;

interface AsLowerCase
{
//
}
Loading

0 comments on commit 9268a47

Please sign in to comment.