Boolean settings should be bools! (#2157)

Boolean settings should be bools!
This commit is contained in:
James Brooks
2016-10-08 14:17:04 +01:00
committed by GitHub
parent dfeab93d88
commit 318227f067

View File

@@ -21,6 +21,15 @@ use CachetHQ\Cachet\Models\Setting;
*/
class Repository
{
/**
* Array of numerical settings that are not bools.
*
* @var string[]
*/
const NOT_BOOL = [
'app_incident_days',
];
/**
* The eloquent model instance.
*
@@ -54,7 +63,9 @@ class Repository
*/
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();
}
/**
@@ -87,7 +98,7 @@ class Repository
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;
@@ -128,4 +139,25 @@ class Repository
{
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;
}
}