-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite Filterable trait used for Squad filters (#658)
* reimplement basics * fix or groups at root level * add is not condition * add remaining filter+comments * fix tests * add new test case * styleci * fix crash with manual squads * fix test
- Loading branch information
1 parent
fe45844
commit d05ca29
Showing
6 changed files
with
271 additions
and
126 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 |
---|---|---|
|
@@ -3,3 +3,8 @@ | |
.env.php | ||
.env | ||
.idea/ | ||
|
||
# testing | ||
vendor/ | ||
composer.lock | ||
.phpunit.cache/test-results |
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,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to present Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Web\Exceptions; | ||
|
||
class InvalidFilterException extends \Exception | ||
{ | ||
|
||
} |
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,125 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to present Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Web\Models; | ||
|
||
use Closure; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
/** | ||
* Helper to build query where clauses that are either connected by AND or by OR. | ||
*/ | ||
class QueryGroupBuilder | ||
{ | ||
/** | ||
* @var bool Whether the where clauses should be linked by AND | ||
*/ | ||
protected bool $is_and_group; | ||
|
||
/** | ||
* @var Builder The query builder to add the where clauses to | ||
*/ | ||
protected Builder $query; | ||
|
||
/** | ||
* @param Builder $query The query builder to add the where clauses to | ||
* @param bool $is_and_group Whether the where clauses should be linked by AND | ||
*/ | ||
public function __construct(Builder $query, bool $is_and_group) { | ||
$this->query = $query; | ||
$this->is_and_group = $is_and_group; | ||
} | ||
|
||
/** | ||
* @return bool Returns true when the where clauses are linked by AND | ||
*/ | ||
public function isAndGroup(): bool { | ||
return $this->is_and_group; | ||
} | ||
|
||
/** | ||
* @return Builder The underlying query builder used for this group | ||
*/ | ||
public function getUnderlyingQuery(): Builder | ||
{ | ||
return $this->query; | ||
} | ||
|
||
/** | ||
* Either adds a 'where' or 'orWhere' to the query, depending on if it is an AND linked group or not. | ||
* | ||
* @param Closure $callback a callback to add constraints | ||
* @return $this | ||
* | ||
* @see Builder::where | ||
* @see Builder::orWhere | ||
*/ | ||
public function where(Closure $callback): QueryGroupBuilder { | ||
if($this->is_and_group){ | ||
$this->query->where($callback); | ||
} else { | ||
$this->query->orWhere($callback); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Either adds a 'whereHas' or 'orWhereHas' to the query, depending on if it is an AND linked group or not. | ||
* | ||
* @param string $relation the relation to check for existence | ||
* @param Closure $callback a callback to add more constraints | ||
* @return $this | ||
* | ||
* @see Builder::whereHas | ||
* @see Builder::orWhereHas | ||
*/ | ||
public function whereHas(string $relation, Closure $callback): QueryGroupBuilder { | ||
if($this->is_and_group){ | ||
$this->query->whereHas($relation, $callback); | ||
} else { | ||
$this->query->orWhereHas($relation, $callback); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Either adds a 'whereDoesntHave' or 'orWhereDoesntHave' to the query, depending on if it is an AND linked group or not. | ||
* | ||
* @param string $relation the relation to check for absence | ||
* @param Closure $callback a callback to add more constraints | ||
* @return $this | ||
* | ||
* @see Builder::whereDoesntHave | ||
* @see Builder::orWhereDoesntHave | ||
*/ | ||
public function whereDoesntHave(string $relation, Closure $callback): QueryGroupBuilder { | ||
if($this->is_and_group){ | ||
$this->query->whereDoesntHave($relation, $callback); | ||
} else { | ||
$this->query->orWhereDoesntHave($relation, $callback); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.