Skip to content

Commit

Permalink
default variables in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspijak committed Dec 6, 2023
1 parent e4177ef commit f1fbe7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

use BulkGate\Plugin\{Settings\Repository\Entity\Setting, Strict, Structure\Collection};
use function array_key_exists, array_merge;
use function array_key_exists, array_merge, preg_match;

class Settings
{
Expand All @@ -19,6 +19,11 @@ class Settings
*/
private array $settings = [];

/**
* @var array<string, mixed>
*/
private array $default_settings = [];

private Repository\Settings $repository;

public function __construct(Repository\Settings $repository)
Expand All @@ -27,6 +32,21 @@ public function __construct(Repository\Settings $repository)
}


/**
* @param array<array-key, mixed> $settings
*/
public function setDefaultSettings(array $settings): void
{
$this->default_settings = [];

foreach ($settings as $key => $value) if (is_string($key) && preg_match('~^[\w_-]+?:?[\w_-]+?$~U', $key))
{
$this->default_settings[$key] = $value;
}
}



/**
* @return mixed
*/
Expand All @@ -45,7 +65,7 @@ public function load(string $settings_key, bool $reload = false)
{
return ($this->settings[$scope][$key]->value);
}
return null;
return $this->default_settings[$settings_key] ?? null;
}
return $this->settings[$scope]->toArray();
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Settings/SettingsTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class SettingsTest extends TestCase
Assert::same(['one' => $one, 'two' => $two], $settings->load('main:'));

Assert::same(['one' => $one, 'two' => $two], $settings->load('main:', true));

$settings->setDefaultSettings(['main:three' => 'v3', 'xx|:' => 'b']);

Assert::same('v3', $settings->load('main:three'));
}


Expand Down

0 comments on commit f1fbe7b

Please sign in to comment.