Skip to content

Commit 7524f6c

Browse files
author
Aris
committed
initial commit
0 parents  commit 7524f6c

File tree

8 files changed

+246
-0
lines changed

8 files changed

+246
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Muhamad Aris
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.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SRC Macro
2+
3+
**SRC Macro** merupakan package yang didalamnya terdapat kumpulan Macro untuk Laravel dan Lumen.
4+
5+
### Cara Install
6+
7+
- `composer require arispati/src-macro`
8+
9+
#### Laravel
10+
11+
- Otomatis terdaftar oleh `Laravel Package Discovery`
12+
13+
#### Lumen
14+
15+
- Daftarkan service provider di `bootstrap/app.php`
16+
```php
17+
$app->register(Arispati\SrcMacro\ServiceProvider::class);
18+
```
19+
20+
### Macro yang tersedia
21+
22+
- **Database - Query Builder**
23+
- [onSearch](#dqb-onSearch)
24+
- [onSort](#dqb-onSort)
25+
- [onFilter](#dqb-onFilter)
26+
27+
### Cara Penggunaan
28+
29+
- <a name="dqb-onSearch"></a> **onSearch**
30+
31+
Pencarian berdasarkan kolom yang sudah ditetapkan
32+
33+
```php
34+
onSearch(
35+
array $columns = [],
36+
// default query param dari front-end
37+
string $searchParam = 'search'
38+
)
39+
```
40+
41+
contoh:
42+
43+
```php
44+
$query = DB::table('namaTabel')->onSearch([
45+
'namaTabel.id', 'nama'
46+
])
47+
```
48+
49+
- <a name="dqb-onSort"></a> **onSort**
50+
51+
Order hasil query berdasarkan kolom yang sudah ditetapkan
52+
53+
```php
54+
onSort(
55+
array $columns = [],
56+
// default query param dari front-end
57+
string $sortParam = 'sort',
58+
string $sortTypeParam = 'sort_type'
59+
)
60+
```
61+
62+
contoh:
63+
64+
```php
65+
$query = DB::table('namaTabel')->onSort([
66+
'id',
67+
// dari query param => kolom pada query
68+
'nama' => 'namaTabel.nama'
69+
])
70+
```
71+
72+
- <a name="dqb-onFilter"></a> **onFilter**
73+
74+
Filter query berdasarkan kolom dan value
75+
76+
```php
77+
onFilter(array $columns) // kolom harus ditetapkan
78+
```
79+
80+
contoh:
81+
82+
```php
83+
$query = DB::table('namaTabel')->onFilter([
84+
'id',
85+
// dari query param => kolom pada query
86+
'nama' => 'namaTabel.nama'
87+
])
88+
```

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "arispati/src-macro",
3+
"description": "Kumpulan Macro untuk SRC",
4+
"license": "MIT",
5+
"keywords": [
6+
"arispati",
7+
"src-macro"
8+
],
9+
"authors": [
10+
{
11+
"name": "Muhamad Aris",
12+
"email": "masarispati@gmail.com"
13+
}
14+
],
15+
"require": {
16+
"illuminate/support": ">= 5.8 < 7.0",
17+
"illuminate/database": ">= 5.8 < 7.0"
18+
},
19+
"require-dev": {
20+
"squizlabs/php_codesniffer": "3.*"
21+
},
22+
"suggest": {
23+
"spatie/laravel-collection-macros": "A set of useful Laravel collection macros."
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Arispati\\SrcMacro\\": "src"
28+
}
29+
},
30+
"scripts": {
31+
"cs": "phpcs --standard=PSR12 src/"
32+
},
33+
"config": {
34+
"sort-packages": true
35+
},
36+
"extra": {
37+
"laravel": {
38+
"providers": [
39+
"Arispati\\SrcMacro\\ServiceProvider"
40+
]
41+
}
42+
},
43+
"minimum-stability": "stable"
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Arispati\SrcMacro\Macros\Database\QueryBuilder;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
Builder::macro('onFilter', function (array $columns) {
8+
if (count($columns)) {
9+
foreach ($columns as $alias => $column) {
10+
if (app('request')->filled($alias)) {
11+
$this->where($column, app('request')->get($alias));
12+
} elseif (app('request')->filled($column)) {
13+
$this->where($column, app('request')->get($column));
14+
}
15+
}
16+
}
17+
18+
return $this;
19+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Arispati\SrcMacro\Macros\Database\QueryBuilder;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
Builder::macro('onSearch', function (array $columns = [], string $searchParam = 'search') {
8+
if (app('request')->filled($searchParam) && count($columns)) {
9+
$this->where(function ($query) use ($columns, $searchParam) {
10+
foreach ($columns as $column) {
11+
$query->orWhere($column, 'like', '%' . app('request')->get($searchParam) . '%');
12+
}
13+
});
14+
}
15+
16+
return $this;
17+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Arispati\SrcMacro\Macros\Database\QueryBuilder;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
Builder::macro('onSort', function (
8+
array $columns = [],
9+
string $sortParam = 'sort',
10+
string $sortTypeParam = 'sort_type'
11+
) {
12+
if (app('request')->filled($sortParam)) {
13+
$sortParams = explode(',', app('request')->get($sortParam));
14+
$order = app('request')->filled($sortTypeParam)
15+
? app('request')->get($sortTypeParam)
16+
: 'asc';
17+
18+
foreach ($sortParams as $column) :
19+
if (count($columns)) {
20+
if (! is_numeric($column) && isset($columns[$column])) {
21+
$this->orderBy($columns[$column], $order);
22+
} elseif (in_array($column, $columns)) {
23+
$this->orderBy($column, $order);
24+
}
25+
} else {
26+
$this->orderBy($column, $order);
27+
}
28+
endforeach;
29+
}
30+
31+
return $this;
32+
});

src/ServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Arispati\SrcMacro;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
class ServiceProvider extends BaseServiceProvider
9+
{
10+
/**
11+
* Register the service provider.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
Collection::make(glob(__DIR__ . '/Macros/Database/QueryBuilder/*.php'))->mapWithKeys(function ($path) {
18+
return [$path => pathinfo($path, PATHINFO_FILENAME)];
19+
})->each(function ($macro, $path) {
20+
require_once $path;
21+
});
22+
}
23+
}

0 commit comments

Comments
 (0)