Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean settings should be bools! #2157

Merged
merged 2 commits into from
Oct 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions app/Settings/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
*/
class Repository
{
/**
* Array of numerical settings that are not bools.
*
* @var string[]
*/
const NOT_BOOL = [
'app_incident_days',
];

/**
* The eloquent model instance.
*
Expand Down Expand Up @@ -54,7 +63,9 @@ public function __construct(Setting $model)
*/
public function all()
{
return $this->model->all(['name', 'value'])->pluck('value', 'name')->toArray();
return $this->model->all(['name', 'value'])->pluck('value', 'name')->map(function ($value, $name) {
return $this->castSetting($name, $value);
})->toArray();
}

/**
Expand Down Expand Up @@ -87,7 +98,7 @@ public function set($name, $value)
public function get($name, $default = null)
{
if ($setting = $this->model->where('name', $name)->first()) {
return $setting->value;
return $this->castSetting($name, $setting->value);
}

return $default;
Expand Down Expand Up @@ -128,4 +139,25 @@ public function stale()
{
return $this->stale;
}

/**
* Cast setting as the applicable type.
*
* @param string $key
* @param string $value
*
* @return mixed
*/
protected function castSetting($key, $value)
{
if (is_null($value)) {
return $value;
}

if (!in_array($key, self::NOT_BOOL) && in_array($value, ['0', '1'])) {
return (bool) $value;
}

return $value;
}
}