X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=WEB-INF%2Flib%2FttConfigHelper.class.php;h=33e94e51350d6783f407b0a210ec77a818c40925;hb=75a1eedb8977b8f2db459128bab9aaf367e3b58b;hp=20af19ad0fbee07952d094712cace8859d3a95cd;hpb=a9d5914d51687f6f760b9c0e72fc4e99101f2830;p=timetracker.git diff --git a/WEB-INF/lib/ttConfigHelper.class.php b/WEB-INF/lib/ttConfigHelper.class.php index 20af19ad..33e94e51 100644 --- a/WEB-INF/lib/ttConfigHelper.class.php +++ b/WEB-INF/lib/ttConfigHelper.class.php @@ -38,13 +38,15 @@ class ttConfigHelper { // Constructor. function __construct($config) { - $this->config = $config; - $this->config_array = explode(',', $this->config); + $this->config = trim($config, ' ,'); + if ($this->config) + $this->config_array = explode(',', $this->config); } // getDefinedValue determines if a value identified by name is defined. function getDefinedValue($name) { - return in_array($name, $this->config_array); + $defined = $this->config_array ? in_array($name, $this->config_array) : false; + return $defined; } // setDefinedValue either sets or deletes a defined value identified by name. @@ -58,31 +60,34 @@ class ttConfigHelper { } } else { // Deleting part. - foreach ($this->config_array as $key => $value) { - if ($value === $name) { - unset($this->config_array[$key]); - $this->config = implode(',', $this->config_array); - return; + if ($this->config_array) { + foreach ($this->config_array as $key => $value) { + if ($value === $name) { + unset($this->config_array[$key]); + $this->config = implode(',', $this->config_array); + return; + } } } } } - // The getIntValue parses an integer value from the source config string. - function getIntValue($name) { + // The getIntValue parses an integer value from the source config array. + function getIntValue($name, $defaultVal = 0) { $name_with_colon = $name.':'; $len = strlen($name_with_colon); - foreach ($this->config_array as $unparsed_value) { - if (substr($unparsed_value, 0, $len) === $name_with_colon) { - // Found value. - $unparsed_len = strlen($unparsed_value); - $int_value = (int) substr($unparsed_value, -($unparsed_len - $len)); - return $int_value; + if ($this->config_array) { + foreach ($this->config_array as $unparsed_value) { + if (substr($unparsed_value, 0, $len) === $name_with_colon) { + // Found value. + $unparsed_len = strlen($unparsed_value); + $int_value = (int) substr($unparsed_value, -($unparsed_len - $len)); + return $int_value; + } } - } - return false; + return $defaultVal; } // The setIntValue sets an integer value into config array. @@ -91,18 +96,20 @@ class ttConfigHelper { $name_with_colon = $name.':'; $len = strlen($name_with_colon); - foreach ($this->config_array as $key => $unparsed_value) { - if (substr($unparsed_value, 0, $len) === $name_with_colon) { - // Found an already existing value. - if ($value !== null) { - // Replace value. - $this->config_array[$key] = $name.':'.$value; - } else { - // Remove value if our new value is NULL. - unset($this->config_array[$key]); + if ($this->config_array) { + foreach ($this->config_array as $key => $unparsed_value) { + if (substr($unparsed_value, 0, $len) === $name_with_colon) { + // Found an already existing value. + if ($value !== null) { + // Replace value. + $this->config_array[$key] = $name.':'.$value; + } else { + // Remove value if our new value is NULL. + unset($this->config_array[$key]); + } + $this->config = implode(',', $this->config_array); + return; } - $this->config = implode(',', $this->config_array); - return; } } // If we get here, the value was not found, so add it. @@ -113,6 +120,6 @@ class ttConfigHelper { // The getConfig returns the config string. function getConfig() { - return $this->config; + return trim($this->config, ' ,'); } }