|
1
|
|
|
<?php namespace Mascame\Artificer\Model; |
|
2
|
|
|
|
|
3
|
|
|
use View; |
|
4
|
|
|
use Route; |
|
5
|
|
|
use \Illuminate\Support\Str as Str; |
|
6
|
|
|
use Mascame\Artificer\Options\AdminOption; |
|
7
|
|
|
use Mascame\Artificer\Options\ModelOption; |
|
8
|
|
|
|
|
9
|
|
|
// Todo: get column type http://stackoverflow.com/questions/18562684/how-to-get-database-field-type-in-laravel |
|
10
|
|
|
class Model |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var ModelSchema |
|
15
|
|
|
*/ |
|
16
|
|
|
public $schema; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
public $models; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
public $columns; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public $model; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public $class; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var |
|
40
|
|
|
*/ |
|
41
|
|
|
public $name; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $keyname; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var |
|
50
|
|
|
*/ |
|
51
|
|
|
public $table; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var |
|
55
|
|
|
*/ |
|
56
|
|
|
public $fillable; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array|mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public $options = array(); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array|mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public $relations = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var |
|
70
|
|
|
*/ |
|
71
|
|
|
public static $current = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ModelSchema $schema |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(ModelSchema $schema) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->schema = $schema; |
|
79
|
|
|
$this->relations = new ModelRelation(); |
|
80
|
|
|
|
|
81
|
|
|
$this->prepareCurrentModel(); |
|
82
|
|
|
$this->share(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
public function share() |
|
87
|
|
|
{ |
|
88
|
|
|
View::share('tables', $this->schema->tables); |
|
89
|
|
|
View::share('models', $this->getCurrentModelsData()); |
|
90
|
|
|
View::share('model', $this->getCurrentModelData()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getCurrentModelsData() |
|
97
|
|
|
{ |
|
98
|
|
|
foreach ($this->schema->models as $modelName => $model) { |
|
99
|
|
|
$this->schema->models[$modelName]['options'] = $this->getOptions($modelName); |
|
100
|
|
|
$this->schema->models[$modelName]['hidden'] = $this->isHidden($modelName); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->schema->models; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $modelName |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function isHidden($modelName) |
|
111
|
|
|
{ |
|
112
|
|
|
return (in_array($modelName, AdminOption::get('models.hidden'))) ? true : false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function hasGuarded() |
|
119
|
|
|
{ |
|
120
|
|
|
return (isset($this->options['guarded']) && !empty($this->options['guarded'])) ? true : false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function hasFillable() |
|
127
|
|
|
{ |
|
128
|
|
|
return (isset($this->options['fillable']) && !empty($this->options['fillable'])) ? true : false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return int|null|string |
|
133
|
|
|
*/ |
|
134
|
|
|
private function getCurrentModelName() |
|
135
|
|
|
{ |
|
136
|
|
|
if ($this->name) { |
|
137
|
|
|
return $this->name; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
foreach ($this->schema->models as $modelName => $model) { |
|
141
|
|
|
if ($this->isCurrent($modelName)) { |
|
142
|
|
|
$this->setCurrent($modelName); |
|
143
|
|
|
|
|
144
|
|
|
return $this->name = $modelName; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function prepareCurrentModel() |
|
152
|
|
|
{ |
|
153
|
|
|
if (!Str::startsWith(Route::currentRouteName(), 'admin.model.')) { |
|
154
|
|
|
return null; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->name = $this->getCurrentModelName(); |
|
158
|
|
|
|
|
159
|
|
|
$this->class = $this->schema->getClass($this->name); |
|
160
|
|
|
$this->model = $this->schema->getInstance($this->name); |
|
|
|
|
|
|
161
|
|
|
$this->table = $this->model->getTable(); |
|
162
|
|
|
$this->columns = $this->schema->getColumns($this->table); |
|
163
|
|
|
$this->fillable = $this->model->getFillable(); |
|
164
|
|
|
$this->options = $this->getOptions(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
private function getCurrentModelData() |
|
171
|
|
|
{ |
|
172
|
|
|
return array( |
|
173
|
|
|
'class' => $this->class, |
|
174
|
|
|
'name' => $this->getCurrentModelName(), |
|
175
|
|
|
'route' => $this->getRouteName(), |
|
176
|
|
|
'table' => $this->table, |
|
177
|
|
|
'columns' => $this->schema->columns, |
|
178
|
|
|
'fillable' => $this->fillable, |
|
179
|
|
|
'hidden' => $this->isHidden($this->name), |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param $model |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function isCurrent($modelName) |
|
188
|
|
|
{ |
|
189
|
|
|
$slug = Route::current()->parameter('slug'); |
|
190
|
|
|
|
|
191
|
|
|
return (isset($this->schema->models[$modelName]['route']) && $this->schema->models[$modelName]['route'] == $slug); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return null |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function getCurrent() |
|
198
|
|
|
{ |
|
199
|
|
|
return (isset(self::$current)) ? self::$current : null; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public static function getCurrentClass() |
|
206
|
|
|
{ |
|
207
|
|
|
return '\\' . self::$current; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param null $model |
|
212
|
|
|
* @return null |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getRouteName($model = null) |
|
215
|
|
|
{ |
|
216
|
|
|
if ($model) return $this->schema->models[$model]['route']; |
|
217
|
|
|
|
|
218
|
|
|
return (isset($this->schema->models[self::$current]['route'])) ? $this->schema->models[self::$current]['route'] : null; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $modelName |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function setCurrent($modelName) |
|
225
|
|
|
{ |
|
226
|
|
|
self::$current = $modelName; |
|
227
|
|
|
ModelOption::set('current', $modelName); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param null $model |
|
232
|
|
|
* @return mixed |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getOptions($model = null) |
|
235
|
|
|
{ |
|
236
|
|
|
return ModelOption::model(($model) ?: $this->name); |
|
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param $key |
|
241
|
|
|
* @param null $model |
|
242
|
|
|
* @return mixed |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getOption($key, $default = null, $model = null) |
|
245
|
|
|
{ |
|
246
|
|
|
return (ModelOption::has($key, $model)) ? ModelOption::get($key, $model) : $default; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return array|mixed |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getRelations() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->relations->get(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.