Skip to content

Commit 848227f

Browse files
committed
allow to fill namespaced setting with an array
1 parent cb5a3d0 commit 848227f

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/NamespacedSettings.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,33 @@ public function load(): static
3232
}
3333

3434
/**
35-
* @param Collection<int, Setting> $settings
35+
* @param array<string, mixed>|Collection<int, Setting> $settings
3636
*/
37-
public function fill(Collection $settings): static
37+
public function fill(array|Collection $settings): static
3838
{
3939

40-
$namespace = $this->getNamespace();
40+
if ($settings instanceof Collection) {
41+
$namespace = $this->getNamespace();
4142

42-
foreach (get_object_vars($this) as $name => $value) {
43+
foreach (get_object_vars($this) as $name => $value) {
44+
45+
$setting = $settings->firstWhere(function ($setting) use ($namespace, $name) {
46+
return $setting->namespace === $namespace && $setting->name === $name;
47+
});
48+
49+
if ($setting) {
50+
$this->{$name} = $setting->value;
51+
}
52+
53+
}
54+
} else {
55+
56+
foreach ($settings as $key => $value) {
4357

44-
$setting = $settings->firstWhere(function ($setting) use ($namespace, $name) {
45-
return $setting->namespace === $namespace && $setting->name === $name;
46-
});
58+
if (property_exists(static::class, $key)) {
59+
$this->{$key} = $value;
60+
}
4761

48-
if ($setting) {
49-
$this->{$name} = $setting->value;
5062
}
5163

5264
}

0 commit comments

Comments
 (0)