Skip to content

Commit

Permalink
support laravel 7
Browse files Browse the repository at this point in the history
fix sort bug when order by id
remove helper dependence
  • Loading branch information
zhichao-poper committed Apr 14, 2020
1 parent 96f52e0 commit 7927a19
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=7.1.0",
"illuminate/support": "5.*|6.*"
"illuminate/support": "5.*|6.*|7.*"
},
"require-dev": {
"composer/composer": "^1.7",
Expand Down
37 changes: 19 additions & 18 deletions src/AdvancedSearchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MatrixLab\LaravelAdvancedSearch;

use Closure;
use Illuminate\Support\Arr;
use ReflectionClass;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
Expand Down Expand Up @@ -95,7 +96,7 @@ public static function getList($conditions = [], $with = [], $selects = ['*'], $
$query = static::getListQuery($conditions, $with, $withTrashed);

// 根据请求中是否存在 page 参数来返回 collection | paginator
if (array_has($conditions, 'page')) {
if (Arr::has($conditions, 'page')) {
/* @var Builder $query */
return $query->paginate((int) (self::getPageSize($conditions)), $selects, 'page', $conditions['page'])->appends(request()->except([
'page',
Expand All @@ -121,7 +122,7 @@ public static function getSimpleList($conditions = [], $with = [], $selects = ['
$query = static::getListQuery($conditions, $with, $withTrashed);

// 根据请求中是否存在 page 参数来返回 collection | paginator
if (array_has($conditions, 'page')) {
if (Arr::has($conditions, 'page')) {
/* @var Builder $query */
return $query->simplePaginate((int) (self::getPageSize($conditions)), $selects, 'page', $conditions['page'])->appends(request()->except([
'page',
Expand Down Expand Up @@ -180,12 +181,12 @@ private static function getQueryForSearch($with)
private static function simpleLikeSearch($query, $conditions)
{
$keyword = '';
if (array_has($conditions, 'keyword')) {
$keyword = array_get($conditions, 'keyword', '');
} elseif (array_has($conditions, 'search')) {
$keyword = array_get($conditions, 'search', '');
} elseif (array_has($conditions, 'key')) {
$keyword = array_get($conditions, 'key', '');
if (Arr::has($conditions, 'keyword')) {
$keyword = Arr::get($conditions, 'keyword', '');
} elseif (Arr::has($conditions, 'search')) {
$keyword = Arr::get($conditions, 'search', '');
} elseif (Arr::has($conditions, 'key')) {
$keyword = Arr::get($conditions, 'key', '');
}

if ($keyword) {
Expand All @@ -212,12 +213,12 @@ private static function whereSearch($query, $conditions)
foreach ($wheres as $where) {
if (is_array($where)) {
foreach ($where as $field => $operatorAndValue) {
$mixType = array_get($operatorAndValue, 'mix', 'and');
$mixType = Arr::get($operatorAndValue, 'mix', 'and');
unset($operatorAndValue['mix']);

// 关联查询
if (str_contains($field, '.')) {
list($relation, $field) = explode('.', $field);
[$relation, $field] = explode('.', $field);
$query->whereHas(camel_case($relation), function ($q) use (
$operatorAndValue,
$mixType,
Expand Down Expand Up @@ -298,7 +299,7 @@ private static function sortOutWhereConditionsPro($conditions)
{
$newConditions = [];

foreach (array_get($conditions, 'wheres', []) as $key => $item) {
foreach (Arr::get($conditions, 'wheres', []) as $key => $item) {
if ($item instanceof Closure || $item instanceof Expression || $item instanceof ModelScope) {
$newConditions[] = $item;
continue;
Expand Down Expand Up @@ -361,7 +362,7 @@ private static function orderSearch($query, $conditions)
$order = isset($conditions['order']) ? $conditions['order'] : [];
if (is_string($order)) {
$order = [
$order => array_get($conditions, 'direction', 'desc'),
$order => Arr::get($conditions, 'direction', 'desc'),
];
}

Expand Down Expand Up @@ -399,7 +400,7 @@ private static function havingSearch(Builder $query, $conditions)
if (is_array($having)) {
foreach ($having as $field => $operatorAndValue) {
$having_raws = [];
$mixType = array_get($operatorAndValue, 'mix', 'and');
$mixType = Arr::get($operatorAndValue, 'mix', 'and');
unset($operatorAndValue['mix']);

foreach ($operatorAndValue as $operator => $value) {
Expand Down Expand Up @@ -431,7 +432,7 @@ private static function sortOutHavingConditions($conditions)
{
$newConditions = [];

foreach (array_get($conditions, 'having', []) as $key => $item) {
foreach (Arr::get($conditions, 'having', []) as $key => $item) {
if (is_int($key)) {
// 如果是闭包的话,直接 push ,不做处理,构造时进行处理
if ($item instanceof Closure || $item instanceof Expression || $item instanceof ModelScope) {
Expand Down Expand Up @@ -476,8 +477,8 @@ private static function sortOutHavingConditions($conditions)
*/
private static function offsetSearch($query, $conditions)
{
$offset = array_has($conditions, 'offset') ? $conditions['offset'] : 0;
$limit = array_has($conditions, 'limit') ? $conditions['limit'] : 0;
$offset = Arr::has($conditions, 'offset') ? $conditions['offset'] : 0;
$limit = Arr::has($conditions, 'limit') ? $conditions['limit'] : 0;
if ($limit > 0) {
$query->skip((int) $offset)->take((int) $limit);
}
Expand All @@ -494,8 +495,8 @@ private static function getPageSize($conditions)
{
$pageSize = request()->has('pageSize') ? request('pageSize') : config('erp.page_size');
$pageSize = request()->has('page_size') ? request('page_size') : $pageSize;
$pageSize = array_has($conditions, 'pageSize') ? $conditions['pageSize'] : $pageSize;
$pageSize = array_has($conditions, 'page_size') ? $conditions['page_size'] : $pageSize;
$pageSize = Arr::has($conditions, 'pageSize') ? $conditions['pageSize'] : $pageSize;
$pageSize = Arr::has($conditions, 'page_size') ? $conditions['page_size'] : $pageSize;

return $pageSize;
}
Expand Down
33 changes: 17 additions & 16 deletions src/ConditionsGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MatrixLab\LaravelAdvancedSearch;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Database\Query\Expression;

Expand Down Expand Up @@ -39,8 +40,8 @@ trait ConditionsGeneratorTrait
public function appendConditions($appendItems)
{
// 合并到 wheres
$this->conditions['wheres'] = array_merge($this->conditions['wheres'], array_get($appendItems, 'wheres', []));
array_forget($appendItems, 'wheres');
$this->conditions['wheres'] = array_merge($this->conditions['wheres'], Arr::get($appendItems, 'wheres', []));
Arr::forget($appendItems, 'wheres');

// 追加数据到根节点
$this->conditions = array_merge($this->conditions, $appendItems);
Expand Down Expand Up @@ -197,11 +198,11 @@ protected function handlePaginate()
{
// 处理页码和分页
foreach ($this->getPageAlias() as $key => $value) {
if (array_has($this->inputArgs, $key)) {
if (Arr::has($this->inputArgs, $key)) {
$this->appendConditions([
$value => $this->getInputArgs($key),
]);
array_forget($this->inputArgs, $key);
Arr::forget($this->inputArgs, $key);
}
}

Expand All @@ -211,23 +212,23 @@ protected function handlePaginate()

// 处理排序
$sorts = [];
if (array_has($this->inputArgs, 'paginator.sort')) {
$sorts = array_merge($sorts, [$this->getInputArgs('paginator.sort')]);
array_forget($this->inputArgs, 'paginator.sort');
if (Arr::has($this->inputArgs, 'paginator.sort')) {
$sorts = array_merge([$this->getInputArgs('paginator.sort')], $sorts);
Arr::forget($this->inputArgs, 'paginator.sort');
}
if (array_has($this->inputArgs, 'paginator.sorts')) {
$sorts = array_merge($sorts, $this->getInputArgs('paginator.sorts'));
array_forget($this->inputArgs, 'paginator.sorts');
if (Arr::has($this->inputArgs, 'paginator.sorts')) {
$sorts = array_merge($this->getInputArgs('paginator.sorts'), $sorts);
Arr::forget($this->inputArgs, 'paginator.sorts');
}
$sorts = collect($sorts)->filter();
$sorts = collect(array_values(array_unique($sorts)))->filter();
$sorts = collect($this->order())->merge($sorts);
$orders = [];
foreach ($sorts as $sort) {
if (is_string($sort)) {
if (! starts_with($sort, ['+', '-'])) {
continue;
}
$orders[substr($sort, 1)] = $sort[0] == '+' ? 'asc' : 'desc';
$orders[substr($sort, 1)] = strpos($sort, '+') === 0 ? 'asc' : 'desc';
}

if ($sort instanceof Expression) {
Expand Down Expand Up @@ -335,7 +336,7 @@ protected function handleInputArgs()
*/
public function getInputArgs($key = null, $default = null)
{
return is_null($key) ? $this->inputArgs : array_get($this->inputArgs, $key, $default);
return is_null($key) ? $this->inputArgs : Arr::get($this->inputArgs, $key, $default);
}

/**
Expand All @@ -347,9 +348,9 @@ public function getInputArgs($key = null, $default = null)
*/
protected function isVaildInput($requestKey)
{
return array_has($this->inputArgs, $requestKey)
&& array_get($this->inputArgs, $requestKey) !== null
&& array_get($this->inputArgs, $requestKey) !== '';
return Arr::has($this->inputArgs, $requestKey)
&& Arr::get($this->inputArgs, $requestKey) !== null
&& Arr::get($this->inputArgs, $requestKey) !== '';
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/WithAndSelectForGraphQLGeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MatrixLab\LaravelAdvancedSearch;

use Illuminate\Support\Arr;
use ReflectionClass;
use Illuminate\Database\Eloquent\Model;
use GraphQL\Type\Definition\ResolveInfo;
Expand All @@ -20,12 +21,12 @@ public static function getGraphQLPaginator($conditions, ResolveInfo $info)
$fields = $info->getFieldSelection(5);

// 如果没有 total 则返回简单分页
if (! array_has($fields, 'cursor.total')) {
if (! Arr::has($fields, 'cursor.total')) {
return static::getSimpleList($conditions, ...static::getWithAndSelect($info));
}

// 如果有查询内容的话
if (array_has($fields, 'items')) {
if (Arr::has($fields, 'items')) {
return static::getList($conditions, ...static::getWithAndSelect($info));
}

Expand Down

0 comments on commit 7927a19

Please sign in to comment.