diff --git a/composer.json b/composer.json index 56133b6..abe7f5e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "require": { "php": ">=7.1.0", - "illuminate/support": "5.*|6.*" + "illuminate/support": "5.*|6.*|7.*" }, "require-dev": { "composer/composer": "^1.7", diff --git a/src/AdvancedSearchTrait.php b/src/AdvancedSearchTrait.php index fba002c..23d0dc6 100644 --- a/src/AdvancedSearchTrait.php +++ b/src/AdvancedSearchTrait.php @@ -3,6 +3,7 @@ namespace MatrixLab\LaravelAdvancedSearch; use Closure; +use Illuminate\Support\Arr; use ReflectionClass; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Builder; @@ -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', @@ -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', @@ -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) { @@ -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, @@ -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; @@ -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'), ]; } @@ -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) { @@ -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) { @@ -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); } @@ -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; } diff --git a/src/ConditionsGeneratorTrait.php b/src/ConditionsGeneratorTrait.php index 55fd60c..a67ad64 100644 --- a/src/ConditionsGeneratorTrait.php +++ b/src/ConditionsGeneratorTrait.php @@ -3,6 +3,7 @@ namespace MatrixLab\LaravelAdvancedSearch; use Closure; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Database\Query\Expression; @@ -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); @@ -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); } } @@ -211,15 +212,15 @@ 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) { @@ -227,7 +228,7 @@ protected function handlePaginate() 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) { @@ -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); } /** @@ -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) !== ''; } /** diff --git a/src/WithAndSelectForGraphQLGeneratorTrait.php b/src/WithAndSelectForGraphQLGeneratorTrait.php index 182d2aa..95ecdd7 100644 --- a/src/WithAndSelectForGraphQLGeneratorTrait.php +++ b/src/WithAndSelectForGraphQLGeneratorTrait.php @@ -2,6 +2,7 @@ namespace MatrixLab\LaravelAdvancedSearch; +use Illuminate\Support\Arr; use ReflectionClass; use Illuminate\Database\Eloquent\Model; use GraphQL\Type\Definition\ResolveInfo; @@ -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)); }