Skip to content

Commit

Permalink
first logic
Browse files Browse the repository at this point in the history
  • Loading branch information
oooiik committed Oct 27, 2022
1 parent 4455868 commit 0881bf7
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 2 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "oooiik/laravel-query-filter",
"description": "",
"description": "In Laravel, it is convenient to write the query filter related to the model in a separate class",
"keywords": ["laravel", "query", "filter", "query filter"],
"type": "library",
"license": "MIT",
"authors": [
Expand All @@ -9,10 +10,19 @@
"email": "author@oooiik.com"
}
],
"require": {},
"require": {
"php": "^7.3|^8.0"
},
"autoload": {
"psr-4": {
"Oooiik\\LaravelQueryFilter\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Oooiik\\LaravelQueryFilter\\Providers\\LaravelQueryFilterServiceProvider"
]
}
}
}
104 changes: 104 additions & 0 deletions src/Console/MakeQueryFilter.php
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';
}

}
10 changes: 10 additions & 0 deletions src/Console/stubs/query-filter.stub
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
{
//
}
23 changes: 23 additions & 0 deletions src/Providers/LaravelQueryFilterServiceProvider.php
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,
]);
}
}

}
41 changes: 41 additions & 0 deletions src/QueryFilter/QueryFilter.php
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;
}
}
22 changes: 22 additions & 0 deletions src/Traits/Model/QueryFilter.php
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;
}
}
}

0 comments on commit 0881bf7

Please sign in to comment.