Skip to content

Commit 7603b53

Browse files
committed
feat(settings): add getMany API
1 parent 5433069 commit 7603b53

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

docs/basic-usage.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ To retrieve a config value use the `settings` service.
1515
$siteName = service('settings')->get('App.siteName');
1616
```
1717

18+
You can retrieve multiple values with `getMany()`. This behaves like calling `get()` for each key.
19+
20+
```php
21+
$settings = service('settings')->getMany([
22+
'App.siteName',
23+
'App.siteEmail',
24+
]);
25+
```
26+
1827
In this case we used the short class name, `App`, which the `config()` method automatically locates within the
1928
`app/Config` directory. If it was from a module, it would be found there. Either way, the fully qualified name
2029
is automatically detected by the Settings class to keep values separated from config files that may share the
@@ -119,6 +128,10 @@ setting('App.siteName', 'My Great Site');
119128

120129
// Using the service through the helper
121130
$name = setting()->get('App.siteName');
131+
$settings = setting()->getMany([
132+
'App.siteName',
133+
'App.siteEmail',
134+
]);
122135
setting()->set('App.siteName', 'My Great Site');
123136
setting()->setMany([
124137
'App.siteName' => 'My Great Site',

src/Settings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ public function get(string $key, ?string $context = null)
7272
return $config->{$property} ?? null;
7373
}
7474

75+
/**
76+
* Retrieve multiple values using the same behavior as get().
77+
*
78+
* @param list<string> $keys
79+
*
80+
* @return array<string, mixed>
81+
*/
82+
public function getMany(array $keys, ?string $context = null): array
83+
{
84+
$settings = [];
85+
86+
foreach ($keys as $key) {
87+
$settings[$key] = $this->get($key, $context);
88+
}
89+
90+
return $settings;
91+
}
92+
7593
/**
7694
* Save a value to the writable handler for later retrieval.
7795
*

tests/SettingsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,70 @@ public function testGetWithContext(): void
5757
$this->assertSame('YesContext', $this->settings->get('Example.siteName', 'testing:true'));
5858
}
5959

60+
public function testGetManyReturnsMultipleValues(): void
61+
{
62+
$this->settings->setMany([
63+
'Example.siteName' => 'BatchName',
64+
'Example.siteEmail' => 'batch@example.com',
65+
]);
66+
67+
$this->assertSame([
68+
'Example.siteName' => 'BatchName',
69+
'Example.siteEmail' => 'batch@example.com',
70+
], $this->settings->getMany([
71+
'Example.siteName',
72+
'Example.siteEmail',
73+
]));
74+
}
75+
76+
public function testGetManyFallsBackToConfigValues(): void
77+
{
78+
$this->assertSame([
79+
'Example.siteName' => 'Settings Test',
80+
'Example.siteEmail' => null,
81+
], $this->settings->getMany([
82+
'Example.siteName',
83+
'Example.siteEmail',
84+
]));
85+
}
86+
87+
public function testGetManyWithContext(): void
88+
{
89+
$this->settings->setMany([
90+
'Example.siteName' => 'NoContext',
91+
'Example.siteEmail' => 'general@example.com',
92+
]);
93+
$this->settings->setMany([
94+
'Example.siteName' => 'YesContext',
95+
], 'testing:true');
96+
97+
$this->assertSame([
98+
'Example.siteName' => 'YesContext',
99+
'Example.siteEmail' => 'general@example.com',
100+
], $this->settings->getMany([
101+
'Example.siteName',
102+
'Example.siteEmail',
103+
], 'testing:true'));
104+
}
105+
106+
public function testGetManyPreservesRequestedKeys(): void
107+
{
108+
$this->settings->set('Example.siteName', 'BatchName');
109+
110+
$this->assertSame([
111+
Example::class . '.siteName' => 'BatchName',
112+
'Nada.siteName' => null,
113+
], $this->settings->getMany([
114+
Example::class . '.siteName',
115+
'Nada.siteName',
116+
]));
117+
}
118+
119+
public function testGetManyAcceptsEmptyArray(): void
120+
{
121+
$this->assertSame([], $this->settings->getMany([]));
122+
}
123+
60124
public function testSetManyStoresMultipleValues(): void
61125
{
62126
$this->settings->setMany([

0 commit comments

Comments
 (0)