1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Config; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Sco\Admin\Exceptions\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class ModelConfig extends Config implements ConfigInterface, Arrayable, Jsonable, JsonSerializable |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $orderBy = []; |
14
|
|
|
protected $modelFilters = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get Model |
18
|
|
|
* |
19
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
20
|
|
|
* @throws \Sco\Admin\Exceptions\InvalidArgumentException |
21
|
|
|
*/ |
22
|
|
|
protected function getModel() |
23
|
|
|
{ |
24
|
|
|
$modelName = $this->getOriginal('model'); |
25
|
|
|
if (class_exists($modelName)) { |
26
|
|
|
return new $modelName(); |
27
|
|
|
} |
28
|
|
|
throw new InvalidArgumentException("class {$modelName} not found"); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function filters($filters) |
32
|
|
|
{ |
33
|
|
|
$this->modelFilters = $filters; |
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function orderBy($column, $direction = 'asc') |
38
|
|
|
{ |
39
|
|
|
$this->orderBy = compact('column', 'direction'); |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function paginate() |
44
|
|
|
{ |
45
|
|
|
$data = $this->parseWhere() |
|
|
|
|
46
|
|
|
->orderBy($this->orderBy['column'], $this->orderBy['direction']) |
47
|
|
|
->paginate(20); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function parseWhere() |
51
|
|
|
{ |
52
|
|
|
$model = $this->getModel(); |
53
|
|
|
if (!empty($this->modelFilters)) { |
54
|
|
|
foreach ($this->modelFilters as $key => $filter) { |
55
|
|
|
list($operator, $value) = is_array($filter) ? $filter : ['=', $filter]; |
56
|
|
|
$model->where($key, $operator, $value); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $model; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|