-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johannes Schobel
committed
Jan 8, 2017
1 parent
1cd3bd1
commit 9ebdd9a
Showing
9 changed files
with
504 additions
and
116 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
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
12 changes: 0 additions & 12 deletions
12
src/JohannesSchobel/DingoQueryMapper/LumenServiceProvider.php
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
src/JohannesSchobel/DingoQueryMapper/Operators/CollectionOperator.php
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,114 @@ | ||
<?php | ||
|
||
namespace JohannesSchobel\DingoQueryMapper\Operators; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use JohannesSchobel\DingoQueryMapper\Exceptions\UnknownColumnException; | ||
|
||
class CollectionOperator implements Operations | ||
{ | ||
protected $collection; | ||
protected $request; | ||
|
||
public function __construct(Collection $collection, Request $request) { | ||
$this->collection = $collection; | ||
$this->request = $request; | ||
} | ||
|
||
public function get() { | ||
return $this->collection; | ||
} | ||
|
||
public function paginate($page, $limit) { | ||
$slice = $this->collection->forPage($page, $limit); | ||
|
||
return new LengthAwarePaginator( | ||
$slice, // only the items needed | ||
count($this->collection), // total amount of items | ||
$limit, // items per page | ||
$page, // current page | ||
['path' => $this->request->url(), 'query' => $this->request->query()] // We need this so we can keep all old query parameters from the url | ||
); | ||
} | ||
|
||
public function sort(array $sorts) { | ||
$comparer = $this->callbackSearchable($sorts); | ||
$this->collection = $this->collection->sort($comparer); | ||
} | ||
|
||
public function filter(array $filters) { | ||
$filterer = $this->callbackFilterable($filters); | ||
$this->collection = $this->collection->filter($filterer); | ||
} | ||
|
||
private function callbackSearchable($criteria) { | ||
$callback = function ($first, $second) use ($criteria) { | ||
foreach ($criteria as $c) { | ||
// normalize sort direction | ||
$orderType = strtolower($c['direction']); | ||
if ($first[$c['column']] < $second[$c['column']]) { | ||
return $orderType === "asc" ? -1 : 1; | ||
} else if ($first[$c['column']] > $second[$c['column']]) { | ||
return $orderType === "asc" ? 1 : -1; | ||
} | ||
} | ||
// all elements were equal | ||
return 0; | ||
}; | ||
return $callback; | ||
} | ||
|
||
private function callbackFilterable($criteria) { | ||
$callback = function($item) use ($criteria) { | ||
$attributes = $item->getAttributes(); | ||
|
||
foreach ($criteria as $c) { | ||
// check, if the criteria to check is present | ||
if(! array_key_exists($c['key'], $attributes)) { | ||
// attribute does not exist - continue with the next one | ||
continue; | ||
} | ||
|
||
// the attribute exists - so check the operator and value | ||
$attribute = $item->getAttribute($c['key']); | ||
$rule = $this->createEvaluationRule($attribute, $c['operator'], $c['value']); | ||
|
||
$evalString = 'return(' . $rule . ');'; | ||
$result = (boolean) eval($evalString); | ||
|
||
if($result === false) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
return $callback; | ||
} | ||
|
||
private function createEvaluationRule($key, $operator, $value) { | ||
// first, check the operator type! | ||
if($operator == '=') $operator = '=='; | ||
$rule = "'%s' %s '%s'"; // key, operator, value | ||
$rule = sprintf($rule, $key, $operator, $value); | ||
|
||
// now check if the operator was "(not) like"? | ||
if (strpos($operator, 'like') !== false) { | ||
$value = str_replace('%', '', $value); | ||
$rule = "substr('%s', 0, strlen('%s')) %s '%s'"; // haystack, $needle, $comparable, $needle | ||
$expectedResult = '==='; | ||
|
||
if(stripos($operator, 'not') !== false) { | ||
// it is a NOT LIKE operator | ||
$expectedResult = '!=='; | ||
} | ||
|
||
$rule = sprintf($rule, $key, $value, $expectedResult, $value); | ||
} | ||
|
||
return $rule; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/JohannesSchobel/DingoQueryMapper/Operators/Operations.php
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,38 @@ | ||
<?php | ||
|
||
namespace JohannesSchobel\DingoQueryMapper\Operators; | ||
|
||
interface Operations | ||
{ | ||
/** | ||
* Get the entire result | ||
* | ||
* @return mixed | ||
*/ | ||
public function get(); | ||
|
||
/** | ||
* Get the result paginated | ||
* | ||
* @param $page | ||
* @param $limit | ||
* @return mixed | ||
*/ | ||
public function paginate($page, $limit); | ||
|
||
/** | ||
* Sort the result using sort criteria | ||
* | ||
* @param array $sorts | ||
* @return mixed | ||
*/ | ||
public function sort(array $sorts); | ||
|
||
/** | ||
* Filter the result using filter criteria | ||
* | ||
* @param array $filters | ||
* @return mixed | ||
*/ | ||
public function filter(array $filters); | ||
} |
Oops, something went wrong.