Skip to content

Commit b54e8fb

Browse files
authored
Manage array type settings (#27)
Manage array type settings
1 parent 6f57896 commit b54e8fb

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to `glorand/laravel-model-settings` will be documented in this file
44

5+
## 3.4.2 - 2019-09-25
6+
### Added
7+
- Manage array type settings
58

69
## 3.4.1 - 2019-09-17
710
### Added

src/Traits/HasSettingsField.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ trait HasSettingsField
2020

2121
private $persistSettings = null;
2222

23+
protected static function bootHasSettingsField()
24+
{
25+
static::saving(function (self $model) {
26+
$model->fixSettingsValue();
27+
});
28+
}
29+
2330
/**
2431
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
2532
* @throws ModelSettingsException
@@ -29,6 +36,17 @@ public function settings(): SettingsManagerContract
2936
return new FieldSettingsManager($this);
3037
}
3138

39+
public function fixSettingsValue()
40+
{
41+
$settingsFieldName = $this->getSettingsFieldName();
42+
$attributes = $this->getAttributes();
43+
if (Arr::has($attributes, $settingsFieldName)) {
44+
if (is_array($this->$settingsFieldName)) {
45+
$this->$settingsFieldName = json_encode($this->$settingsFieldName);
46+
}
47+
}
48+
}
49+
3250
/**
3351
* @return array
3452
* @throws ModelSettingsException

tests/FieldSettingsManagerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function testInit()
3535
$this->assertTrue(array_key_exists(HasSettingsField::class, $traits));
3636
}
3737

38+
public function testModelArraySettings()
39+
{
40+
$testArray = ['a' => 'b'];
41+
$this->model->settings = $testArray;
42+
$this->model->save();
43+
$this->assertEquals($this->model->settings()->all(), $testArray);
44+
}
45+
3846
/**
3947
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
4048
*/

0 commit comments

Comments
 (0)