From d9eb19443dcb28fc196bf4f9091d62c019539d5a Mon Sep 17 00:00:00 2001 From: Lorand Gombos Date: Thu, 29 Aug 2019 11:33:19 +0300 Subject: [PATCH] Default Settings (#20) * Default Settings --- CHANGELOG.md | 4 ++++ README.md | 17 +++++++++++++++++ src/Managers/AbstractSettingsManager.php | 7 +++++-- src/Traits/HasSettings.php | 11 +++++++++++ tests/FieldSettingsManagerTest.php | 18 +++++++++++++++++- tests/Models/UserWithField.php | 2 ++ tests/Models/UsersWithTable.php | 2 ++ tests/TableSettingsManagerTest.php | 21 +++++++++++++++++++-- 8 files changed, 77 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c483eb..efab393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `glorand/laravel-model-settings` will be documented in this file +## 3.3.0 - 2019-08-29 +### Added +- Default settings on Model + ## 3.2.0 - 2019-08-07 ### Added - Compatibility with PSR-16, CacheInterface diff --git a/README.md b/README.md index 9b173a1..811ffb9 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,9 @@

Latest Stable Version + + + Total Downloads Build Status @@ -37,6 +40,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o - [Updating your Eloquent Models](#update_models) - [Option 1 - `HasSettingsField` trait](#update_models_1) - [Option 2 - `HasSettingsTable` trait](#update_models_2) +- [Default Settings](#default_settings) - [Usage](#usage) - [Get all model's settings](#get_all) - [Get a specific setting](#get) @@ -109,6 +113,19 @@ class User extends Model } ``` +##Default settings + +```php +use Glorand\Model\Settings\Traits\HasSettingsTable; + +class User extends Model +{ + public $defaultSettings = [ + 'key_1' => 'val_1', + ]; +} +``` + ## Usage ```php diff --git a/src/Managers/AbstractSettingsManager.php b/src/Managers/AbstractSettingsManager.php index 5895525..5536baa 100644 --- a/src/Managers/AbstractSettingsManager.php +++ b/src/Managers/AbstractSettingsManager.php @@ -16,9 +16,12 @@ abstract class AbstractSettingsManager implements SettingsManagerContract /** @var \Illuminate\Database\Eloquent\Model */ protected $model; + /** @var array */ + protected $defaultSettings = []; + /** * AbstractSettingsManager constructor. - * @param \Illuminate\Database\Eloquent\Model $model + * @param \Illuminate\Database\Eloquent\Model|HasSettings $model * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException */ public function __construct(Model $model) @@ -34,7 +37,7 @@ public function __construct(Model $model) */ public function all(): array { - return $this->model->getSettingsValue(); + return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()); } /** diff --git a/src/Traits/HasSettings.php b/src/Traits/HasSettings.php index 4c560be..35ed779 100644 --- a/src/Traits/HasSettings.php +++ b/src/Traits/HasSettings.php @@ -2,8 +2,19 @@ namespace Glorand\Model\Settings\Traits; +use Illuminate\Support\Arr; + trait HasSettings { + public function getDefaultSettings(): array + { + if (property_exists($this, 'defaultSettings')) { + return Arr::wrap($this->defaultSettings); + } + + return []; + } + /** * @return array */ diff --git a/tests/FieldSettingsManagerTest.php b/tests/FieldSettingsManagerTest.php index 6b2ea47..c7fdba1 100644 --- a/tests/FieldSettingsManagerTest.php +++ b/tests/FieldSettingsManagerTest.php @@ -17,6 +17,10 @@ class FieldSettingsManagerTest extends TestCase 'email' => "john@doe.com", ], ]; + /** @var array */ + protected $defaultSettingsTestArray = [ + 'project' => 'Main Project', + ]; public function setUp(): void { @@ -31,13 +35,25 @@ public function testInit() } /** - * @throws \Exception + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException */ public function testAll() { $this->assertEquals($this->model->settings()->all(), []); } + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testDefaultValue() + { + $this->model->defaultSettings = $this->defaultSettingsTestArray; + $this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all()); + + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray)); + } + /** * @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException * @expectedExceptionMessage Unknown field diff --git a/tests/Models/UserWithField.php b/tests/Models/UserWithField.php index 02c5660..9404d88 100644 --- a/tests/Models/UserWithField.php +++ b/tests/Models/UserWithField.php @@ -16,4 +16,6 @@ class UserWithField extends Model protected $guarded = []; protected $fillable = ['id', 'name']; + + public $defaultSettings = []; } diff --git a/tests/Models/UsersWithTable.php b/tests/Models/UsersWithTable.php index 5a7f8b6..88fcae7 100644 --- a/tests/Models/UsersWithTable.php +++ b/tests/Models/UsersWithTable.php @@ -17,4 +17,6 @@ class UsersWithTable extends Model protected $guarded = []; protected $fillable = ['id', 'name']; + + public $defaultSettings = []; } diff --git a/tests/TableSettingsManagerTest.php b/tests/TableSettingsManagerTest.php index 220bff4..ac39e06 100644 --- a/tests/TableSettingsManagerTest.php +++ b/tests/TableSettingsManagerTest.php @@ -3,6 +3,7 @@ namespace Glorand\Model\Settings\Tests; use Glorand\Model\Settings\Models\ModelSettings; +use Glorand\Model\Settings\Tests\Models\UsersWithDefaultSettingsTable; use Glorand\Model\Settings\Tests\Models\UsersWithTable as User; use Glorand\Model\Settings\Traits\HasSettingsTable; use Illuminate\Database\Eloquent\Relations\MorphOne; @@ -10,6 +11,8 @@ class TableSettingsManagerTest extends TestCase { + /** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */ + private $model; /** @var array */ protected $testArray = [ 'user' => [ @@ -18,8 +21,10 @@ class TableSettingsManagerTest extends TestCase 'email' => "john@doe.com", ], ]; - /** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */ - private $model; + /** @var array */ + protected $defaultSettingsTestArray = [ + 'project' => 'Main Project', + ]; public function setUp(): void { @@ -41,6 +46,18 @@ public function testAll() $this->assertEquals($this->model->settings()->all(), []); } + /** + * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException + */ + public function testDefaultValue() + { + $this->model->defaultSettings = $this->defaultSettingsTestArray; + $this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all()); + + $this->model->settings()->apply($this->testArray); + $this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray)); + } + /** * @throws \Exception */