-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
oooiik
committed
Oct 27, 2022
1 parent
4455868
commit 0881bf7
Showing
7 changed files
with
233 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Toshev Obidjon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Oooiik\LaravelQueryFilter\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
|
||
class MakeQueryFilter extends Command | ||
{ | ||
/** | ||
* The filesystem instance. | ||
* | ||
* @var Filesystem | ||
*/ | ||
protected $files; | ||
protected $modelName; | ||
|
||
protected $signature = 'make:query-filter {model}'; | ||
|
||
protected $description = 'Create a new query filter class'; | ||
|
||
public function __construct(Filesystem $files) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->files = $files; | ||
} | ||
|
||
public function handle() | ||
{ | ||
$model = $this->argument('model'); | ||
$this->setModelName($model); | ||
$this->buildClass(); | ||
} | ||
|
||
|
||
protected function buildClass() | ||
{ | ||
$stub = $this->replaceStub(); | ||
if (is_file($this->getGenerationPath())) { | ||
throw new InvalidArgumentException('Such a class exists!'); | ||
} | ||
$this->files->ensureDirectoryExists($this->getGenerationDir()); | ||
$this->files->put($this->getGenerationPath(), $stub); | ||
$this->info('QueryFilter created successfully'); | ||
} | ||
|
||
protected function getGenerationPath() | ||
{ | ||
return app_path('Filters/Query/' . $this->getGenerationClassName() . '.php'); | ||
} | ||
|
||
protected function getGenerationDir() | ||
{ | ||
return app_path('Filters/Query'); | ||
} | ||
|
||
protected function getGenerationClassName() | ||
{ | ||
return $this->modelName . 'QueryFilter'; | ||
} | ||
|
||
protected function hasModel($model) | ||
{ | ||
return class_exists($model); | ||
} | ||
|
||
protected function setModelName($model) | ||
{ | ||
$modelClass = '\\App\\Models\\' . $model; | ||
if (!$this->hasModel($modelClass)) { | ||
throw new InvalidArgumentException('Model not found!'); | ||
} | ||
$this->modelName = class_basename($model); | ||
} | ||
|
||
protected function replaceStub() | ||
{ | ||
$stub = str_replace( | ||
['{{ namespace }}', '{{ class }}'], | ||
[$this->getNamespace(), $this->getClassName()], | ||
$this->getStub() | ||
); | ||
|
||
return $stub; | ||
} | ||
|
||
protected function getStub() | ||
{ | ||
return $this->files->get(__DIR__ . '/stubs/query-filter.stub'); | ||
} | ||
|
||
protected function getNamespace() | ||
{ | ||
return 'App\Filters\Query'; | ||
} | ||
|
||
protected function getClassName() | ||
{ | ||
return $this->modelName . 'QueryFilter'; | ||
} | ||
|
||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use Oooiik\LaravelQueryFilter\QueryFilter\QueryFilter; | ||
|
||
class {{ class }} extends QueryFilter | ||
{ | ||
// | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Oooiik\LaravelQueryFilter\Providers; | ||
|
||
use Oooiik\LaravelQueryFilter\Console\MakeQueryFilter; | ||
|
||
class LaravelQueryFilterServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
// | ||
} | ||
|
||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
MakeQueryFilter::class, | ||
]); | ||
} | ||
} | ||
|
||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Oooiik\LaravelQueryFilter\QueryFilter; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
abstract class QueryFilter | ||
{ | ||
/** @var Builder */ | ||
protected $builder; | ||
|
||
public function __construct(Builder $builder) | ||
{ | ||
$this->builder = $builder; | ||
} | ||
|
||
public static function builder(Builder $builder) | ||
{ | ||
return new static($builder); | ||
} | ||
|
||
public function filters(){ | ||
$staticClassMethods = get_class_methods(static::class); | ||
$selfClassMethods = get_class_methods(self::class); | ||
return array_diff($staticClassMethods, $selfClassMethods); | ||
} | ||
|
||
public function apply(array $validated){ | ||
foreach ($this->filters() as $filter){ | ||
if(in_array($filter, array_keys($validated))){ | ||
$this->$filter($validated[$filter], $validated); | ||
} | ||
} | ||
return $this; | ||
} | ||
|
||
public function query() | ||
{ | ||
return $this->builder; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Oooiik\LaravelQueryFilter\Traits\Model; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
/** | ||
* @method static Builder queryFilter(array $validated = []) | ||
*/ | ||
trait QueryFilter | ||
{ | ||
public function scopeQueryFilter(Builder $query, array $validated = []) | ||
{ | ||
$modelName = class_basename(static::class); | ||
$queryFilterClassName = '\\App\\Filters\\Query\\' . $modelName . 'QueryFilter'; | ||
if (class_exists($queryFilterClassName)) { | ||
return $queryFilterClassName::builder($query)->apply($validated)->query(); | ||
} else { | ||
return $query; | ||
} | ||
} | ||
} |