diff --git a/src/Traits/HasSettingsTrait.php b/src/Traits/HasSettingsTrait.php index 730fa29..91f7247 100644 --- a/src/Traits/HasSettingsTrait.php +++ b/src/Traits/HasSettingsTrait.php @@ -80,18 +80,25 @@ public function setSetting($key, $value) * getSetting function. * Returns a bool if the setting is true. * - * @param $key + * @param $key + * @param null $default * * @return bool * @throws InvalidUserSettingsFieldUsed */ - public function getSetting($key) + public function getSetting($key, $default = null) { $this->validateKey($key); $currentSetting = (integer)$this->getAttribute($this->getSettingsColumn()); $bitwiseList = $this->getBitwiseList(); - return ($currentSetting & $bitwiseList[$key]) > 1 ? true : false; + if(($currentSetting & $bitwiseList[$key]) > 1) { + return true; + } + if($default !== null) { + return $default; + } + return false; } /** diff --git a/tests/UserSettingsAreStoredTest.php b/tests/UserSettingsAreStoredTest.php index 222ff96..ce648e3 100644 --- a/tests/UserSettingsAreStoredTest.php +++ b/tests/UserSettingsAreStoredTest.php @@ -62,4 +62,13 @@ public function array_settings_are_stored() $this->assertFalse($user->setting('test_setting_3')); } + /** @test */ + public function default_values_are_returned() + { + $user = $this->testUser; + + $this->assertFalse($user->getSetting('test_setting_3')); + $this->assertEquals('test_return_value', $user->getSetting('test_setting_3', 'test_return_value')); + } + } \ No newline at end of file