Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.

Commit abbc808

Browse files
committed
🐘⚡️
0 parents  commit abbc808

File tree

8 files changed

+350
-0
lines changed

8 files changed

+350
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
/composer.phar
3+
/composer.lock
4+
.DS_Store
5+
.vscode

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) 2018 Awssat Developers
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Introduction
2+
Clear previously set statements in Laravel query builder/Eloqent builder easily.
3+
4+
## Features
5+
- Support most of query builder statemets. (where, limit, order etc..)
6+
- Intuitive, just type ->reset{..} followed by the statement name like: resetLimit()
7+
- Well tested (check tests folder).
8+
- Easy to use ..
9+
10+
11+
## Install
12+
13+
Via Composer
14+
``` bash
15+
composer require awssat/laravel-query-test
16+
```
17+
18+
### Before Laravel 5.5
19+
You'll need to manually register `Awssat\QueryReset\QueryResetServiceProvider::class` service provider in `config/app.php`.
20+
21+
## Usage
22+
| Query\Builder method | Description |
23+
| --- | --- |
24+
| resetOrder() | remove all `order by ...` statements from the query |
25+
| resetLimit() | remove all `limit ...` statements from the query |
26+
| resetOffset() | remove all `offset ...` statements from the query |
27+
| resetHaving() | remove all `having ...` statements from the query |
28+
| resetWhere() | remove all `where ...` statements from the query |
29+
| resetSelect() | remove all `select ...` statements from the query |
30+
| resetJoin() | remove all `join ...` statements from the query |
31+
| resetDistinct() | remove all `distinct ...` statements from the query |
32+
| resetGroup() | remove all `group by ...` statements from the query |
33+
| resetAggregate() | remove all aggregate's methods statements from the query such as `count`, `max`, `min`, `avg`, and `sum`. |
34+
35+
| Eloquent\Builder method | Description |
36+
| --- | --- |
37+
| resetWith() or resetEagerLoad() | remove all eager Loads |
38+
| resetScopes() | remove all restrictive scopes |
39+
40+
41+
### Examples
42+
```php
43+
$model->orderBy('id')->resetOrder()
44+
```
45+
46+
```php
47+
$model->with('items')->resetWith()
48+
```
49+
50+
## Usage case
51+
if you're using statements in your relations defining methods or using built in laravel realtions that's using statement by default such as `order by` or a model's scope that interfere with your query ... and you don't want that for a specific query call .. use this package.
52+
53+
54+
55+
## License
56+
57+
This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "awssat/laravel-query-reset",
3+
"type": "library",
4+
"description": "Clear previously set statements in Laravel query builder",
5+
"keywords": ["laravel", "builder", "clear", "reset", "query"],
6+
"license": "MIT",
7+
"homepage": "https://github.com/awssat/laravel-query-reset",
8+
"authors": [
9+
{
10+
"name": "Awssat developers",
11+
"email": "hello@awssat.com"
12+
}
13+
],
14+
"require": {
15+
"illuminate/database": "5.*",
16+
"illuminate/support": "5.*"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^6.1",
20+
"mockery/mockery": "~1.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Awssat\\QueryReset\\": "src"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Awssat\\QueryReset\\Tests\\": "tests"
30+
}
31+
},
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"Awssat\\QueryReset\\QueryResetServiceProvider"
36+
]
37+
}
38+
}
39+
}

src/EloquentReset.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Awssat\QueryReset;
4+
5+
class EloquentReset
6+
{
7+
protected static $statements = [
8+
'with' => ['variable' => 'eagerLoad', 'set' => []],
9+
'eagerLoad' => ['variable' => 'eagerLoad', 'set' => []],
10+
'scopes' => ['variable' => 'scopes', 'set' => []],
11+
];
12+
13+
public static function getMethods()
14+
{
15+
return array_keys(static::$statements);
16+
}
17+
18+
public static function __callStatic($name, $arguments)
19+
{
20+
$name = lcfirst(str_replace('reset', '', $name));
21+
22+
if (array_key_exists($name, static::$statements)) {
23+
$currentStatement = static::$statements[$name];
24+
25+
return function () use ($name, $currentStatement) {
26+
$cname = $currentStatement['variable'];
27+
28+
$this->{$cname} = $currentStatement['set'];
29+
30+
return $this;
31+
};
32+
}
33+
}
34+
}

src/QueryReset.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Awssat\QueryReset;
4+
5+
class QueryReset
6+
{
7+
protected static $statements = [
8+
'order' => ['variable' => 'orders', 'unions' => true, 'set' => null],
9+
'limit' => ['variable' => 'limit', 'unions' => true, 'set' => null],
10+
'offset' => ['variable' => 'offset', 'unions' => true, 'set' => null],
11+
'having' => ['variable' => 'havings', 'set' => null],
12+
'where' => ['variable' => 'wheres', 'set' => []],
13+
'select' => ['variable' => 'columns', 'set' => null],
14+
'join' => ['variable' => 'joins', 'set' => null],
15+
'distinct' => ['variable' => 'distinct', 'set' => null],
16+
'aggregate' => ['variable' => 'aggregate', 'set' => null],
17+
'group' => ['variable' => 'groups', 'set' => null],
18+
];
19+
20+
public static function getMethods()
21+
{
22+
return array_keys(static::$statements);
23+
}
24+
25+
public static function __callStatic($name, $arguments)
26+
{
27+
$name = lcfirst(str_replace('reset', '', $name));
28+
29+
if (array_key_exists($name, static::$statements)) {
30+
$currentStatement = static::$statements[$name];
31+
32+
return function () use ($name, $currentStatement) {
33+
$cname = $currentStatement['variable'];
34+
$unions = $currentStatement['unions'] ?? false;
35+
36+
$this->{$this->unions && $unions ? 'unions'.ucfirst($cname) : $cname} = $currentStatement['set'];
37+
38+
return $this;
39+
};
40+
}
41+
}
42+
}

src/QueryResetServiceProvider.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Awssat\QueryReset;
4+
5+
use Illuminate\Database\Query\Builder;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Support\ServiceProvider;
8+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
9+
10+
class QueryResetServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Add ->reset* methods to query builder
14+
*/
15+
public function register()
16+
{
17+
//Query builder
18+
Collection::make(QueryReset::getMethods())
19+
->reject(function($method) {
20+
return Builder::hasMacro('reset'. ucfirst($method));
21+
})
22+
->each(function($method) {
23+
Builder::macro('reset'. ucfirst($method), QueryReset::$method());
24+
});
25+
26+
//Eloquent builder
27+
Collection::make(EloquentReset::getMethods())
28+
->reject(function($method) {
29+
return EloquentBuilder::hasMacro('reset'. ucfirst($method));
30+
})
31+
->each(function($method) {
32+
EloquentBuilder::macro('reset'. ucfirst($method), EloquentReset::$method());
33+
});
34+
}
35+
}

tests/QueryResetTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Awssat\QueryReset\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Awssat\QueryReset\QueryReset;
7+
use Awssat\QueryReset\EloquentReset;
8+
use Illuminate\Database\Query\Builder;
9+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
10+
use Mockery as m;
11+
12+
13+
14+
class QueryResetTest extends TestCase
15+
{
16+
17+
public function setUp()
18+
{
19+
parent::setUp();
20+
21+
//load macros
22+
collect(QueryReset::getMethods())
23+
->each(function ($item) {
24+
$name = 'reset'.ucfirst($item);
25+
Builder::macro('reset' . ucfirst($item), QueryReset::{$name}());
26+
});
27+
28+
collect(EloquentReset::getMethods())
29+
->each(function ($item) {
30+
$name = 'reset'.ucfirst($item);
31+
EloquentBuilder::macro('reset'.ucfirst($item), EloquentReset::{$name}());
32+
});
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function query_rest_class_get_methods_return_non_empty_array()
39+
{
40+
$a = QueryReset::getMethods();
41+
42+
$this->assertTrue(is_array($a) && count($a) > 0);
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function eloquent_rest_class_get_methods_return_non_empty_array()
49+
{
50+
$a = EloquentReset::getMethods();
51+
52+
$this->assertTrue(is_array($a) && count($a) > 0);
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function static_call_of_resetMethod_return_callable()
59+
{
60+
$this->assertTrue(is_callable(QueryReset::resetLimit()));
61+
$this->assertTrue(is_callable(EloquentReset::resetWith()));
62+
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function query_rest_methods_added_successfully_to_builder()
68+
{
69+
collect(QueryReset::getMethods())
70+
->each(function($item) {
71+
$name = 'reset' . ucfirst($item);
72+
$this->assertTrue(Builder::hasMacro($name), "{$name} macro is not loaded!");
73+
});
74+
}
75+
76+
/**
77+
* @test
78+
*/
79+
public function eloquent_rest_methods_added_successfully_to_builder()
80+
{
81+
//EloquentBuilder doesnt have 'hasMacro' currently .. so.. later!
82+
$this->assertTrue(true);
83+
}
84+
85+
86+
/**
87+
* @test
88+
*/
89+
public function query_rest_methods_should_really_really_reset()
90+
{
91+
$this->assertNotContains('limit 3', $this->getBuilder()->from('items')->limit(3)->resetLimit()->where('id', '=', 2)->toSql());
92+
$this->assertNotContains('order by', $this->getBuilder()->from('items')->limit(3)->resetLimit()->where('id', '=', 2)->toSql());
93+
$this->assertNotContains('offset 12', $this->getBuilder()->from('items')->offset(12)->resetOffset()->orderBy('id')->toSql());
94+
$this->assertNotContains('name_one', $this->getBuilder()->select('name_one')->from('items')->offset(12)->resetSelect()->toSql());
95+
$this->assertNotContains('distinct', $this->getBuilder()->distinct()->from('items')->offset(12)->resetDistinct()->toSql());
96+
}
97+
98+
/**
99+
* @test
100+
*/
101+
public function eloquent_rest_methods_should_really_really_reset()
102+
{
103+
$this->assertCount(0, $this->getEloquentBuilder()->with(['items'])->resetWith()->getEagerLoads());
104+
}
105+
106+
protected function getBuilder()
107+
{
108+
$grammar = new \Illuminate\Database\Query\Grammars\Grammar;
109+
$processor = m::mock('Illuminate\Database\Query\Processors\Processor');
110+
return new Builder(m::mock('Illuminate\Database\ConnectionInterface'), $grammar, $processor);
111+
}
112+
113+
protected function getEloquentBuilder()
114+
{
115+
return new EloquentBuilder($this->getBuilder());
116+
}
117+
}

0 commit comments

Comments
 (0)