1 | <?php namespace Mascame\Artificer\Model; |
||
11 | class Model |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var ModelSchema |
||
16 | */ |
||
17 | public $schema; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | public $models; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | public $columns; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Database\Eloquent\Model |
||
31 | */ |
||
32 | public $model; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | public $class; |
||
38 | |||
39 | /** |
||
40 | * @var |
||
41 | */ |
||
42 | public $name; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | public $keyname; |
||
48 | |||
49 | /** |
||
50 | * @var |
||
51 | */ |
||
52 | public $table; |
||
53 | |||
54 | /** |
||
55 | * @var |
||
56 | */ |
||
57 | public $fillable; |
||
58 | |||
59 | /** |
||
60 | * @var array|mixed |
||
61 | */ |
||
62 | protected $options = []; |
||
63 | protected $defaultOptions = null; |
||
64 | |||
65 | /** |
||
66 | * @var array|mixed |
||
67 | */ |
||
68 | public $relations = []; |
||
69 | |||
70 | /** |
||
71 | * @var |
||
72 | */ |
||
73 | public static $current = null; |
||
74 | |||
75 | /** |
||
76 | * @param ModelSchema $schema |
||
77 | */ |
||
78 | public function __construct(ModelSchema $schema) |
||
79 | { |
||
80 | $this->schema = $schema; |
||
81 | $this->relations = new ModelRelation(); |
||
82 | |||
83 | if (Str::startsWith(Route::currentRouteName(), 'admin.model.')) { |
||
84 | $this->prepareCurrentModel(); |
||
85 | } |
||
86 | |||
87 | $this->share(); |
||
88 | } |
||
89 | |||
90 | |||
91 | public function share() |
||
92 | { |
||
93 | View::share('tables', $this->schema->tables); |
||
94 | View::share('models', $this->getCurrentModelsData()); |
||
95 | View::share('model', $this->getCurrentModelData()); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | private function getCurrentModelsData() |
||
102 | { |
||
103 | foreach ($this->schema->models as $modelName => $model) { |
||
104 | $this->schema->models[$modelName]['options'] = $this->getOptions($modelName); |
||
105 | $this->schema->models[$modelName]['hidden'] = $this->isHidden($modelName); |
||
106 | } |
||
107 | |||
108 | return $this->schema->models; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param $modelName |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function isHidden($modelName) |
||
116 | { |
||
117 | return (in_array($modelName, AdminOption::get('model.hidden'))) ? true : false; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function hasGuarded() |
||
124 | { |
||
125 | return ! empty($this->getOption('guarded', [])); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function hasFillable() |
||
132 | { |
||
133 | return ! empty($this->getOption('fillable', [])); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return int|null|string |
||
138 | */ |
||
139 | private function getCurrentModelName() |
||
140 | { |
||
141 | if ($this->name) return $this->name; |
||
142 | |||
143 | foreach ($this->schema->models as $modelName => $model) { |
||
144 | if ($this->isCurrent($modelName)) { |
||
145 | $this->setCurrent($modelName); |
||
146 | |||
147 | return $this->name = $modelName; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | return null; |
||
152 | } |
||
153 | |||
154 | public function prepareCurrentModel() |
||
155 | { |
||
156 | $this->name = $this->getCurrentModelName(); |
||
157 | $this->class = $this->schema->getClass($this->name); |
||
158 | $this->model = $this->schema->getInstance($this->name); |
||
|
|||
159 | $this->table = $this->model->getTable(); |
||
160 | $this->columns = $this->schema->getColumns($this->table); |
||
161 | $this->fillable = $this->model->getFillable(); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return array |
||
166 | */ |
||
167 | private function getCurrentModelData() |
||
179 | |||
180 | /** |
||
181 | * @param $model |
||
182 | * @return bool |
||
183 | */ |
||
184 | protected function isCurrent($modelName) |
||
190 | |||
191 | /** |
||
192 | * @return Model |
||
193 | */ |
||
194 | public static function getCurrent() |
||
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | public static function getCurrentClass() |
||
206 | |||
207 | /** |
||
208 | * @param null $model |
||
209 | * @return null |
||
210 | */ |
||
211 | public function getRouteName($model = null) |
||
217 | |||
218 | /** |
||
219 | * @param $modelName |
||
220 | */ |
||
221 | protected function setCurrent($modelName) |
||
222 | { |
||
223 | self::$current = $modelName; |
||
224 | // ModelOption::set('current', $modelName); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param null $model |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public function getOptions($model = null) |
||
239 | |||
240 | public function getDefaultOptions() |
||
241 | { |
||
242 | if ($this->defaultOptions) return $this->defaultOptions; |
||
243 | |||
244 | return $this->defaultOptions = config('admin.model.default_model'); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param $key |
||
249 | * @param null $model |
||
250 | * @return mixed |
||
251 | */ |
||
252 | public function getOption($key, $default = null, $model = null) |
||
259 | |||
260 | /** |
||
261 | * @return array|mixed |
||
262 | */ |
||
263 | public function getRelations() |
||
267 | |||
268 | } |
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.