|
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\ParseFilterTrait; |
|
15
|
|
|
use Modules\Core\Traits\Criteria\ParseOrderByTrait; |
|
16
|
|
|
use Modules\Core\Traits\Criteria\ParseSearchableTrait; |
|
17
|
|
|
use Modules\Core\Traits\Criteria\ParseWithTrait; |
|
18
|
|
|
use Prettus\Repository\Contracts\CriteriaInterface; |
|
19
|
|
|
use Prettus\Repository\Contracts\RepositoryInterface; |
|
20
|
|
|
|
|
21
|
|
|
class RequestCriteria implements CriteriaInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \Illuminate\Http\Request $request */ |
|
24
|
|
|
protected $request; |
|
25
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */ |
|
26
|
|
|
protected $model; |
|
27
|
|
|
/** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */ |
|
28
|
|
|
protected $repository; |
|
29
|
|
|
protected $search; |
|
30
|
|
|
protected $searchData; |
|
31
|
|
|
protected $searchFields; |
|
32
|
|
|
protected $isFirstField; |
|
33
|
|
|
protected $modelForceAndWhere; |
|
34
|
|
|
protected $fieldsSearchable; |
|
35
|
|
|
protected $fields; |
|
36
|
|
|
protected $filter; |
|
37
|
|
|
protected $orderBy; |
|
38
|
|
|
protected $sortedBy; |
|
39
|
|
|
protected $with; |
|
40
|
|
|
protected $searchJoin; |
|
41
|
|
|
protected $acceptedConditions; |
|
42
|
|
|
protected $originalFields; |
|
43
|
|
|
protected $searchClosures; |
|
44
|
|
|
|
|
45
|
|
|
use ParseSearchableTrait; |
|
46
|
|
|
use ParseOrderByTrait; |
|
47
|
|
|
use ParseFilterTrait; |
|
48
|
|
|
use ParseWithTrait; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(Request $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->request = $request; |
|
53
|
|
|
$this->isFirstField = true; |
|
54
|
|
|
|
|
55
|
|
|
$this->setCrossSearchClosure(); |
|
56
|
|
|
$this->setBetweenSearchClosure(); |
|
57
|
|
|
$this->setInSearchClosure(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model |
|
62
|
|
|
* @param \Prettus\Repository\Contracts\RepositoryInterface $repository |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function apply($model, RepositoryInterface $repository) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->model = $model; |
|
71
|
|
|
$this->repository = $repository; |
|
72
|
|
|
|
|
73
|
|
|
$this->parseSearchable(); |
|
74
|
|
|
$this->parseOrderBy(); |
|
75
|
|
|
$this->parseFilter(); |
|
76
|
|
|
$this->parseWith(); |
|
77
|
|
|
|
|
78
|
|
|
return $this->model; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setSearchClosure(string $condition, Closure $closure) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->searchClosures[$condition] = $closure; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function setCrossSearchClosure() |
|
87
|
|
|
{ |
|
88
|
|
|
$crossMin = config('repository.criteria.cross.min', 'min'); |
|
89
|
|
|
$crossMax = config('repository.criteria.cross.min', 'max'); |
|
90
|
|
|
|
|
91
|
|
|
$this->setSearchClosure('cross', function (...$attributes) use ($crossMin, $crossMax) { |
|
92
|
|
|
$attributes[0]->where(function (Builder $query) use ($attributes, $crossMin, $crossMax) { |
|
93
|
|
|
$query->where("{$attributes[2]}_{$crossMin}", '<=', (int) $attributes[3][0]) |
|
94
|
|
|
->where("{$attributes[2]}_{$crossMax}", '>=', (int) $attributes[3][1]); |
|
95
|
|
|
})->orWhere(function (Builder $query) use ($attributes, $crossMin, $crossMax) { |
|
96
|
|
|
$query->where("{$attributes[2]}_{$crossMin}", '<=', (int) $attributes[3][0]) |
|
97
|
|
|
->where("{$attributes[2]}_{$crossMax}", '>=', (int) $attributes[3][0]); |
|
98
|
|
|
})->orWhere(function (Builder $query) use ($attributes, $crossMin, $crossMax) { |
|
99
|
|
|
$query->where("{$attributes[2]}_{$crossMin}", '>=', (int) $attributes[3][0]) |
|
100
|
|
|
->where("{$attributes[2]}_{$crossMax}", '<=', (int) $attributes[3][1]); |
|
101
|
|
|
})->orWhere(function (Builder $query) use ($attributes, $crossMin, $crossMax) { |
|
102
|
|
|
$query->where("{$attributes[2]}_{$crossMin}", '>=', (int) $attributes[3][0]) |
|
103
|
|
|
->where("{$attributes[2]}_{$crossMax}", '>=', (int) $attributes[3][1]) |
|
104
|
|
|
->where("{$attributes[2]}_{$crossMin}", '<=', (int) $attributes[3][1]); |
|
105
|
|
|
}); |
|
106
|
|
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function setBetweenSearchClosure() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->setSearchClosure('between', function (...$attributes) { |
|
112
|
|
|
$attributes[0]->whereBetween($attributes[2], $attributes[3]); |
|
113
|
|
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function setInSearchClosure() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->setSearchClosure('in', function (...$attributes) { |
|
119
|
|
|
$attributes[0]->whereIn($attributes[2], $attributes[3]); |
|
120
|
|
|
}); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|