1 <?php namespace CupOfTea\EasyCfg;
2
3 use App;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 use CupOfTea\EasyCfg\Contracts\Provider as ProviderContract;
8
9 trait Configurable
10 {
11
12 13 14 15 16
17 public $_cupoftea_easy_cfg = [];
18
19 20 21 22 23
24 private $_cupoftea_easy_cfg_observing = false;
25
26 27 28 29 30
31 protected $fields = [];
32
33 34 35 36 37 38
39 public function delete()
40 {
41 if (is_a($this, Model::class)) {
42 App::make(ProviderContract::class)->deleteAll(get_class($this), $this->primaryKey);
43 }
44
45 return parent::delete();
46 }
47
48 49 50 51 52 53
54 public function __get($key)
55 {
56 if (isset($this->$key)) {
57 return $this->$key;
58 }
59
60 if (is_a($this, Model::class) && $attribute = $this->getAttribute($key)) {
61 return $attribute;
62 }
63
64 $cfg = App::make(ProviderContract::class);
65
66 if (isset($this->id) || isset($this->primaryKey)) {
67 return $cfg->get($key, get_class($this), isset($this->primaryKey) ? $this->primaryKey : $this->id);
68 } else {
69 return $cfg->get($key, get_class($this));
70 }
71 }
72
73 74 75 76 77 78 79
80 public function __set($key, $value)
81 {
82 if (isset($this->$key)) {
83 $this->$key = $value;
84 return;
85 }
86
87 if (is_a($this, Model::class) && in_array($key, $this->fields)) {
88 $this->setAttribute($key, $value);
89 return;
90 }
91
92 $cfg = App::make(ProviderContract::class);
93
94 if (is_a($this, Model::class) && !config('easycfg.autosave')) {
95 $this->_cupoftea_easy_cfg[$key] = $value;
96
97 if (!$this->_cupoftea_easy_cfg_observing) {
98 $this->saved(function($model) use ($cfg) {
99 foreach($model->_cupoftea_easy_cfg as $key => $value) {
100 if ($value === null) {
101 $cfg->delete($key, get_class($model), $model->primaryKey);
102 } else {
103 $cfg->set($key, $value, get_class($model), $model->primaryKey);
104 }
105 }
106 });
107
108 $this->_cupoftea_easy_cfg_observing = true;
109 }
110 } else {
111 if (isset($this->id) || isset($this->primaryKey)) {
112 return $cfg->set($key, $value, get_class($this), isset($this->primaryKey) ? $this->primaryKey : $this->id);
113 } else {
114 return $cfg->set($key, $value, get_class($this));
115 }
116 }
117 }
118
119 120 121 122 123 124
125 function __unset($key)
126 {
127 $cfg = App::make(ProviderContract::class);
128
129 if (is_a($this, Model::class) && !config('easycfg.autosave')) {
130 $this->_cupoftea_easy_cfg[$key] = null;
131
132 if (!$this->_cupoftea_easy_cfg_observing) {
133 $this->saved(function($model) use ($cfg) {
134 foreach($model->_cupoftea_easy_cfg as $key => $value) {
135 if ($value === null) {
136 $cfg->delete($key, get_class($model), $model->primaryKey);
137 } else {
138 $cfg->set($key, $value, get_class($model), $model->primaryKey);
139 }
140 }
141 });
142
143 $this->_cupoftea_easy_cfg_observing = true;
144 }
145 } else {
146 if (isset($this->id) || isset($this->primaryKey)) {
147 return $cfg->delete($key, get_class($this), isset($this->primaryKey) ? $this->primaryKey : $this->id);
148 } else {
149 return $cfg->delete($key, get_class($this));
150 }
151 }
152 }
153
154 }
155