X-Laravel /
model-settings-bag
| 1 | <?php |
||
| 2 | |||
| 3 | namespace XLaravel\ModelSettingsBag; |
||
| 4 | |||
| 5 | use Illuminate\Support\Arr; |
||
| 6 | use XLaravel\ModelSettingsBag\Exceptions\Exception; |
||
| 7 | |||
| 8 | trait HasSettingsBag |
||
| 9 | { |
||
| 10 | # Boot the HasSettingsBag trait. |
||
| 11 | public static function bootHasSettingsBag(): void |
||
| 12 | { |
||
| 13 | self::creating(function ($model) { |
||
| 14 | if (!$model->settings) { |
||
| 15 | $model->settings = $model->getDefaultSettings() ?: null; |
||
| 16 | } |
||
| 17 | }); |
||
| 18 | |||
| 19 | self::saving(function ($model) { |
||
| 20 | if ($model->settings && property_exists($model, 'allowedSettings') && is_array($model->allowedSettings)) { |
||
| 21 | $model->settings = Arr::only($model->settings, $model->allowedSettings); |
||
| 22 | } |
||
| 23 | }); |
||
| 24 | } |
||
| 25 | |||
| 26 | # Get the model's default settings. |
||
| 27 | public function getDefaultSettings(): array |
||
| 28 | { |
||
| 29 | return (isset($this->defaultSettings) && is_array($this->defaultSettings)) |
||
| 30 | ? $this->defaultSettings |
||
| 31 | : []; |
||
| 32 | } |
||
| 33 | |||
| 34 | # Get the settings attribute. |
||
| 35 | public function getSettingsAttribute($settings) |
||
| 36 | { |
||
| 37 | return json_decode($settings, true); |
||
| 38 | } |
||
| 39 | |||
| 40 | # Set the settings attribute. |
||
| 41 | public function setSettingsAttribute($settings): void |
||
| 42 | { |
||
| 43 | $this->attributes['settings'] = json_encode($settings); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 44 | } |
||
| 45 | |||
| 46 | # The model's settings. |
||
| 47 | public function settings(string $settingName = null): SettingsBag |
||
| 48 | { |
||
| 49 | if ($settingName === null) { |
||
| 50 | return new SettingsBag($this); |
||
| 51 | } |
||
| 52 | |||
| 53 | $settingRelation = $settingName . 'SettingsBag'; |
||
| 54 | if (!method_exists($this, $settingRelation) || !in_array(__TRAIT__, class_uses($this->{$settingRelation}))) { |
||
| 55 | throw new Exception($settingName . ' setting not available on ' . get_class($this) . ' model'); |
||
| 56 | } |
||
| 57 | |||
| 58 | $settingModel = $this->{$settingRelation}; |
||
| 59 | if ($settingModel === null) { |
||
| 60 | $settingModel = $this->{$settingRelation}()->getRelated(); |
||
| 61 | $settingModel->{$this->getForeignKey()} = $this->{$this->getKeyName()}; |
||
| 62 | } |
||
| 63 | |||
| 64 | return new SettingsBag($settingModel); |
||
| 65 | } |
||
| 66 | |||
| 67 | # Map settings() to another alias specified with $mapSettingsTo. |
||
| 68 | public function __call($name, $args) |
||
| 69 | { |
||
| 70 | if (isset($this->mapSettingsTo) && $name == $this->mapSettingsTo) { |
||
| 71 | return $this->settings(...$args); |
||
| 72 | } |
||
| 73 | |||
| 74 | return is_callable(['parent', '__call']) |
||
| 75 | ? parent::__call($name, $args) |
||
| 76 | : null; |
||
| 77 | } |
||
| 78 | } |
||
| 79 |