|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPHub\Repositories\Criteria; |
|
4
|
|
|
|
|
5
|
|
|
use Prettus\Repository\Contracts\RepositoryInterface; |
|
6
|
|
|
use Prettus\Repository\Criteria\RequestCriteria; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Created by PhpStorm. |
|
10
|
|
|
* User: xuan |
|
11
|
|
|
* Date: 9/22/15 |
|
12
|
|
|
* Time: 8:05 AM. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class BaseCriteria extends RequestCriteria |
|
15
|
|
|
{ |
|
16
|
|
|
public function apply($builder, RepositoryInterface $repository) |
|
17
|
|
|
{ |
|
18
|
|
|
$fieldsSearchable = $repository->getFieldsSearchable(); |
|
19
|
|
|
$search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
|
20
|
|
|
$searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null); |
|
21
|
|
|
$filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
|
22
|
|
|
$orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
|
23
|
|
|
$sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
|
24
|
|
|
$sortedBy = ! empty($sortedBy) ? $sortedBy : 'asc'; |
|
25
|
|
|
|
|
26
|
|
|
if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
|
27
|
|
|
$searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', |
|
28
|
|
|
$searchFields); |
|
29
|
|
|
$fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
|
30
|
|
|
$isFirstField = true; |
|
31
|
|
|
$searchData = $this->parserSearchData($search); |
|
32
|
|
|
$search = $this->parserSearchValue($search); |
|
|
|
|
|
|
33
|
|
|
$modelForceAndWhere = false; |
|
34
|
|
|
|
|
35
|
|
|
$builder = $builder->where(function ($query) use ( |
|
36
|
|
|
$fields, |
|
37
|
|
|
$search, |
|
38
|
|
|
$searchData, |
|
39
|
|
|
$isFirstField, |
|
40
|
|
|
$modelForceAndWhere |
|
41
|
|
|
) { |
|
42
|
|
|
foreach ($fields as $field => $condition) { |
|
43
|
|
|
if (is_numeric($field)) { |
|
44
|
|
|
$field = $condition; |
|
45
|
|
|
$condition = '='; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$value = null; |
|
49
|
|
|
|
|
50
|
|
|
$condition = trim(strtolower($condition)); |
|
51
|
|
|
|
|
52
|
|
|
if (isset($searchData[$field])) { |
|
53
|
|
|
$value = $condition === 'like' ? "%{$searchData[$field]}%" : $searchData[$field]; |
|
54
|
|
|
} else { |
|
55
|
|
|
if (! is_null($search)) { |
|
56
|
|
|
$value = $condition === 'like' ? "%{$search}%" : $search; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($isFirstField || $modelForceAndWhere) { |
|
61
|
|
|
if (! is_null($value)) { |
|
62
|
|
|
$query->where($field, $condition, $value); |
|
63
|
|
|
$isFirstField = false; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
if (! is_null($value)) { |
|
67
|
|
|
$query->orWhere($field, $condition, $value); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (isset($orderBy) && in_array(strtolower($sortedBy), ['asc', 'desc'])) { |
|
75
|
|
|
$builder = $builder->orderBy($orderBy, $sortedBy); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach (FilterManager::get() as $filter) { |
|
79
|
|
|
// eg. filter 'hot' 会调用方法 'filterHot' |
|
80
|
|
|
$method_name = camel_case('filter_'.$filter); |
|
81
|
|
|
if (method_exists($this, $method_name)) { |
|
82
|
|
|
$builder = call_user_func([$this, $method_name], $builder); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $builder; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.