Skip to content

Commit 5551863

Browse files
committed
feat(v3): add SDK config bag
1 parent e2db700 commit 5551863

4 files changed

Lines changed: 140 additions & 1 deletion

File tree

‎src/Api.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Api
3232

3333
private array $headerDefaults = [];
3434

35+
private Config $config;
36+
3537
private ClientBuilder $clientBuilder;
3638

3739
private ?CacheBuilder $cacheBuilder = null;
@@ -46,6 +48,7 @@ class Api
4648

4749
public function __construct()
4850
{
51+
$this->config = new Config();
4952
$this->clientBuilder ??= new ClientBuilder();
5053
$this->responseBuilder = new ResponseBuilder();
5154
$this->eventDispatcher = new EventDispatcher();
@@ -141,6 +144,15 @@ protected function responses(): ResponseBuilder
141144
return $this->responseBuilder;
142145
}
143146

147+
protected function config(?array $values = null): Config
148+
{
149+
if ($values !== null) {
150+
$this->config->merge($values);
151+
}
152+
153+
return $this->config;
154+
}
155+
144156
private function configurePlugins(): void
145157
{
146158
// https://docs.php-http.org/en/latest/plugins/content-type.html

‎src/Config.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Api;
4+
5+
class Config
6+
{
7+
public function __construct(
8+
private array $values = []
9+
) {}
10+
11+
public function all(): array
12+
{
13+
return $this->values;
14+
}
15+
16+
public function has(string $key): bool
17+
{
18+
return array_key_exists($key, $this->values);
19+
}
20+
21+
public function get(string $key, mixed $default = null): mixed
22+
{
23+
if (!$this->has($key)) {
24+
return $default;
25+
}
26+
27+
return $this->values[$key];
28+
}
29+
30+
public function set(string $key, mixed $value): self
31+
{
32+
$this->values[$key] = $value;
33+
34+
return $this;
35+
}
36+
37+
public function merge(array $values): self
38+
{
39+
foreach ($values as $key => $value) {
40+
$this->set($key, $value);
41+
}
42+
43+
return $this;
44+
}
45+
}

‎tests/Integration/ApiTest.php‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,40 @@ public function testHeaderDefaults()
8888
$this->assertNull($this->api->getHeaderDefault('X-Test'));
8989
}
9090

91+
public function testConfigCanBeSetAndReadBySdkApi(): void
92+
{
93+
$api = new class extends Api {
94+
public function setOptions(array $options): self
95+
{
96+
$this->config($options);
97+
98+
return $this;
99+
}
100+
101+
public function option(string $key, mixed $default = null): mixed
102+
{
103+
return $this->config()->get($key, $default);
104+
}
105+
106+
public function options(): array
107+
{
108+
return $this->config()->all();
109+
}
110+
};
111+
112+
$api
113+
->setOptions(['timezone' => 'UTC'])
114+
->setOptions(['units' => 'metric']);
115+
116+
$this->assertSame('UTC', $api->option('timezone'));
117+
$this->assertSame('metric', $api->option('units'));
118+
$this->assertSame('en', $api->option('locale', 'en'));
119+
$this->assertSame([
120+
'timezone' => 'UTC',
121+
'units' => 'metric',
122+
], $api->options());
123+
}
124+
91125
public function testCache()
92126
{
93127
$this->assertNull($this->api->getCacheBuilder());
@@ -238,4 +272,4 @@ public function testBuildPath()
238272

239273
$this->assertSame('/path/with/multiple/parameters', $path);
240274
}
241-
}
275+
}

‎tests/Unit/ConfigTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Api\Test\Unit;
4+
5+
use ProgrammatorDev\Api\Config;
6+
use ProgrammatorDev\Api\Test\AbstractTestCase;
7+
8+
class ConfigTest extends AbstractTestCase
9+
{
10+
public function testConfigReturnsValues(): void
11+
{
12+
$config = new Config(['timezone' => 'UTC']);
13+
14+
$this->assertTrue($config->has('timezone'));
15+
$this->assertSame('UTC', $config->get('timezone'));
16+
$this->assertSame(['timezone' => 'UTC'], $config->all());
17+
}
18+
19+
public function testConfigReturnsDefaultForMissingValue(): void
20+
{
21+
$config = new Config();
22+
23+
$this->assertFalse($config->has('timezone'));
24+
$this->assertSame('Europe/Lisbon', $config->get('timezone', 'Europe/Lisbon'));
25+
}
26+
27+
public function testConfigCanStoreNullValues(): void
28+
{
29+
$config = new Config(['timezone' => null]);
30+
31+
$this->assertTrue($config->has('timezone'));
32+
$this->assertNull($config->get('timezone', 'Europe/Lisbon'));
33+
}
34+
35+
public function testConfigCanBeUpdated(): void
36+
{
37+
$config = new Config(['timezone' => 'UTC']);
38+
39+
$config
40+
->set('timezone', 'Europe/Lisbon')
41+
->merge(['units' => 'metric']);
42+
43+
$this->assertSame([
44+
'timezone' => 'Europe/Lisbon',
45+
'units' => 'metric',
46+
], $config->all());
47+
}
48+
}

0 commit comments

Comments
 (0)