Skip to content

Commit 2b53bf7

Browse files
committed
Nicer syntax through the helper. Fixes #3
1 parent ad5af97 commit 2b53bf7

File tree

3 files changed

+121
-17
lines changed

3 files changed

+121
-17
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ or telling your BaseController to always load it.
6767

6868
```php
6969
helper('setting');
70-
$name = setting('App', 'siteName');
70+
71+
// Using 'dot' syntax to separate class and field name
72+
$name = setting('App.siteName');
73+
// Store a value
74+
setting('App.sitename', 'My Great Site');
75+
76+
// Using the service through the helper
7177
$name = setting()->get('App', 'siteName');
7278
$setting()->set('App', 'siteName', 'My Great Site');
7379
```

src/Helpers/setting_helper.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
<?php
22

3-
if (! function_exists('setting')) {
4-
/**
5-
* Provides a convenience interface to the
6-
* Bonfire/Settings/Settings class.
7-
*
8-
* @param string|null $class
9-
* @param string|null $field
10-
*/
11-
function setting(string $class = null, string $field = null)
12-
{
13-
$setting = service('settings');
3+
if (! function_exists('setting'))
4+
{
5+
/**
6+
* Provides a convenience interface to the
7+
* Bonfire/Settings/Settings class.
8+
*
9+
* @param string|null $field
10+
* @param null $value
11+
*
12+
* @return mixed
13+
*/
14+
function setting(string $field = null, $value = null)
15+
{
16+
$setting = service('settings');
1417

15-
if (empty($class) || empty($field)) {
16-
return $setting;
17-
}
18+
if (empty($field))
19+
{
20+
return $setting;
21+
}
1822

19-
return $setting->get($class, $field);
20-
}
23+
// Parse the field name for class.field
24+
$parts = explode('.', $field);
25+
26+
if (count($parts) === 1)
27+
{
28+
throw new \RuntimeException('$field must contain both the class and field name, i.e. Foo.bar');
29+
}
30+
31+
[
32+
$class,
33+
$field,
34+
] = $parts;
35+
36+
// Getting the value?
37+
if ($value === null)
38+
{
39+
return $setting->get($class, $field);
40+
}
41+
42+
// Setting the value
43+
return $setting->set($class, $field, $value);
44+
}
2145
}

tests/HelperTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace CodeIgniter4\Tests;
4+
5+
use CodeIgniter\I18n\Time;
6+
use CodeIgniter\Test\DatabaseTestTrait;
7+
use Sparks\Settings\Settings;
8+
use Tests\Support\TestCase;
9+
10+
class HelperTest extends TestCase
11+
{
12+
use DatabaseTestTrait;
13+
14+
protected $namespace = 'Sparks\Settings';
15+
protected $refresh = true;
16+
protected $table;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->table = config('Settings')->database['table'];
23+
helper('setting');
24+
}
25+
26+
public function testReturnsServiceByDefault()
27+
{
28+
$this->assertInstanceOf(Settings::class, setting());
29+
}
30+
31+
public function testThrowsExceptionWithInvalidField()
32+
{
33+
$this->expectException(\RuntimeException::class);
34+
35+
setting('Foobar');
36+
}
37+
38+
public function testReturnsValueDotArray()
39+
{
40+
$this->hasInDatabase($this->table, [
41+
'class' => 'Foo',
42+
'key' => 'bar',
43+
'value' => 'baz',
44+
'type' => 'string',
45+
'created_at' => Time::now()->toDateTimeString(),
46+
'updated_at' => Time::now()->toDateTimeString(),
47+
]);
48+
49+
$this->assertEquals('baz', setting('Foo.bar'));
50+
}
51+
52+
public function testSettingValueDotArray()
53+
{
54+
$this->hasInDatabase($this->table, [
55+
'class' => 'Foo',
56+
'key' => 'bar',
57+
'value' => 'baz',
58+
'type' => 'string',
59+
'created_at' => Time::now()->toDateTimeString(),
60+
'updated_at' => Time::now()->toDateTimeString(),
61+
]);
62+
63+
setting('Foo.bar', false);
64+
65+
$this->seeInDatabase($this->table, [
66+
'class' => 'Foo',
67+
'key' => 'bar',
68+
'value' => '0',
69+
'type' => 'boolean',
70+
]);
71+
72+
$this->assertSame(false, setting('Foo.bar'));
73+
}
74+
}

0 commit comments

Comments
 (0)