1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Attributes; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\HtmlString; |
6
|
|
|
use LaravelFlare\Flare\Admin\Models\ModelAdmin; |
7
|
|
|
|
8
|
|
|
class BaseAttribute |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Attribute Type Constant. |
12
|
|
|
*/ |
13
|
|
|
const ATTRIBUTE_TYPE = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* View Path for this Attribute Type |
17
|
|
|
* Defaults to flare::admin.attributes which outputs |
18
|
|
|
* a warning callout notifying the user that the field |
19
|
|
|
* view does not yet exist. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $viewpath = 'flare::admin.attributes'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Attribute. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $attribute; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Field. |
34
|
|
|
* |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
protected $field; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Value. |
41
|
|
|
* |
42
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
43
|
|
|
*/ |
44
|
|
|
protected $value; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Eloquent Model. |
48
|
|
|
* |
49
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
50
|
|
|
*/ |
51
|
|
|
protected $model; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Model Manager. |
55
|
|
|
* |
56
|
|
|
* @var \LaravelFlare\Flare\Admin\Models\ModelAdmin |
57
|
|
|
*/ |
58
|
|
|
protected $modelManager; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* __construct. |
62
|
|
|
* |
63
|
|
|
* @param string $attribute |
64
|
|
|
* @param string $field |
65
|
|
|
* @param string $modelManager |
66
|
|
|
*/ |
67
|
|
|
public function __construct($attribute, $field, $value, $modelManager = null) |
68
|
|
|
{ |
69
|
|
|
$this->attribute = $attribute; |
70
|
|
|
$this->field = $field; |
71
|
|
|
$this->value = $value; |
72
|
|
|
$this->modelManager = $modelManager; |
|
|
|
|
73
|
|
|
|
74
|
|
|
if ($modelManager instanceof ModelAdmin) { |
75
|
|
|
$this->model = $modelManager->model(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the View to Render as an HTMLString |
81
|
|
|
* |
82
|
|
|
* @param boolean $view |
83
|
|
|
* |
84
|
|
|
* @return /Illuminate/Support/String |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function render($view = false) |
87
|
|
|
{ |
88
|
|
|
if (method_exists($this, $method = 'render'.ucfirst($view))) { |
89
|
|
|
return new HtmlString( |
90
|
|
|
call_user_func_array([$this, $method], []) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Renders the Add (Create) Field View. |
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\View\View |
99
|
|
|
*/ |
100
|
|
|
public function renderAdd() |
101
|
|
|
{ |
102
|
|
|
return view($this->viewpath.'.add', $this->viewData()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Renders the Edit (Update) Field View. |
107
|
|
|
* |
108
|
|
|
* @return \Illuminate\View\View |
109
|
|
|
*/ |
110
|
|
|
public function renderEdit() |
111
|
|
|
{ |
112
|
|
|
return view($this->viewpath.'.edit', $this->viewData()); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Renders the Clone (Update) Field View. |
117
|
|
|
* |
118
|
|
|
* @return \Illuminate\View\View |
119
|
|
|
*/ |
120
|
|
|
public function renderClone() |
121
|
|
|
{ |
122
|
|
|
if (view()->exists($this->viewpath.'.clone')) { |
|
|
|
|
123
|
|
|
view($this->viewpath.'.clone', $this->viewData()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return view($this->viewpath.'.edit', $this->viewData()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Renders the Viewable Field View. |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\View\View |
133
|
|
|
*/ |
134
|
|
|
public function renderView() |
135
|
|
|
{ |
136
|
|
|
return view($this->viewpath.'.view', $this->viewData()); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Getter for Attribute. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getAttribute() |
145
|
|
|
{ |
146
|
|
|
return $this->attribute; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Getter for Field. |
151
|
|
|
* |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
public function getField() |
155
|
|
|
{ |
156
|
|
|
$this->getFieldOptions(); |
157
|
|
|
|
158
|
|
|
return $this->field; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the current value |
163
|
|
|
* |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function getValue() |
167
|
|
|
{ |
168
|
|
|
if ($this->modelManager) { |
169
|
|
|
return $this->modelManager->getAttribute($this->attribute); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($this->model) { |
173
|
|
|
return $this->model->getAttribute($this->attribute); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->value; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the old or current value |
181
|
|
|
* |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
|
public function getOldValue() |
185
|
|
|
{ |
186
|
|
|
return old($this->attribute, $this->getValue()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Gets Field Options if they are defined. |
191
|
|
|
*/ |
192
|
|
|
public function getFieldOptions() |
193
|
|
|
{ |
194
|
|
|
if (method_exists($this->getModelManager(), $method = camel_case('get_'.$this->getAttribute().'_options'))) { |
195
|
|
|
// First check for a method of options based on getAttributeNameOptions() |
196
|
|
|
$this->field['options'] = $this->getModelManager()->$method(); |
197
|
|
|
} elseif (isset($this->field['options']) && is_string($this->field['options']) && method_exists($this->getModelManager(), $method = camel_case('get_'.$this->field['options'].'_options'))) { |
198
|
|
|
// Check if Options is a string and if so, check for a method |
199
|
|
|
// of options based on getDefinedOptions() |
200
|
|
|
$this->field['options'] = $this->getModelManager()->$method(); |
201
|
|
|
} elseif (isset($this->field['options']) && is_string($this->field['options'])) { |
202
|
|
|
// Otherwise, if the options have been provided as a string |
203
|
|
|
// we will assume that the available options are comma |
204
|
|
|
// delimited and explode and return that array. |
205
|
|
|
$this->field['options'] = explode(',', $this->field['options']); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Accessor for Model. |
211
|
|
|
* |
212
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
213
|
|
|
*/ |
214
|
|
|
public function getModel() |
215
|
|
|
{ |
216
|
|
|
return $this->model; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Accessor for Model. |
221
|
|
|
* |
222
|
|
|
* @var \LaravelFlare\Flare\Admin\Models\ModelAdmin |
223
|
|
|
*/ |
224
|
|
|
public function getModelManager() |
225
|
|
|
{ |
226
|
|
|
return $this->modelManager; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Acessor for Attribute Type converted to Title Case. |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function getAttributeType() |
235
|
|
|
{ |
236
|
|
|
return title_case(isset($this->getField()['type']) ? $this->getField()['type'] : self::ATTRIBUTE_TYPE); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Acessor for Attribute Title converted to Title Case with Spaces. |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getAttributeTitle() |
245
|
|
|
{ |
246
|
|
|
return str_replace('_', ' ', title_case($this->getAttribute())); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns all of the accessible data for the Attirbute View |
251
|
|
|
* |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
protected function viewData() |
255
|
|
|
{ |
256
|
|
|
return [ |
257
|
|
|
'field' => $this->getField(), |
258
|
|
|
'model' => $this->getModel(), |
259
|
|
|
'attribute' => $this->getAttribute(), |
260
|
|
|
'value' => $this->getValue(), |
261
|
|
|
'oldValue' => $this->getOldValue(), |
262
|
|
|
'modelManager' => $this->getModelManager(), |
263
|
|
|
'attributeType' => $this->getAttributeType(), |
264
|
|
|
'attributeTitle' => $this->getAttributeTitle(), |
265
|
|
|
]; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.