1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Xetaravel\Models\Presenters\SettingPresenter; |
5
|
|
|
|
6
|
|
|
class Setting extends Model |
7
|
|
|
{ |
8
|
|
|
use SettingPresenter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* All type with their labels. (Used in admin panel for radio buttons) |
12
|
|
|
*/ |
13
|
|
|
public const TYPES = [ |
14
|
|
|
'value_int' => 'Value Integer', |
15
|
|
|
'value_str' => 'Value String', |
16
|
|
|
'value_bool' => 'Value Boolean' |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The attributes that are mass assignable. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fillable = [ |
25
|
|
|
'name', |
26
|
|
|
'value_int', |
27
|
|
|
'value_str', |
28
|
|
|
'value_bool', |
29
|
|
|
'value', |
30
|
|
|
'description', |
31
|
|
|
'last_updated_user_id', |
32
|
|
|
'is_deletable' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The accessors to append to the model's array form. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $appends = [ |
41
|
|
|
'value', |
42
|
|
|
'type' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Function to set the value regarding the type to the model and return it. |
47
|
|
|
* |
48
|
|
|
* @param string $value The value to assign. |
49
|
|
|
* @param string $type The type of the value. |
50
|
|
|
* @param Setting $model The model where the value will be assigned. |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
protected static function castValue(string $value, string $type, Setting $model) |
55
|
|
|
{ |
56
|
|
|
switch ($type) { |
57
|
|
|
case "value_int": |
58
|
|
|
$model->value_int = intval($value); |
|
|
|
|
59
|
|
|
$model->value_bool = null; |
|
|
|
|
60
|
|
|
$model->value_str = null; |
|
|
|
|
61
|
|
|
break; |
62
|
|
|
|
63
|
|
|
case "value_bool": |
64
|
|
|
$model->value_bool = boolval($value); |
65
|
|
|
$model->value_int = null; |
66
|
|
|
$model->value_str = null; |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case "value_str": |
70
|
|
|
$model->value_str = $value; |
71
|
|
|
$model->value_int = null; |
72
|
|
|
$model->value_bool = null; |
73
|
|
|
break; |
74
|
|
|
|
75
|
|
|
default: |
76
|
|
|
$model->value_str = $value; |
77
|
|
|
$model->value_int = null; |
78
|
|
|
$model->value_bool = null; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
return $model; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.