1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\View; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
9
|
|
|
use Sco\Admin\Contracts\View\ViewInterface; |
10
|
|
|
|
11
|
|
|
abstract class View implements ViewInterface, Arrayable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $with = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var RepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
protected $repository; |
22
|
|
|
|
23
|
|
|
protected $scopes; |
24
|
|
|
|
25
|
|
|
protected $applies; |
26
|
|
|
|
27
|
|
|
protected $filters; |
28
|
|
|
|
29
|
|
|
protected $type; |
30
|
|
|
|
31
|
|
|
protected $extensions; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->scopes = new Collection(); |
36
|
|
|
$this->applies = new Collection(); |
37
|
|
|
$this->filters = new Collection(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function initialize() |
41
|
|
|
{ |
42
|
|
|
//$this->extensions->initialize(); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return \Illuminate\Support\Collection |
47
|
|
|
*/ |
48
|
|
|
public function getFilters(): Collection |
49
|
|
|
{ |
50
|
|
|
return $this->filters; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $filters |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function setFilters($filters) |
59
|
|
|
{ |
60
|
|
|
$filters = is_array($filters) ? $filters : (array)$filters; |
61
|
|
|
foreach ($filters as $filter) { |
62
|
|
|
$this->addFilter($filter); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function addFilter($filter) |
69
|
|
|
{ |
70
|
|
|
$this->filters->push($filter); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return \Illuminate\Support\Collection |
75
|
|
|
*/ |
76
|
|
|
public function getScopes(): Collection |
77
|
|
|
{ |
78
|
|
|
return $this->scopes; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $scopes |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setScopes($scopes) |
87
|
|
|
{ |
88
|
|
|
$scopes = is_array($scopes) ? $scopes : (array)$scopes; |
89
|
|
|
foreach ($scopes as $scope) { |
90
|
|
|
$this->addScope($scope); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public function addScope($scope) |
98
|
|
|
{ |
99
|
|
|
$this->scopes->push($scope); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function getApplies() |
108
|
|
|
{ |
109
|
|
|
return $this->applies; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $applies |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function setApplies($applies) |
118
|
|
|
{ |
119
|
|
|
$applies = is_array($applies) ? $applies : (array)$applies; |
120
|
|
|
foreach ($applies as $apply) { |
121
|
|
|
$this->addApply($apply); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function addApply($apply) |
128
|
|
|
{ |
129
|
|
|
$this->applies->push($apply); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setRepository(RepositoryInterface $repository) |
135
|
|
|
{ |
136
|
|
|
$this->repository = $repository; |
137
|
|
|
$this->repository->with($this->getWith()); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getRepository() |
143
|
|
|
{ |
144
|
|
|
return $this->repository; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string[] |
149
|
|
|
*/ |
150
|
|
|
public function getWith() |
151
|
|
|
{ |
152
|
|
|
return $this->with; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function with($relations) |
159
|
|
|
{ |
160
|
|
|
$this->with = array_flatten(func_get_args()); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getQuery() |
166
|
|
|
{ |
167
|
|
|
$repository = $this->getRepository(); |
168
|
|
|
|
169
|
|
|
$builder = $repository->getQuery(); |
170
|
|
|
|
171
|
|
|
if ($repository->isRestorable()) { |
172
|
|
|
$builder->withTrashed(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $builder; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Add an "order by" clause to the query. |
180
|
|
|
* |
181
|
|
|
* @param string $column |
182
|
|
|
* @param string $direction |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function orderBy($column, $direction = 'asc') |
187
|
|
|
{ |
188
|
|
|
$this->scopes['orderBy'] = function (Builder $builder) use ($column, $direction) { |
189
|
|
|
$builder->orderBy($column, $direction); |
|
|
|
|
190
|
|
|
}; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function toArray() |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
|
|
'type' => $this->type, |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.