From abaedf12ab72cf26ced9045fba6b4d82846d1392 Mon Sep 17 00:00:00 2001 From: igor-chepurnoi Date: Mon, 20 Aug 2018 22:31:58 +0300 Subject: [PATCH 1/2] Add getAllBySection method to the Settings component --- README.md | 3 +++ components/Settings.php | 22 ++++++++++++++++++++++ tests/SettingsTest.php | 15 +++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 24ba8e4..0d511f0 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/components/Settings.php b/components/Settings.php index 952ed68..22d5877 100755 --- a/components/Settings.php +++ b/components/Settings.php @@ -66,6 +66,28 @@ 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. * diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 59ea02d..77245e3 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -23,6 +23,21 @@ public function testSetAndGetSetting() $this->assertEquals('admin@mail.com', Yii::$app->settings->get('admin', 'email'), 'Wrong setting name!'); } + public function testGetAllSettingsBySection() + { + Yii::$app->settings->set('admin', 'email', 'admin@mail.com'); + 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( 'admin@mail.com', $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', 'admin@mail.com'); From fe77ca689cdbadd69628a70a91d19c87e5a684e1 Mon Sep 17 00:00:00 2001 From: igor-chepurnoi Date: Mon, 20 Aug 2018 22:34:45 +0300 Subject: [PATCH 2/2] cs-fix --- components/Settings.php | 1 - tests/SettingsTest.php | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/components/Settings.php b/components/Settings.php index 22d5877..f2bada0 100755 --- a/components/Settings.php +++ b/components/Settings.php @@ -84,7 +84,6 @@ public function getAllBySection($section, $default = null) $this->setting = $default; } - return $this->setting; } diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 77245e3..48a53cd 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -32,9 +32,9 @@ public function testGetAllSettingsBySection() $settings = Yii::$app->settings->getAllBySection('admin'); $this->assertCount(3, $settings, 'Wrong settings count!'); - $this->assertEquals( 'admin@mail.com', $settings['email']); - $this->assertEquals( 'admin', $settings['username']); - $this->assertEquals( 'http://example.org', $settings['website']); + $this->assertEquals('admin@mail.com', $settings['email']); + $this->assertEquals('admin', $settings['username']); + $this->assertEquals('http://example.org', $settings['website']); $this->assertNull(Yii::$app->settings->getAllBySection('not-existed')); }