|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\View; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
|
8
|
|
|
use Sco\Admin\Contracts\View\Extensions\ExtensionInterface; |
|
9
|
|
|
use Sco\Admin\Contracts\View\ViewInterface; |
|
10
|
|
|
use Sco\Admin\View\Extensions\Applies; |
|
11
|
|
|
use Sco\Admin\View\Extensions\Filters; |
|
12
|
|
|
use Sco\Admin\View\Extensions\Scopes; |
|
13
|
|
|
use Sco\Admin\Contracts\View\Filters\FilterInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @method Scopes getScopes() get query scopes |
|
17
|
|
|
* @method $this setScopes($scope, ...$scopes) set query scopes |
|
18
|
|
|
* @method $this addScope($scope, $parameter, ...$parameters) add query scope |
|
19
|
|
|
* |
|
20
|
|
|
* @method Applies getApplies() get query applies |
|
21
|
|
|
* @method $this setApplies(\Closure $apply, ...$applies) set query applies |
|
22
|
|
|
* @method $this addApply(\Closure $apply) add query apply |
|
23
|
|
|
* |
|
24
|
|
|
* @method Filters getFilters() |
|
25
|
|
|
* @method $this setFilters(FilterInterface $filter, ...$filters) |
|
26
|
|
|
* @method $this addFilter(FilterInterface $filter) |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class View implements ViewInterface, Arrayable |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $with = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RepositoryInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $repository; |
|
40
|
|
|
|
|
41
|
|
|
protected $type; |
|
42
|
|
|
|
|
43
|
|
|
protected $extensions; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->extensions = new Extensions(); |
|
48
|
|
|
|
|
49
|
|
|
$this->extend('scopes', new Scopes()); |
|
50
|
|
|
$this->extend('applies', new Applies()); |
|
51
|
|
|
$this->extend('filters', new Filters()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function initialize() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->extensions->initialize(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function extend($name, ExtensionInterface $extension) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->extensions->put($name, $extension); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setRepository(RepositoryInterface $repository) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->repository = $repository; |
|
67
|
|
|
$this->repository->with($this->getWith()); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getRepository() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->repository; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string[] |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getWith() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->with; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function with($relations) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->with = array_flatten(func_get_args()); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getQuery() |
|
96
|
|
|
{ |
|
97
|
|
|
$repository = $this->getRepository(); |
|
98
|
|
|
|
|
99
|
|
|
$builder = $repository->getQuery(); |
|
100
|
|
|
|
|
101
|
|
|
if ($repository->isRestorable()) { |
|
102
|
|
|
$builder->withTrashed(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $builder; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Add an "order by" clause to the query. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $column |
|
112
|
|
|
* @param string $direction |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function orderBy($column, $direction = 'asc') |
|
117
|
|
|
{ |
|
118
|
|
|
$this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) { |
|
|
|
|
|
|
119
|
|
|
$builder->orderBy($column, $direction); |
|
|
|
|
|
|
120
|
|
|
}; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function toArray() |
|
126
|
|
|
{ |
|
127
|
|
|
return [ |
|
128
|
|
|
'type' => $this->type, |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function __call($name, $parameters) |
|
133
|
|
|
{ |
|
134
|
|
|
$method = snake_case(substr($name, 3)); |
|
135
|
|
|
|
|
136
|
|
|
if (starts_with($name, 'get') && $this->extensions->has($method)) { |
|
137
|
|
|
return $this->extensions->get($method); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
if (starts_with($name, 'set') && $this->extensions->has($method)) { |
|
|
|
|
|
|
141
|
|
|
$extension = $this->extensions->get($method); |
|
142
|
|
|
call_user_func_array([$extension, 'set'], $parameters); |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
View Code Duplication |
if (starts_with($name, 'add') && $this->extensions->has(str_plural($method))) { |
|
|
|
|
|
|
148
|
|
|
$extension = $this->extensions->get(str_plural($method)); |
|
149
|
|
|
call_user_func_array([$extension, 'add'], $parameters); |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
throw new \BadMethodCallException("Call to undefined method [{$name}]"); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: