This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Field.php
244 lines (219 loc) · 6.11 KB
/
Field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace Meiko\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Config;
/**
* Filter field
*/
class Field
{
/**
* Filter key
*
* @var string
*/
protected $key;
/**
* Filter value
*
* @var string
*/
protected $value;
/**
* Possible query callback
*
* @var callable
*/
protected $callback;
/**
* Base classname
*
* @var string
*/
protected $baseClassname;
/**
* Available comparison types and their query counterparts
*
* @var array
*/
protected $types = [
'eq' => '=',
'ne' => '!=',
'gt' => '>',
'ge' => '>=',
'lt' => '<',
'le' => '<=',
'lk' => 'like',
'not-lk' => 'not like',
'in' => null,
'not-in' => null,
'bw' => null,
'not-bw' => null,
'ex' => null,
'not-ex' => null,
];
/**
* Query wildcard character
*
* @var string
*/
protected $wildcard = '*';
/**
* Create a new field instance
*
* @param string $key
* @param string $value
* @param callable|null $callback
*/
public function __construct(string $key, string $value, callable $callback = null)
{
$this->key = $key;
$this->value = $value;
$this->callback = $callback;
// change like and not like for postgres
$connection = Config::get('database.default');
if (Config::get('database.connections.' . $connection . '.driver') == 'pgsql') {
$this->types['lk'] = 'ilike';
$this->types['not-lk'] = 'not ilike';
}
}
/**
* Get field key
*
* @return void
*/
protected function getKey()
{
return $this->key;
}
/**
* Get field comparison type based on field value
*
* @return string
*/
protected function getType()
{
foreach (array_keys($this->types) as $type) {
if (strpos($this->value, $type.'=') === 0) {
return $type;
}
}
return (strpos($this->value, $this->wildcard) !== false) ? 'lk' : 'eq';
}
/**
* Is filter key an id key
*
* @return boolean
*/
protected function isIdKey()
{
return (substr($this->getKey(), -3) == '_id' || $this->getKey() == 'id');
}
/**
* Resolve model id from key
*
* @param string $id
* @return integer
*/
protected function getModelId($id)
{
if ($this->getKey() == 'id') {
$className = $this->baseClassname;
} else {
$namespace = config('filterable.namespace', '\App');
$modelName = studly_case(substr($this->getKey(), 0, -3));
$className = $namespace . '\\' . $modelName;
}
$idResolver = config('filterable.idResolver');
$instance = new $className;
$primaryKeyName = $instance->getKeyName();
if ($idResolver && class_exists($idResolver)) {
$resolver = new $idResolver;
$id = $resolver->resolve($modelName, $id, $instance->getRouteKeyName());
}
$model = $instance->where($instance->getRouteKeyName(), $id)->first();
return ($model) ? $model->$primaryKeyName : null;
}
/**
* Get field value
*
* @return string|null|array
*/
protected function getValue()
{
$value = preg_replace('/^('.implode('|', array_keys($this->types)).')=/i', '', $this->value);
$arrayTypes = ['bw', 'not-bw', 'in', 'not-in'];
$likeTypes = ['lk', 'not-lk'];
$matchTypes = ['eq', 'ne'];
$arraySeparator = '|';
if ($value == 'null') {
return null;
} elseif (in_array($this->getType(), $arrayTypes) || strpos($value, $arraySeparator) !== false) {
$arr = [];
foreach (explode($arraySeparator, $value) as $v) {
if ($this->isIdKey()) {
$id = $this->getModelId($v);
if ($id === null) {
continue;
}
$arr[] = $id;
} else {
$arr[] = $v;
}
}
return $arr;
} elseif (in_array($this->getType(), $likeTypes)) {
$queryWildcard = '%';
if (strpos($value, $this->wildcard) !== false) {
return str_replace($this->wildcard, $queryWildcard, $value);
} else {
return $queryWildcard . $value . $queryWildcard;
}
} elseif (in_array($this->getType(), $matchTypes) && $this->isIdKey()) {
$value = $this->getModelId($value);
}
return $value;
}
/**
* Apply filter to query
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function apply(Builder $query)
{
$key = $this->getKey();
$type = $this->getType();
$value = $this->getValue();
if ($this->callback) {
$query = ($this->callback)($query, $type, $value);
} elseif ($type == 'in') {
$query->whereIn($key, $value);
} elseif ($type == 'not-in') {
$query->whereNotIn($key, $value);
} elseif ($type == 'bw') {
$query->whereBetween($key, $value);
} elseif ($type == 'not-bw') {
$query->whereNotBetween($key, $value);
} elseif (is_null($value) && in_array($type, ['eq', 'ne'])) {
if ($type == 'eq') {
$query->whereNull($key);
} elseif ($type == 'ne') {
$query->whereNotNull($key);
}
} else {
$query->where($key, ((isset($this->types[$type])) ? $this->types[$type] : '='), $value);
}
return $query;
}
/**
* Set base classname to use against id-filters
*
* @param string $classname
* @return void
*/
public function setBaseClassname(string $classname)
{
$this->baseClassname = $classname;
}
}