Skip to content

Commit d9eb194

Browse files
authored
Default Settings (#20)
* Default Settings
1 parent 4bf842a commit d9eb194

File tree

8 files changed

+77
-5
lines changed

8 files changed

+77
-5
lines changed

CHANGELOG.md

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

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

5+
## 3.3.0 - 2019-08-29
6+
### Added
7+
- Default settings on Model
8+
59
## 3.2.0 - 2019-08-07
610
### Added
711
- Compatibility with PSR-16, CacheInterface

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<p align="center">
1010
<a href="https://packagist.org/packages/glorand/laravel-model-settings">
1111
<img src="https://poser.pugx.org/glorand/laravel-model-settings/v/stable" alt="Latest Stable Version">
12+
</a>
13+
<a href="https://packagist.org/packages/glorand/laravel-model-settings">
14+
<img src="https://poser.pugx.org/glorand/laravel-model-settings/downloads" alt="Total Downloads">
1215
</a>
1316
<a href="https://travis-ci.com/glorand/laravel-model-settings">
1417
<img src="https://travis-ci.com/glorand/laravel-model-settings.svg?branch=master" alt="Build Status">
@@ -37,6 +40,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
3740
- [Updating your Eloquent Models](#update_models)
3841
- [Option 1 - `HasSettingsField` trait](#update_models_1)
3942
- [Option 2 - `HasSettingsTable` trait](#update_models_2)
43+
- [Default Settings](#default_settings)
4044
- [Usage](#usage)
4145
- [Get all model's settings](#get_all)
4246
- [Get a specific setting](#get)
@@ -109,6 +113,19 @@ class User extends Model
109113
}
110114
```
111115

116+
##Default settings <a name="default_settings"></a>
117+
118+
```php
119+
use Glorand\Model\Settings\Traits\HasSettingsTable;
120+
121+
class User extends Model
122+
{
123+
public $defaultSettings = [
124+
'key_1' => 'val_1',
125+
];
126+
}
127+
```
128+
112129
## Usage <a name="usage"></a>
113130

114131
```php

src/Managers/AbstractSettingsManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ abstract class AbstractSettingsManager implements SettingsManagerContract
1616
/** @var \Illuminate\Database\Eloquent\Model */
1717
protected $model;
1818

19+
/** @var array */
20+
protected $defaultSettings = [];
21+
1922
/**
2023
* AbstractSettingsManager constructor.
21-
* @param \Illuminate\Database\Eloquent\Model $model
24+
* @param \Illuminate\Database\Eloquent\Model|HasSettings $model
2225
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
2326
*/
2427
public function __construct(Model $model)
@@ -34,7 +37,7 @@ public function __construct(Model $model)
3437
*/
3538
public function all(): array
3639
{
37-
return $this->model->getSettingsValue();
40+
return array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue());
3841
}
3942

4043
/**

src/Traits/HasSettings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
namespace Glorand\Model\Settings\Traits;
44

5+
use Illuminate\Support\Arr;
6+
57
trait HasSettings
68
{
9+
public function getDefaultSettings(): array
10+
{
11+
if (property_exists($this, 'defaultSettings')) {
12+
return Arr::wrap($this->defaultSettings);
13+
}
14+
15+
return [];
16+
}
17+
718
/**
819
* @return array
920
*/

tests/FieldSettingsManagerTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class FieldSettingsManagerTest extends TestCase
1717
'email' => "[email protected]",
1818
],
1919
];
20+
/** @var array */
21+
protected $defaultSettingsTestArray = [
22+
'project' => 'Main Project',
23+
];
2024

2125
public function setUp(): void
2226
{
@@ -31,13 +35,25 @@ public function testInit()
3135
}
3236

3337
/**
34-
* @throws \Exception
38+
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
3539
*/
3640
public function testAll()
3741
{
3842
$this->assertEquals($this->model->settings()->all(), []);
3943
}
4044

45+
/**
46+
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
47+
*/
48+
public function testDefaultValue()
49+
{
50+
$this->model->defaultSettings = $this->defaultSettingsTestArray;
51+
$this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all());
52+
53+
$this->model->settings()->apply($this->testArray);
54+
$this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray));
55+
}
56+
4157
/**
4258
* @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException
4359
* @expectedExceptionMessage Unknown field

tests/Models/UserWithField.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ class UserWithField extends Model
1616
protected $guarded = [];
1717

1818
protected $fillable = ['id', 'name'];
19+
20+
public $defaultSettings = [];
1921
}

tests/Models/UsersWithTable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ class UsersWithTable extends Model
1717
protected $guarded = [];
1818

1919
protected $fillable = ['id', 'name'];
20+
21+
public $defaultSettings = [];
2022
}

tests/TableSettingsManagerTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Glorand\Model\Settings\Tests;
44

55
use Glorand\Model\Settings\Models\ModelSettings;
6+
use Glorand\Model\Settings\Tests\Models\UsersWithDefaultSettingsTable;
67
use Glorand\Model\Settings\Tests\Models\UsersWithTable as User;
78
use Glorand\Model\Settings\Traits\HasSettingsTable;
89
use Illuminate\Database\Eloquent\Relations\MorphOne;
910
use Illuminate\Support\Facades\DB;
1011

1112
class TableSettingsManagerTest extends TestCase
1213
{
14+
/** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */
15+
private $model;
1316
/** @var array */
1417
protected $testArray = [
1518
'user' => [
@@ -18,8 +21,10 @@ class TableSettingsManagerTest extends TestCase
1821
'email' => "[email protected]",
1922
],
2023
];
21-
/** @var \Glorand\Model\Settings\Tests\Models\UsersWithTable */
22-
private $model;
24+
/** @var array */
25+
protected $defaultSettingsTestArray = [
26+
'project' => 'Main Project',
27+
];
2328

2429
public function setUp(): void
2530
{
@@ -41,6 +46,18 @@ public function testAll()
4146
$this->assertEquals($this->model->settings()->all(), []);
4247
}
4348

49+
/**
50+
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
51+
*/
52+
public function testDefaultValue()
53+
{
54+
$this->model->defaultSettings = $this->defaultSettingsTestArray;
55+
$this->assertEquals($this->defaultSettingsTestArray, $this->model->settings()->all());
56+
57+
$this->model->settings()->apply($this->testArray);
58+
$this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray));
59+
}
60+
4461
/**
4562
* @throws \Exception
4663
*/

0 commit comments

Comments
 (0)