Skip to content

Commit 0881bf7

Browse files
author
oooiik
committed
first logic
1 parent 4455868 commit 0881bf7

File tree

7 files changed

+233
-2
lines changed

7 files changed

+233
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Toshev Obidjon
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "oooiik/laravel-query-filter",
3-
"description": "",
3+
"description": "In Laravel, it is convenient to write the query filter related to the model in a separate class",
4+
"keywords": ["laravel", "query", "filter", "query filter"],
45
"type": "library",
56
"license": "MIT",
67
"authors": [
@@ -9,10 +10,19 @@
910
"email": "[email protected]"
1011
}
1112
],
12-
"require": {},
13+
"require": {
14+
"php": "^7.3|^8.0"
15+
},
1316
"autoload": {
1417
"psr-4": {
1518
"Oooiik\\LaravelQueryFilter\\": "src"
1619
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"Oooiik\\LaravelQueryFilter\\Providers\\LaravelQueryFilterServiceProvider"
25+
]
26+
}
1727
}
1828
}

src/Console/MakeQueryFilter.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Oooiik\LaravelQueryFilter\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Symfony\Component\Console\Exception\InvalidArgumentException;
8+
9+
class MakeQueryFilter extends Command
10+
{
11+
/**
12+
* The filesystem instance.
13+
*
14+
* @var Filesystem
15+
*/
16+
protected $files;
17+
protected $modelName;
18+
19+
protected $signature = 'make:query-filter {model}';
20+
21+
protected $description = 'Create a new query filter class';
22+
23+
public function __construct(Filesystem $files)
24+
{
25+
parent::__construct();
26+
27+
$this->files = $files;
28+
}
29+
30+
public function handle()
31+
{
32+
$model = $this->argument('model');
33+
$this->setModelName($model);
34+
$this->buildClass();
35+
}
36+
37+
38+
protected function buildClass()
39+
{
40+
$stub = $this->replaceStub();
41+
if (is_file($this->getGenerationPath())) {
42+
throw new InvalidArgumentException('Such a class exists!');
43+
}
44+
$this->files->ensureDirectoryExists($this->getGenerationDir());
45+
$this->files->put($this->getGenerationPath(), $stub);
46+
$this->info('QueryFilter created successfully');
47+
}
48+
49+
protected function getGenerationPath()
50+
{
51+
return app_path('Filters/Query/' . $this->getGenerationClassName() . '.php');
52+
}
53+
54+
protected function getGenerationDir()
55+
{
56+
return app_path('Filters/Query');
57+
}
58+
59+
protected function getGenerationClassName()
60+
{
61+
return $this->modelName . 'QueryFilter';
62+
}
63+
64+
protected function hasModel($model)
65+
{
66+
return class_exists($model);
67+
}
68+
69+
protected function setModelName($model)
70+
{
71+
$modelClass = '\\App\\Models\\' . $model;
72+
if (!$this->hasModel($modelClass)) {
73+
throw new InvalidArgumentException('Model not found!');
74+
}
75+
$this->modelName = class_basename($model);
76+
}
77+
78+
protected function replaceStub()
79+
{
80+
$stub = str_replace(
81+
['{{ namespace }}', '{{ class }}'],
82+
[$this->getNamespace(), $this->getClassName()],
83+
$this->getStub()
84+
);
85+
86+
return $stub;
87+
}
88+
89+
protected function getStub()
90+
{
91+
return $this->files->get(__DIR__ . '/stubs/query-filter.stub');
92+
}
93+
94+
protected function getNamespace()
95+
{
96+
return 'App\Filters\Query';
97+
}
98+
99+
protected function getClassName()
100+
{
101+
return $this->modelName . 'QueryFilter';
102+
}
103+
104+
}

src/Console/stubs/query-filter.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Oooiik\LaravelQueryFilter\QueryFilter\QueryFilter;
6+
7+
class {{ class }} extends QueryFilter
8+
{
9+
//
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Oooiik\LaravelQueryFilter\Providers;
4+
5+
use Oooiik\LaravelQueryFilter\Console\MakeQueryFilter;
6+
7+
class LaravelQueryFilterServiceProvider extends \Illuminate\Support\ServiceProvider
8+
{
9+
public function register()
10+
{
11+
//
12+
}
13+
14+
public function boot()
15+
{
16+
if ($this->app->runningInConsole()) {
17+
$this->commands([
18+
MakeQueryFilter::class,
19+
]);
20+
}
21+
}
22+
23+
}

src/QueryFilter/QueryFilter.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Oooiik\LaravelQueryFilter\QueryFilter;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
7+
abstract class QueryFilter
8+
{
9+
/** @var Builder */
10+
protected $builder;
11+
12+
public function __construct(Builder $builder)
13+
{
14+
$this->builder = $builder;
15+
}
16+
17+
public static function builder(Builder $builder)
18+
{
19+
return new static($builder);
20+
}
21+
22+
public function filters(){
23+
$staticClassMethods = get_class_methods(static::class);
24+
$selfClassMethods = get_class_methods(self::class);
25+
return array_diff($staticClassMethods, $selfClassMethods);
26+
}
27+
28+
public function apply(array $validated){
29+
foreach ($this->filters() as $filter){
30+
if(in_array($filter, array_keys($validated))){
31+
$this->$filter($validated[$filter], $validated);
32+
}
33+
}
34+
return $this;
35+
}
36+
37+
public function query()
38+
{
39+
return $this->builder;
40+
}
41+
}

src/Traits/Model/QueryFilter.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Oooiik\LaravelQueryFilter\Traits\Model;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
7+
/**
8+
* @method static Builder queryFilter(array $validated = [])
9+
*/
10+
trait QueryFilter
11+
{
12+
public function scopeQueryFilter(Builder $query, array $validated = [])
13+
{
14+
$modelName = class_basename(static::class);
15+
$queryFilterClassName = '\\App\\Filters\\Query\\' . $modelName . 'QueryFilter';
16+
if (class_exists($queryFilterClassName)) {
17+
return $queryFilterClassName::builder($query)->apply($validated)->query();
18+
} else {
19+
return $query;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)