1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Fields; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Services\CrudFields; |
6
|
|
|
use Akibatech\Crud\Traits\FieldHasUiModifiers; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Field |
11
|
|
|
* |
12
|
|
|
* @package Akibatech\Crud\Fields |
13
|
|
|
*/ |
14
|
|
|
abstract class Field |
15
|
|
|
{ |
16
|
|
|
use FieldHasUiModifiers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const TYPE = 'type'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CrudFields |
25
|
|
|
*/ |
26
|
|
|
protected $fields; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $identifier; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $rules; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Field constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $identifier |
42
|
|
|
* @param array|string $rules |
43
|
|
|
*/ |
44
|
|
|
public function __construct($identifier, $rules = null) |
45
|
|
|
{ |
46
|
|
|
$this->identifier = $identifier; |
47
|
|
|
|
48
|
|
|
if (!is_null($rules)) |
49
|
|
|
{ |
50
|
|
|
$this->withRules($rules); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructs staticly. |
56
|
|
|
* |
57
|
|
|
* @param string $idenfitier |
58
|
|
|
* @param null|string|array $rules |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public static function handle($idenfitier, $rules = null) |
62
|
|
|
{ |
63
|
|
|
return (new static($idenfitier, $rules)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add validation rules to the field. |
69
|
|
|
* |
70
|
|
|
* @param string|array $rules |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function withRules($rules) |
74
|
|
|
{ |
75
|
|
|
if (is_array($rules)) |
76
|
|
|
{ |
77
|
|
|
foreach ($rules as $rule) |
78
|
|
|
{ |
79
|
|
|
$this->addRule($rule); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
if (is_string($rules)) |
85
|
|
|
{ |
86
|
|
|
if (stripos($rules, '|') !== false) |
87
|
|
|
{ |
88
|
|
|
$rules = explode('|', $rules); |
89
|
|
|
|
90
|
|
|
return $this->withRules($rules); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->withRules([$rules]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add a validation rule. |
102
|
|
|
* |
103
|
|
|
* @param string $rule |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
public function addRule($rule) |
107
|
|
|
{ |
108
|
|
|
$this->rules[] = $rule; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param CrudFields $fields |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
public function setFields(CrudFields $fields) |
118
|
|
|
{ |
119
|
|
|
$this->fields = $fields; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the field identifier. |
126
|
|
|
* |
127
|
|
|
* @param void |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getIdentifier() |
131
|
|
|
{ |
132
|
|
|
return $this->identifier; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Render the field form. |
137
|
|
|
* |
138
|
|
|
* @param void |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function form() |
142
|
|
|
{ |
143
|
|
|
return $this->getForm()->render(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the form view. |
148
|
|
|
* |
149
|
|
|
* @param void |
150
|
|
|
* @return View |
151
|
|
|
*/ |
152
|
|
|
protected function getForm() |
153
|
|
|
{ |
154
|
|
|
return view()->make($this->getViewName())->with($this->getViewBaseVariables()); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the field view name. |
159
|
|
|
* |
160
|
|
|
* @param void |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
abstract public function getViewName(); |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns additionnal variables to the views. |
167
|
|
|
* |
168
|
|
|
* @param void |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
protected function getViewVariables() |
172
|
|
|
{ |
173
|
|
|
return []; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns all base variables for the view. |
178
|
|
|
* |
179
|
|
|
* @param void |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
protected function getViewBaseVariables() |
183
|
|
|
{ |
184
|
|
|
$base_variables = [ |
185
|
|
|
'field' => $this, |
186
|
|
|
'has_error' => $this->hasError(), |
187
|
|
|
'error' => $this->getError(), |
188
|
|
|
'placeholder' => $this->getPlaceholder(), |
189
|
|
|
'help' => $this->getHelp(), |
190
|
|
|
'has_old' => $this->hasOld(), |
191
|
|
|
'old' => $this->getOld(), |
192
|
|
|
'label' => $this->getLabel(), |
193
|
|
|
'name' => $this->identifier, |
194
|
|
|
'id' => 'field-' . $this->identifier, |
195
|
|
|
'value' => $this->getValue() |
196
|
|
|
]; |
197
|
|
|
|
198
|
|
|
return array_merge($base_variables, $this->getViewVariables()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Checks if the field has an error. |
203
|
|
|
* |
204
|
|
|
* @param void |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function hasError() |
208
|
|
|
{ |
209
|
|
|
return $this->fields->getErrors()->has($this->identifier); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns the error. |
214
|
|
|
* |
215
|
|
|
* @param void |
216
|
|
|
* @return null|string |
217
|
|
|
*/ |
218
|
|
|
public function getError() |
219
|
|
|
{ |
220
|
|
|
if ($this->hasError()) |
221
|
|
|
{ |
222
|
|
|
return $this->fields->getErrors()->first($this->identifier); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Checks if the field has a previous value. |
230
|
|
|
* |
231
|
|
|
* @param void |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function hasOld() |
235
|
|
|
{ |
236
|
|
|
return $this->fields->getOldInput()->has($this->identifier); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Returns the old value. |
241
|
|
|
* |
242
|
|
|
* @param void |
243
|
|
|
* @return string|null |
244
|
|
|
*/ |
245
|
|
|
public function getOld() |
246
|
|
|
{ |
247
|
|
|
if ($this->hasOld()) |
248
|
|
|
{ |
249
|
|
|
return $this->fields->getOldInput()->first($this->identifier); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return null; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the field value. |
257
|
|
|
* |
258
|
|
|
* @param void |
259
|
|
|
* @return mixed |
260
|
|
|
*/ |
261
|
|
|
public function getValue() |
262
|
|
|
{ |
263
|
|
|
if ($this->fields->getEntry()) |
264
|
|
|
{ |
265
|
|
|
return $this->fields->getEntry()->getModel()->getAttributeValue($this->identifier); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return null; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get the value to be displayed on a table. |
273
|
|
|
* |
274
|
|
|
* @param void |
275
|
|
|
* @return mixed |
276
|
|
|
*/ |
277
|
|
|
public function getTableValue() |
278
|
|
|
{ |
279
|
|
|
return $this->getValue(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set a new value to the model. |
284
|
|
|
* |
285
|
|
|
* @param mixed $value |
286
|
|
|
* @return self |
287
|
|
|
*/ |
288
|
|
|
public function newValue($value) |
289
|
|
|
{ |
290
|
|
|
$this->fields->getEntry()->getModel()->setAttribute($this->identifier, $value); |
|
|
|
|
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param void |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
public function getRules() |
300
|
|
|
{ |
301
|
|
|
return $this->rules; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: