Skip to content

Commit b6229a7

Browse files
committed
init
0 parents  commit b6229a7

File tree

10 files changed

+288
-0
lines changed

10 files changed

+288
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea
2+
/.vscode

CHANGELOG.md

Whitespace-only changes.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Laravel Eloquent Filters
2+
3+
Create eloquent filters easily
4+
5+
```shell
6+
php artisan make:filter UserFilter
7+
```

composer.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "laratoolbox/eloquent-filters",
3+
"description": "Add filters to eloquent models",
4+
"keywords": [
5+
"laratoolbox",
6+
"eloquent",
7+
"filters"
8+
],
9+
"homepage": "https://github.com/laratoolbox/eloquent-filters",
10+
"license": "MIT",
11+
"type": "library",
12+
"authors": [
13+
{
14+
"name": "Semih ERDOGAN",
15+
"email": "[email protected]",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7.0",
21+
"illuminate/support": "^5.5|^5.6|^5.7|^5.8|^6.0|^7.0|^8.0"
22+
},
23+
"require-dev": {
24+
"orchestra/testbench": "^3.5|^3.6|^3.7|^3.8|^4.0|^5.0|^6.0",
25+
"phpunit/phpunit": "~6.0|^7.0|^7.5|^8.4|^9.0"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"LaraToolbox\\EloquentFilters\\": "src"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"LaraToolbox\\EloquentFilters\\Tests\\": "tests"
35+
}
36+
},
37+
"scripts": {
38+
"test": "vendor/bin/phpunit",
39+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
40+
41+
},
42+
"config": {
43+
"sort-packages": true
44+
},
45+
"extra": {
46+
"laravel": {
47+
"providers": [
48+
"LaraToolbox\\EloquentFilters\\PackageServiceProvider"
49+
]
50+
}
51+
}
52+
}

src/Commands/MakeFilter.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace LaraToolbox\EloquentFilters\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class ShowTableColumnsCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'make:filter';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create a new filter class';
22+
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return int
37+
*/
38+
public function handle()
39+
{
40+
$filterClass = $this->option('name');
41+
42+
$filterContent = str_replace(
43+
[
44+
'{namespace}',
45+
'{className}',
46+
],
47+
[
48+
config('eloquent_filters.namespace', 'App\Filters'),
49+
$filterClass,
50+
],
51+
file_get_contents('../stub/filter.stub') // TODO: make stub customizable
52+
);
53+
54+
file_put_contents(
55+
"$filterClass.php",
56+
config('eloquent_filters.base_folder', app_path('Filters'))
57+
);
58+
59+
$this->info('Filter created.');
60+
return 0;
61+
}
62+
}

src/EloquentFilters.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace LaraToolbox\EloquentFilters;
4+
5+
abstract class FilterBase
6+
{
7+
/**
8+
* @var Request
9+
*/
10+
protected $request;
11+
12+
/**
13+
* The Eloquent builder.
14+
*
15+
* @var \Illuminate\Database\Eloquent\Builder
16+
*/
17+
protected $builder;
18+
19+
/**
20+
* Registered filters to operate upon.
21+
*
22+
* @var array
23+
*/
24+
protected $filters = [];
25+
26+
/**
27+
* Create a new ThreadFilters instance.
28+
*
29+
* @param \Illuminate\Http\Request $request
30+
*/
31+
public function __construct($request = null)
32+
{
33+
if (is_null($request)) {
34+
$request = request();
35+
}
36+
37+
$this->request = $request;
38+
}
39+
40+
/**
41+
* Apply the filters.
42+
*
43+
* @param \Illuminate\Database\Eloquent\Builder $builder
44+
*
45+
* @return \Illuminate\Database\Eloquent\Builder
46+
*/
47+
public function apply($builder)
48+
{
49+
$this->builder = $builder;
50+
51+
foreach ($this->getFilters() as $filter => $value) {
52+
if (method_exists($this, $filter)) {
53+
$this->$filter($value);
54+
}
55+
}
56+
57+
return $this->builder;
58+
}
59+
60+
/**
61+
* Fetch all relevant filters from the request.
62+
*
63+
* @return array
64+
*/
65+
public function getFilters()
66+
{
67+
return array_filter($this->request->only($this->filters), function ($value) {
68+
if (is_null($value)) {
69+
return false;
70+
}
71+
72+
if ($value === '') {
73+
return false;
74+
}
75+
76+
if (is_array($value) && count($value) === 0) {
77+
return false;
78+
}
79+
80+
return true;
81+
});
82+
}
83+
}

src/PackageServiceProvider.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace LaraToolbox\EloquentFilters;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use LaraToolbox\EloquentFilters\Commands\MakeFilter;
7+
8+
class DatabaseViewerServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application services.
12+
*/
13+
public function boot()
14+
{
15+
if ($this->app->runningInConsole()) {
16+
$this->commands([
17+
MakeFilter::class,
18+
]);
19+
}
20+
}
21+
22+
/**
23+
* Register the application services.
24+
*/
25+
public function register()
26+
{
27+
//
28+
}
29+
}

src/Trait/Filter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace LaraToolbox\EloquentFilters\Traits;
4+
5+
trait Filter
6+
{
7+
/**
8+
* Apply all relevant filters.
9+
*
10+
* @param \Illuminate\Database\Eloquent\Builder $query
11+
* @param $filters
12+
*
13+
* @return \Illuminate\Database\Eloquent\Builder
14+
*/
15+
public function scopeFilter($query, $filters)
16+
{
17+
if (!is_subclass_of($filters, \LaraToolbox\EloquentFilters\FilterBase::class)) {
18+
// TODO: throw custom exception here!
19+
}
20+
21+
return $filters->apply($query);
22+
}
23+
}

src/config/eloquent_filters.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'namespace' => 'App\Filters',
5+
6+
'base_folder' => app_path('Filters'),
7+
];

src/stub/filter.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace {namespace};
2+
3+
use LaraToolbox\EloquentFilters\FilterBase;
4+
5+
class {className} extends FilterBase
6+
{
7+
/**
8+
* Registered filters to operate upon.
9+
*
10+
* @var array
11+
*/
12+
protected $filters = [
13+
'example'
14+
];
15+
16+
/**
17+
* @return \Illuminate\Database\Eloquent\Builder
18+
*/
19+
protected function example()
20+
{
21+
// return $this->builder->where('', '');
22+
}
23+
}

0 commit comments

Comments
 (0)