From 318227f067d1bf730d78fe12853de97c814be1fa Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sat, 8 Oct 2016 14:17:04 +0100 Subject: [PATCH] Boolean settings should be bools! (#2157) Boolean settings should be bools! --- app/Settings/Repository.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/app/Settings/Repository.php b/app/Settings/Repository.php index 9b44173a..6b398bce 100644 --- a/app/Settings/Repository.php +++ b/app/Settings/Repository.php @@ -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; + } }