1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Fields; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Services\CrudFields; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Field |
9
|
|
|
* |
10
|
|
|
* @package Akibatech\Crud\Fields |
11
|
|
|
*/ |
12
|
|
|
abstract class Field |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var CrudFields |
16
|
|
|
*/ |
17
|
|
|
protected $fields; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $identifier; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $label; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $placeholder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $rules; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Field constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $identifier |
43
|
|
|
* @param array|string $rules |
44
|
|
|
*/ |
45
|
|
|
public function __construct($identifier, $rules = null) |
46
|
|
|
{ |
47
|
|
|
$this->identifier = $identifier; |
48
|
|
|
|
49
|
|
|
if (!is_null($rules)) |
50
|
|
|
{ |
51
|
|
|
$this->withRules($rules); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructs staticly. |
57
|
|
|
* |
58
|
|
|
* @param string $idenfitier |
59
|
|
|
* @param null|string|array $rules |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
|
|
public static function handle($idenfitier, $rules = null) |
63
|
|
|
{ |
64
|
|
|
return (new static($idenfitier, $rules)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add validation rules to the field. |
70
|
|
|
* |
71
|
|
|
* @param string|array $rules |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function withRules($rules) |
75
|
|
|
{ |
76
|
|
|
if (is_array($rules)) |
77
|
|
|
{ |
78
|
|
|
foreach ($rules as $rule) |
79
|
|
|
{ |
80
|
|
|
$this->addRule($rule); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
else |
84
|
|
|
{ |
85
|
|
|
if (is_string($rules)) |
86
|
|
|
{ |
87
|
|
|
if (stripos($rules, '|') !== false) |
88
|
|
|
{ |
89
|
|
|
$rules = explode('|', $rules); |
90
|
|
|
|
91
|
|
|
return $this->withRules($rules); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->withRules([$rules]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add a validation rule. |
103
|
|
|
* |
104
|
|
|
* @param string $rule |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public function addRule($rule) |
108
|
|
|
{ |
109
|
|
|
$this->rules[] = $rule; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param CrudFields $fields |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function setFields(CrudFields $fields) |
119
|
|
|
{ |
120
|
|
|
$this->fields = $fields; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the field identifier. |
127
|
|
|
* |
128
|
|
|
* @param void |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getIdentifier() |
132
|
|
|
{ |
133
|
|
|
return $this->identifier; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set a custom label for the field. |
138
|
|
|
* |
139
|
|
|
* @param string $name |
140
|
|
|
* @return self |
141
|
|
|
*/ |
142
|
|
|
public function withLabel($name) |
143
|
|
|
{ |
144
|
|
|
$this->label = $name; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Defines a placeholder for the field. |
151
|
|
|
* |
152
|
|
|
* @param string $placeholder |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
|
|
public function withPlaceholder($placeholder) |
156
|
|
|
{ |
157
|
|
|
$this->placeholder = $placeholder; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Appends an help message to the input. |
164
|
|
|
* |
165
|
|
|
* @param string $help |
166
|
|
|
* @return self |
167
|
|
|
*/ |
168
|
|
|
public function withHelp($help) |
169
|
|
|
{ |
170
|
|
|
$this->help = $help; |
|
|
|
|
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the field's form. |
177
|
|
|
* |
178
|
|
|
* @param void |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function form() |
182
|
|
|
{ |
183
|
|
|
$view = view()->make($this->getViewName())->with([ |
|
|
|
|
184
|
|
|
'field' => $this, |
185
|
|
|
'has_error' => $this->hasError(), |
186
|
|
|
'error' => $this->getError(), |
187
|
|
|
'placeholder' => $this->getPlaceholder(), |
188
|
|
|
'help' => $this->getHelp(), |
189
|
|
|
'has_old' => $this->hasOld(), |
190
|
|
|
'old' => $this->getOld(), |
191
|
|
|
'label' => $this->getLabel(), |
192
|
|
|
'name' => $this->identifier, |
193
|
|
|
'id' => 'field-' . $this->identifier, |
194
|
|
|
'value' => $this->getValue() |
195
|
|
|
]); |
196
|
|
|
|
197
|
|
|
return $view->render(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the field view name. |
202
|
|
|
* |
203
|
|
|
* @param void |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
abstract public function getViewName(); |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Checks if the field has an error. |
210
|
|
|
* |
211
|
|
|
* @param void |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function hasError() |
215
|
|
|
{ |
216
|
|
|
return $this->fields->getErrors()->has($this->identifier); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Returns the error. |
221
|
|
|
* |
222
|
|
|
* @param void |
223
|
|
|
* @return null|string |
224
|
|
|
*/ |
225
|
|
|
public function getError() |
226
|
|
|
{ |
227
|
|
|
if ($this->hasError()) |
228
|
|
|
{ |
229
|
|
|
return $this->fields->getErrors()->first($this->identifier); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return null; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Returns the field's placeholder. |
237
|
|
|
* |
238
|
|
|
* @param void |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getPlaceholder() |
242
|
|
|
{ |
243
|
|
|
if (empty($this->placeholder)) |
244
|
|
|
{ |
245
|
|
|
return null; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $this->placeholder; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns the field's help. |
253
|
|
|
* |
254
|
|
|
* @param void |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getHelp() |
258
|
|
|
{ |
259
|
|
|
if (empty($this->help)) |
260
|
|
|
{ |
261
|
|
|
return null; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $this->help; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Checks if the field has a previous value. |
269
|
|
|
* |
270
|
|
|
* @param void |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
public function hasOld() |
274
|
|
|
{ |
275
|
|
|
return $this->fields->getOldInput()->has($this->identifier); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns the old value. |
280
|
|
|
* |
281
|
|
|
* @param void |
282
|
|
|
* @return string|null |
283
|
|
|
*/ |
284
|
|
|
public function getOld() |
285
|
|
|
{ |
286
|
|
|
if ($this->hasOld()) |
287
|
|
|
{ |
288
|
|
|
return $this->fields->getOldInput()->first($this->identifier); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return null; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns the field's label. |
296
|
|
|
* |
297
|
|
|
* @param void |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
public function getLabel() |
301
|
|
|
{ |
302
|
|
|
if (empty($this->label)) |
303
|
|
|
{ |
304
|
|
|
return title_case($this->identifier); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $this->label; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get the field value. |
312
|
|
|
* |
313
|
|
|
* @param void |
314
|
|
|
* @return mixed |
315
|
|
|
*/ |
316
|
|
|
public function getValue() |
317
|
|
|
{ |
318
|
|
|
return $this->fields->getEntry()->getModel()->getAttributeValue($this->identifier); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Set a new value to the model. |
323
|
|
|
* |
324
|
|
|
* @param mixed $value |
325
|
|
|
* @return self |
326
|
|
|
*/ |
327
|
|
|
public function newValue($value) |
328
|
|
|
{ |
329
|
|
|
$this->fields->getEntry()->getModel()->setAttribute($this->identifier, $value); |
|
|
|
|
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @param void |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
|
|
public function getRules() |
339
|
|
|
{ |
340
|
|
|
return $this->rules; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: