Skip to content

Commit

Permalink
Merge pull request #31 from yii2mod/feature-add-get-all-settings-method
Browse files Browse the repository at this point in the history
Add getAllBySection method to the Settings component
  • Loading branch information
Igor Chepurnoy authored Aug 20, 2018
2 parents f513c0b + fe77ca6 commit 0f8685f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ $settings->remove('section', 'key');
// Removes all settings
$settings->removeAll();

// Get's all values in the specific section.
$settings->getAllBySection('admin');

$settings->invalidateCache(); // automatically called on set(), remove();
```

Expand Down
21 changes: 21 additions & 0 deletions components/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ public function init()
$this->model = Yii::createObject($this->modelClass);
}

/**
* Get's all values in the specific section.
*
* @param string $section
* @param null $default
*
* @return mixed
*/
public function getAllBySection($section, $default = null)
{
$items = $this->getSettingsConfig();

if (isset($items[$section])) {
$this->setting = ArrayHelper::getColumn($items[$section], 'value');
} else {
$this->setting = $default;
}

return $this->setting;
}

/**
* Get's the value for the given section and key.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public function testSetAndGetSetting()
$this->assertEquals('[email protected]', Yii::$app->settings->get('admin', 'email'), 'Wrong setting name!');
}

public function testGetAllSettingsBySection()
{
Yii::$app->settings->set('admin', 'email', '[email protected]');
Yii::$app->settings->set('admin', 'username', 'admin');
Yii::$app->settings->set('admin', 'website', 'http://example.org');

$settings = Yii::$app->settings->getAllBySection('admin');

$this->assertCount(3, $settings, 'Wrong settings count!');
$this->assertEquals('[email protected]', $settings['email']);
$this->assertEquals('admin', $settings['username']);
$this->assertEquals('http://example.org', $settings['website']);
$this->assertNull(Yii::$app->settings->getAllBySection('not-existed'));
}

public function testRemoveSetting()
{
Yii::$app->settings->set('admin', 'email', '[email protected]');
Expand Down

0 comments on commit 0f8685f

Please sign in to comment.