Skip to content

Commit 0a42afd

Browse files
authored
Refactor the work with default settings (flatten arrays) (#78)
* Refactor the work with default settings (flatten arrays)
1 parent 38b2b35 commit 0a42afd

File tree

4 files changed

+107
-19
lines changed

4 files changed

+107
-19
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+
## 4.2.0 - 2020-12-14
6+
### Added
7+
- Refactor the work with default settings (flatten arrays)
8+
59
## 4.1.0 - 2020-12-03
610
### Added
711
- Refactor unit tests

src/Managers/AbstractSettingsManager.php

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,59 @@ public function __construct(Model $model)
3434
}
3535

3636
/**
37+
* Check if array is associative and not sequential
38+
* @param array $arr
39+
* @return bool
40+
*/
41+
private static function isAssoc(array $arr): bool
42+
{
43+
if ([] === $arr) {
44+
return false;
45+
}
46+
47+
return array_keys($arr) !== range(0, count($arr) - 1);
48+
}
49+
50+
/**
51+
* Flatten array with dots for settings package
52+
* @param $array
53+
* @param string $prepend
3754
* @return array
3855
*/
39-
public function all(): array
56+
public static function dotFlatten($array, $prepend = ''): array
4057
{
41-
$array = [];
42-
foreach (array_merge($this->model->getDefaultSettings(), $this->model->getSettingsValue()) as $key => $value) {
43-
Arr::set($array, $key, $value);
58+
$results = [];
59+
foreach ($array as $key => $value) {
60+
// only re-run if nested array is associative (key-based)
61+
if (is_array($value) && static::isAssoc($value) && !empty($value)) {
62+
$results = array_merge($results, static::dotFlatten($value, $prepend . $key . '.'));
63+
} else {
64+
$results[$prepend . $key] = $value;
65+
}
4466
}
4567

46-
return $array;
68+
return $results;
69+
}
70+
71+
/**
72+
* Get nested merged array with all available keys
73+
* @return array
74+
*/
75+
public function all(): array
76+
{
77+
return $this->getMultiple();
78+
}
79+
80+
/**
81+
* Get flat merged array with dot-notation keys
82+
* @return array
83+
*/
84+
public function allFlattened(): array
85+
{
86+
$flattenedDefaultSettings = static::dotFlatten($this->model->getDefaultSettings());
87+
$flattenedSettingsValue = static::dotFlatten($this->model->getSettingsValue());
88+
89+
return array_merge($flattenedDefaultSettings, $flattenedSettingsValue);
4790
}
4891

4992
/**
@@ -74,7 +117,7 @@ public function has(string $path): bool
74117
/**
75118
* @param string|null $path
76119
* @param null $default
77-
* @return array|mixed
120+
* @return array|\ArrayAccess|mixed
78121
*/
79122
public function get(string $path = null, $default = null)
80123
{
@@ -84,16 +127,25 @@ public function get(string $path = null, $default = null)
84127
/**
85128
* @param iterable|null $paths
86129
* @param null $default
87-
* @return iterable
130+
* @return array
88131
*/
89-
public function getMultiple(iterable $paths = null, $default = null): iterable
132+
public function getMultiple(iterable $paths = null, $default = null): array
90133
{
91-
$values = [];
134+
$array = [];
135+
$allFlattened = $this->allFlattened();
136+
$settingsArray = [];
137+
foreach ($allFlattened as $key => $value) {
138+
Arr::set($settingsArray, $key, $value);
139+
}
140+
if (is_null($paths)) {
141+
return $settingsArray;
142+
}
143+
92144
foreach ($paths as $path) {
93-
$values[$path] = $this->get($path, $default);
145+
Arr::set($array, $path, Arr::get($settingsArray, $path, $default));
94146
}
95147

96-
return $values;
148+
return $array;
97149
}
98150

99151
/**

tests/CommonFunctionalityTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ public function testGetMultiple(string $modelType)
151151
$values = $model->settings()->getMultiple(['user.first_name', 'user.last_name'], 'def_val');
152152
$this->assertEquals(
153153
[
154-
'user.first_name' => 'def_val',
155-
'user.last_name' => 'def_val',
154+
'user' => [
155+
'first_name' => 'def_val',
156+
'last_name' => 'def_val',
157+
],
156158
],
157159
$values
158160
);
@@ -169,7 +171,9 @@ public function testGetMultiple(string $modelType)
169171
'last_name' => 'Doe',
170172
'email' => '[email protected]',
171173
],
172-
'project.name' => 'Project One',
174+
'project' => [
175+
'name' => 'Project One',
176+
],
173177
'date' => 'def_val',
174178
],
175179
$values
@@ -303,6 +307,38 @@ public function testDefaultValue(string $modelType)
303307
array_merge($this->defaultSettingsTestArray, $this->testArray),
304308
$model->settings()->all()
305309
);
310+
311+
$model->settings()->clear();
312+
313+
$default = [
314+
'a' => [
315+
'val_a_1' => 1,
316+
'val_a_2' => 2,
317+
],
318+
];
319+
$applyData = [
320+
'a' => [
321+
'val_a_2' => '2-updated',
322+
'val_a_3' => 3,
323+
],
324+
'b' => 'b-val',
325+
];
326+
327+
$model->defaultSettings = $default;
328+
$this->assertEquals($default, $model->settings()->all());
329+
330+
$model->settings()->apply($applyData);
331+
$this->assertEquals(
332+
[
333+
'a' => [
334+
'val_a_1' => 1,
335+
'val_a_2' => '2-updated',
336+
'val_a_3' => 3,
337+
],
338+
'b' => 'b-val',
339+
],
340+
$model->settings()->all()
341+
);
306342
}
307343

308344
/**

tests/TestCase.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ public function setUp(): void
3030

3131
protected function checkRequirements()
3232
{
33-
collect($this->getAnnotations())->filter(function ($location) {
34-
return in_array('!Travis', Arr::get($location, 'requires', []));
35-
})->each(function ($location) {
36-
$this->markTestSkipped('Travis will not run this test.');
37-
});
33+
//
3834
}
3935

4036
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)