Skip to content

Commit f1fbe7b

Browse files
committed
default variables in settings
1 parent e4177ef commit f1fbe7b

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/Settings/Settings.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
use BulkGate\Plugin\{Settings\Repository\Entity\Setting, Strict, Structure\Collection};
11-
use function array_key_exists, array_merge;
11+
use function array_key_exists, array_merge, preg_match;
1212

1313
class Settings
1414
{
@@ -19,6 +19,11 @@ class Settings
1919
*/
2020
private array $settings = [];
2121

22+
/**
23+
* @var array<string, mixed>
24+
*/
25+
private array $default_settings = [];
26+
2227
private Repository\Settings $repository;
2328

2429
public function __construct(Repository\Settings $repository)
@@ -27,6 +32,21 @@ public function __construct(Repository\Settings $repository)
2732
}
2833

2934

35+
/**
36+
* @param array<array-key, mixed> $settings
37+
*/
38+
public function setDefaultSettings(array $settings): void
39+
{
40+
$this->default_settings = [];
41+
42+
foreach ($settings as $key => $value) if (is_string($key) && preg_match('~^[\w_-]+?:?[\w_-]+?$~U', $key))
43+
{
44+
$this->default_settings[$key] = $value;
45+
}
46+
}
47+
48+
49+
3050
/**
3151
* @return mixed
3252
*/
@@ -45,7 +65,7 @@ public function load(string $settings_key, bool $reload = false)
4565
{
4666
return ($this->settings[$scope][$key]->value);
4767
}
48-
return null;
68+
return $this->default_settings[$settings_key] ?? null;
4969
}
5070
return $this->settings[$scope]->toArray();
5171
}

tests/Settings/SettingsTest.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class SettingsTest extends TestCase
3232
Assert::same(['one' => $one, 'two' => $two], $settings->load('main:'));
3333

3434
Assert::same(['one' => $one, 'two' => $two], $settings->load('main:', true));
35+
36+
$settings->setDefaultSettings(['main:three' => 'v3', 'xx|:' => 'b']);
37+
38+
Assert::same('v3', $settings->load('main:three'));
3539
}
3640

3741

0 commit comments

Comments
 (0)