1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: guoliang |
5
|
|
|
* Date: 2019/3/11 |
6
|
|
|
* Time: δΈε10:11. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Modules\Core\Criteria; |
10
|
|
|
|
11
|
|
|
use Closure; |
12
|
|
|
use Illuminate\Database\Eloquent\Builder; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Modules\Core\Traits\Criteria\ParseCrossTrait; |
|
|
|
|
15
|
|
|
use Modules\Core\Traits\Criteria\ParseFilterTrait; |
16
|
|
|
use Modules\Core\Traits\Criteria\ParseOrderByTrait; |
17
|
|
|
use Modules\Core\Traits\Criteria\ParseSearchableTrait; |
18
|
|
|
use Modules\Core\Traits\Criteria\ParseWithTrait; |
19
|
|
|
use Prettus\Repository\Contracts\CriteriaInterface; |
20
|
|
|
use Prettus\Repository\Contracts\RepositoryInterface; |
21
|
|
|
|
22
|
|
|
class RequestCriteria implements CriteriaInterface |
23
|
|
|
{ |
24
|
|
|
/** @var \Illuminate\Http\Request $request */ |
25
|
|
|
protected $request; |
26
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */ |
27
|
|
|
protected $model; |
28
|
|
|
/** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */ |
29
|
|
|
protected $repository; |
30
|
|
|
protected $search; |
31
|
|
|
protected $searchData; |
32
|
|
|
protected $searchFields; |
33
|
|
|
protected $isFirstField; |
34
|
|
|
protected $modelForceAndWhere; |
35
|
|
|
protected $fieldsSearchable; |
36
|
|
|
protected $fields; |
37
|
|
|
protected $filter; |
38
|
|
|
protected $orderBy; |
39
|
|
|
protected $sortedBy; |
40
|
|
|
protected $with; |
41
|
|
|
protected $searchJoin; |
42
|
|
|
protected $acceptedConditions; |
43
|
|
|
protected $originalFields; |
44
|
|
|
protected $searchClosures; |
45
|
|
|
|
46
|
|
|
use ParseSearchableTrait; |
47
|
|
|
use ParseOrderByTrait; |
48
|
|
|
use ParseFilterTrait; |
49
|
|
|
use ParseWithTrait; |
50
|
|
|
|
51
|
|
|
public function __construct(Request $request) |
52
|
|
|
{ |
53
|
|
|
$this->request = $request; |
54
|
|
|
$this->isFirstField = true; |
55
|
|
|
|
56
|
|
|
$this->setCrossSearchClosure(); |
57
|
|
|
$this->setBetweenSearchClosure(); |
58
|
|
|
$this->setInSearchClosure(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model |
63
|
|
|
* @param \Prettus\Repository\Contracts\RepositoryInterface $repository |
64
|
|
|
* |
65
|
|
|
* @throws \Exception |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function apply($model, RepositoryInterface $repository) |
70
|
|
|
{ |
71
|
|
|
$this->model = $model; |
72
|
|
|
$this->repository = $repository; |
73
|
|
|
|
74
|
|
|
$this->parseSearchable(); |
75
|
|
|
$this->parseOrderBy(); |
76
|
|
|
$this->parseFilter(); |
77
|
|
|
$this->parseWith(); |
78
|
|
|
|
79
|
|
|
return $this->model; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setSearchClosure(string $condition, Closure $closure) |
83
|
|
|
{ |
84
|
|
|
$this->searchClosures[$condition] = $closure; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function setCrossSearchClosure() |
88
|
|
|
{ |
89
|
|
|
$crossMin = config('repository.criteria.cross.min', 'min'); |
90
|
|
|
$crossMax = config('repository.criteria.cross.min', 'max'); |
91
|
|
|
|
92
|
|
|
$this->setSearchClosure('cross', function ( |
93
|
|
|
Builder $query, |
94
|
|
|
$condition, |
95
|
|
|
$field, |
96
|
|
|
$value, |
97
|
|
|
$modelTableName = null |
|
|
|
|
98
|
|
|
) use ($crossMin, $crossMax) { |
99
|
|
|
$query->where(function (Builder $query) use ($field, $value, $crossMin, $crossMax) { |
100
|
|
|
$query->where("{$field}_{$crossMin}", '<=', (int) $value[0]) |
101
|
|
|
->where("{$field}_{$crossMax}", '>=', (int) $value[1]); |
102
|
|
|
})->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) { |
103
|
|
|
$query->where("{$field}_{$crossMin}", '<=', (int) $value[0]) |
104
|
|
|
->where("{$field}_{$crossMax}", '>=', (int) $value[0]); |
105
|
|
|
})->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) { |
106
|
|
|
$query->where("{$field}_{$crossMin}", '>=', (int) $value[0]) |
107
|
|
|
->where("{$field}_{$crossMax}", '<=', (int) $value[1]); |
108
|
|
|
})->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) { |
109
|
|
|
$query->where("{$field}_{$crossMin}", '>=', (int) $value[0]) |
110
|
|
|
->where("{$field}_{$crossMax}", '>=', (int) $value[1]) |
111
|
|
|
->where("{$field}_{$crossMin}", '<=', (int) $value[1]); |
112
|
|
|
}); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function setBetweenSearchClosure() |
117
|
|
|
{ |
118
|
|
|
$this->setSearchClosure('between', function ( |
119
|
|
|
Builder $query, |
120
|
|
|
$condition, |
121
|
|
|
$field, |
122
|
|
|
$value, |
123
|
|
|
$modelTableName = null |
|
|
|
|
124
|
|
|
) { |
125
|
|
|
$query->whereBetween($field, $value); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function setInSearchClosure() |
130
|
|
|
{ |
131
|
|
|
$this->setSearchClosure('in', function (Builder $query, $condition, $field, $value, $modelTableName = null) { |
|
|
|
|
132
|
|
|
$query->whereIn($field, $value); |
133
|
|
|
}); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths