Skip to content

Commit

Permalink
Added default value in getSettings method (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Taronyuu committed Jul 7, 2018
1 parent 23a932c commit 3ef1218
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Traits/HasSettingsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/UserSettingsAreStoredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}

0 comments on commit 3ef1218

Please sign in to comment.