2014-05-13 14:15:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Settings;
|
|
|
|
|
2014-05-13 14:32:30 +02:00
|
|
|
use ManiaControl\Utils\ClassUtil;
|
2014-05-13 14:15:00 +02:00
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* ManiaControl Setting Model Class
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2016-05-04 09:57:31 +02:00
|
|
|
* @copyright 2014-2016 ManiaControl Team
|
2014-05-13 14:15:00 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
|
|
|
class Setting {
|
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const CLASS_NAME = __CLASS__;
|
|
|
|
const TYPE_STRING = 'string';
|
|
|
|
const TYPE_INT = 'int';
|
|
|
|
const TYPE_REAL = 'real';
|
|
|
|
const TYPE_BOOL = 'bool';
|
2014-05-14 00:28:53 +02:00
|
|
|
const TYPE_SET = 'set';
|
|
|
|
const VALUE_DELIMITER = ';;';
|
2014-05-13 14:15:00 +02:00
|
|
|
|
|
|
|
/*
|
2014-07-25 16:28:47 +02:00
|
|
|
* Public properties
|
2014-05-13 14:15:00 +02:00
|
|
|
*/
|
|
|
|
public $index = null;
|
|
|
|
public $class = null;
|
|
|
|
public $setting = null;
|
|
|
|
public $type = null;
|
|
|
|
public $value = null;
|
|
|
|
public $default = null;
|
2014-05-14 00:28:53 +02:00
|
|
|
public $set = null;
|
2014-05-13 14:15:00 +02:00
|
|
|
public $fetchTime = null;
|
|
|
|
|
|
|
|
/**
|
2014-07-25 16:28:47 +02:00
|
|
|
* Construct a new setting instance
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
2014-05-14 00:28:53 +02:00
|
|
|
* @param mixed $object
|
|
|
|
* @param string $settingName
|
|
|
|
* @param mixed $defaultValue
|
2014-05-13 14:15:00 +02:00
|
|
|
*/
|
2014-05-14 00:28:53 +02:00
|
|
|
public function __construct($object, $settingName, $defaultValue) {
|
|
|
|
if ($object === false) {
|
|
|
|
// Fetched from Database
|
2014-05-18 21:46:00 +02:00
|
|
|
$this->value = $this->castValue($this->value);
|
|
|
|
$this->default = $this->castValue($this->default);
|
|
|
|
if ($this->set) {
|
|
|
|
$this->set = $this->castValue($this->set, $this->type);
|
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
$this->fetchTime = time();
|
2014-05-14 00:28:53 +02:00
|
|
|
} else {
|
|
|
|
// Created by Values
|
|
|
|
$this->class = ClassUtil::getClass($object);
|
|
|
|
$this->setting = (string)$settingName;
|
|
|
|
$this->type = self::getValueType($defaultValue);
|
|
|
|
if ($this->type === self::TYPE_SET) {
|
|
|
|
// Save Set and use first Value as Default
|
|
|
|
$this->set = $defaultValue;
|
|
|
|
$this->value = reset($defaultValue);
|
|
|
|
} else {
|
|
|
|
$this->value = $defaultValue;
|
|
|
|
}
|
|
|
|
$this->default = $this->value;
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-14 00:28:53 +02:00
|
|
|
* Cast the Value based on the Setting Type
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
2014-05-18 21:46:00 +02:00
|
|
|
* @param mixed $value
|
|
|
|
* @param string $type
|
2014-05-13 14:15:00 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-05-18 21:46:00 +02:00
|
|
|
private static function castValue($value, $type = null) {
|
|
|
|
if ($type === null) {
|
|
|
|
$type = self::getValueType($value);
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_INT) {
|
2014-05-13 14:15:00 +02:00
|
|
|
return (int)$value;
|
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($type === self::TYPE_REAL) {
|
2014-05-13 14:15:00 +02:00
|
|
|
return (float)$value;
|
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($type === self::TYPE_BOOL) {
|
2014-05-13 14:15:00 +02:00
|
|
|
return (bool)$value;
|
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($type === self::TYPE_STRING) {
|
2014-05-13 14:15:00 +02:00
|
|
|
return (string)$value;
|
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($type === self::TYPE_SET) {
|
2014-05-14 00:28:53 +02:00
|
|
|
return explode(self::VALUE_DELIMITER, $value);
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
trigger_error("Unsupported Setting Value Type: '" . print_r($type, true) . "'!");
|
2014-05-13 14:15:00 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-14 00:28:53 +02:00
|
|
|
* Get Type of a Value Parameter
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
2014-05-14 00:28:53 +02:00
|
|
|
* @param mixed $value
|
2014-05-13 14:15:00 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-18 21:46:00 +02:00
|
|
|
private static function getValueType($value) {
|
2014-05-14 00:28:53 +02:00
|
|
|
if (is_int($value)) {
|
|
|
|
return self::TYPE_INT;
|
|
|
|
}
|
|
|
|
if (is_real($value)) {
|
|
|
|
return self::TYPE_REAL;
|
|
|
|
}
|
|
|
|
if (is_bool($value)) {
|
|
|
|
return self::TYPE_BOOL;
|
|
|
|
}
|
|
|
|
if (is_string($value)) {
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
|
|
return self::TYPE_SET;
|
|
|
|
}
|
|
|
|
trigger_error("Unsupported Setting Value Type: '" . print_r($value, true) . "'!");
|
|
|
|
return null;
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
|
2014-05-18 23:35:39 +02:00
|
|
|
/**
|
|
|
|
* Get whether the Setting has been persisted at some point
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPersisted() {
|
|
|
|
return ($this->index > 0);
|
|
|
|
}
|
|
|
|
|
2014-05-13 14:15:00 +02:00
|
|
|
/**
|
|
|
|
* Get the Formatted Value of the Setting
|
|
|
|
*
|
2014-05-14 00:28:53 +02:00
|
|
|
* @return string
|
2014-05-13 14:15:00 +02:00
|
|
|
*/
|
|
|
|
public function getFormattedValue() {
|
2014-05-18 21:46:00 +02:00
|
|
|
return self::formatValue($this->value);
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-18 21:46:00 +02:00
|
|
|
* Format the given Value based on the Type
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
2014-05-18 21:46:00 +02:00
|
|
|
* @param mixed $value
|
|
|
|
* @param string $type
|
2014-05-14 00:28:53 +02:00
|
|
|
* @return string
|
2014-05-13 14:15:00 +02:00
|
|
|
*/
|
2014-05-18 21:46:00 +02:00
|
|
|
private static function formatValue($value, $type = null) {
|
|
|
|
if ($type === null) {
|
|
|
|
$type = self::getValueType($value);
|
|
|
|
}
|
|
|
|
if ($type === self::TYPE_BOOL) {
|
2014-05-13 14:15:00 +02:00
|
|
|
return ($value ? 1 : 0);
|
|
|
|
}
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($type === self::TYPE_SET) {
|
2014-05-14 00:28:53 +02:00
|
|
|
return implode(self::VALUE_DELIMITER, $value);
|
|
|
|
}
|
2014-05-13 14:15:00 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-14 00:28:53 +02:00
|
|
|
* Get the Formatted Default of the Setting
|
2014-05-13 14:15:00 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-14 00:28:53 +02:00
|
|
|
public function getFormattedDefault() {
|
2014-05-18 21:46:00 +02:00
|
|
|
return self::formatValue($this->default);
|
2014-05-14 00:28:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Formatted Set of the Setting
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormattedSet() {
|
2014-05-18 21:46:00 +02:00
|
|
|
if ($this->type === self::TYPE_SET) {
|
|
|
|
return self::formatValue($this->set);
|
|
|
|
}
|
|
|
|
return '';
|
2014-05-13 14:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the Settings belongs to the given Class
|
|
|
|
*
|
|
|
|
* @param mixed $object
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function belongsToClass($object) {
|
|
|
|
$className = ClassUtil::getClass($object);
|
|
|
|
return ($className === $this->class);
|
|
|
|
}
|
|
|
|
}
|