SET setting type fixes

This commit is contained in:
Steffen Schröder 2014-05-18 21:46:00 +02:00
parent 36b8baaecd
commit 3af2da045a
2 changed files with 46 additions and 26 deletions

View File

@ -45,9 +45,11 @@ class Setting {
public function __construct($object, $settingName, $defaultValue) { public function __construct($object, $settingName, $defaultValue) {
if ($object === false) { if ($object === false) {
// Fetched from Database // Fetched from Database
$this->value = $this->castValue($this->value); $this->value = $this->castValue($this->value);
$this->default = $this->castValue($this->default); $this->default = $this->castValue($this->default);
$this->set = $this->castValue($this->set); if ($this->set) {
$this->set = $this->castValue($this->set, $this->type);
}
$this->fetchTime = time(); $this->fetchTime = time();
} else { } else {
// Created by Values // Created by Values
@ -68,26 +70,30 @@ class Setting {
/** /**
* Cast the Value based on the Setting Type * Cast the Value based on the Setting Type
* *
* @param string $value * @param mixed $value
* @param string $type
* @return mixed * @return mixed
*/ */
private function castValue($value) { private static function castValue($value, $type = null) {
if ($this->type === self::TYPE_INT) { if ($type === null) {
$type = self::getValueType($value);
}
if ($type === self::TYPE_INT) {
return (int)$value; return (int)$value;
} }
if ($this->type === self::TYPE_REAL) { if ($type === self::TYPE_REAL) {
return (float)$value; return (float)$value;
} }
if ($this->type === self::TYPE_BOOL) { if ($type === self::TYPE_BOOL) {
return (bool)$value; return (bool)$value;
} }
if ($this->type === self::TYPE_STRING) { if ($type === self::TYPE_STRING) {
return (string)$value; return (string)$value;
} }
if ($this->type === self::TYPE_SET) { if ($type === self::TYPE_SET) {
return explode(self::VALUE_DELIMITER, $value); return explode(self::VALUE_DELIMITER, $value);
} }
trigger_error("Unsupported Setting Value Type: '" . print_r($this->type, true) . "'!"); trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!");
return $value; return $value;
} }
@ -97,7 +103,7 @@ class Setting {
* @param mixed $value * @param mixed $value
* @return string * @return string
*/ */
public static function getValueType($value) { private static function getValueType($value) {
if (is_int($value)) { if (is_int($value)) {
return self::TYPE_INT; return self::TYPE_INT;
} }
@ -123,20 +129,24 @@ class Setting {
* @return string * @return string
*/ */
public function getFormattedValue() { public function getFormattedValue() {
return $this->formatValue($this->value); return self::formatValue($this->value);
} }
/** /**
* Format the given Value based on the Setting Type * Format the given Value based on the Type
* *
* @param mixed $value * @param mixed $value
* @param string $type
* @return string * @return string
*/ */
private function formatValue($value) { private static function formatValue($value, $type = null) {
if ($this->type === self::TYPE_BOOL) { if ($type === null) {
$type = self::getValueType($value);
}
if ($type === self::TYPE_BOOL) {
return ($value ? 1 : 0); return ($value ? 1 : 0);
} }
if ($this->type === self::TYPE_SET) { if ($type === self::TYPE_SET) {
return implode(self::VALUE_DELIMITER, $value); return implode(self::VALUE_DELIMITER, $value);
} }
return $value; return $value;
@ -148,7 +158,7 @@ class Setting {
* @return string * @return string
*/ */
public function getFormattedDefault() { public function getFormattedDefault() {
return $this->formatValue($this->default); return self::formatValue($this->default);
} }
/** /**
@ -157,7 +167,10 @@ class Setting {
* @return string * @return string
*/ */
public function getFormattedSet() { public function getFormattedSet() {
return $this->formatValue($this->set); if ($this->type === self::TYPE_SET) {
return self::formatValue($this->set);
}
return '';
} }
/** /**

View File

@ -237,24 +237,31 @@ class SettingManager implements CallbackListener {
`setting`, `setting`,
`type`, `type`,
`value`, `value`,
`default` `default`,
`set`
) VALUES ( ) VALUES (
?, ?, ?, ?,
@value := ?, ?,
@value ?,
?,
?,
?
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`), `index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`), `type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), `value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)),
`default` = VALUES(`default`), `default` = VALUES(`default`),
`set` = VALUES(`set`),
`changed` = NOW();"; `changed` = NOW();";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$formattedValue = $setting->getFormattedValue(); $formattedValue = $setting->getFormattedValue();
$settingStatement->bind_param('ssss', $setting->class, $setting->setting, $setting->type, $formattedValue); $formattedDefault = $setting->getFormattedDefault();
$formattedSet = $setting->getFormattedSet();
$settingStatement->bind_param('ssssss', $setting->class, $setting->setting, $setting->type, $formattedValue, $formattedDefault, $formattedSet);
$settingStatement->execute(); $settingStatement->execute();
if ($settingStatement->error) { if ($settingStatement->error) {
trigger_error($settingStatement->error); trigger_error($settingStatement->error);