1
|
|
|
<?php namespace Arcanesoft\Settings\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Support\Bases\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Setting |
7
|
|
|
* |
8
|
|
|
* @package Arcanesoft\Settings\Models |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @property int id |
12
|
|
|
* @property string domain |
13
|
|
|
* @property string key |
14
|
|
|
* @property mixed value |
15
|
|
|
* @property mixed type |
16
|
|
|
* @property mixed casted_value |
17
|
|
|
* @property \Carbon\Carbon created_at |
18
|
|
|
* @property \Carbon\Carbon updated_at |
19
|
|
|
*/ |
20
|
|
|
class Setting extends Model |
21
|
|
|
{ |
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Properties |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* The attributes that are mass assignable. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $fillable = ['domain', 'key', 'value']; |
32
|
|
|
|
33
|
|
|
/* ------------------------------------------------------------------------------------------------ |
34
|
|
|
| Constructor |
35
|
|
|
| ------------------------------------------------------------------------------------------------ |
36
|
|
|
*/ |
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
30 |
|
public function __construct(array $attributes = []) |
41
|
|
|
{ |
42
|
30 |
|
$configs = config('arcanesoft.settings.database'); |
43
|
|
|
|
44
|
30 |
|
$this->setConnection(array_get($configs, 'connection', null)); |
45
|
30 |
|
$this->setTable(array_get($configs, 'table', 'settings')); |
46
|
|
|
|
47
|
30 |
|
parent::__construct($attributes); |
48
|
30 |
|
} |
49
|
|
|
|
50
|
|
|
/* ------------------------------------------------------------------------------------------------ |
51
|
|
|
| Getters & Setters |
52
|
|
|
| ------------------------------------------------------------------------------------------------ |
53
|
|
|
*/ |
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
18 |
|
protected function getCastType($key) |
58
|
|
|
{ |
59
|
18 |
|
return $this->attributes['type']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the type attributes. |
64
|
|
|
* |
65
|
|
|
* @param string $type |
66
|
|
|
*/ |
67
|
18 |
|
public function setTypeAttribute($type) |
68
|
|
|
{ |
69
|
18 |
|
$this->casts['value'] = $type; |
70
|
18 |
|
$this->attributes['type'] = $type; |
71
|
18 |
|
} |
72
|
|
|
|
73
|
18 |
|
public function getCastedValueAttribute() |
74
|
|
|
{ |
75
|
18 |
|
$this->casts['value'] = $this->type; |
76
|
|
|
|
77
|
18 |
|
return $this->castAttribute('value', $this->attributes['value']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* ------------------------------------------------------------------------------------------------ |
81
|
|
|
| CRUD Functions |
82
|
|
|
| ------------------------------------------------------------------------------------------------ |
83
|
|
|
*/ |
84
|
|
|
/** |
85
|
|
|
* Create a setting. |
86
|
|
|
* |
87
|
|
|
* @param string $domain |
88
|
|
|
* @param string $key |
89
|
|
|
* @param mixed $value |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
18 |
|
public function createOne($domain, $key, $value) |
94
|
|
|
{ |
95
|
18 |
|
$setting = new self; |
96
|
|
|
|
97
|
18 |
|
$setting->domain = $domain; |
98
|
18 |
|
$setting->key = $key; |
99
|
18 |
|
$setting->updateValue($value); |
100
|
|
|
|
101
|
18 |
|
return $setting->save(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update the setting value. |
106
|
|
|
* |
107
|
|
|
* @param mixed $value |
108
|
|
|
*/ |
109
|
18 |
|
public function updateValue($value) |
110
|
|
|
{ |
111
|
18 |
|
$this->type = gettype($value); |
112
|
18 |
|
$this->value = $value; |
113
|
18 |
|
} |
114
|
|
|
} |
115
|
|
|
|