Add support for hashes in the Config class.

This commit is contained in:
2011-09-24 16:13:23 +01:00
parent b8dfca3aa5
commit 6c5596e436

View File

@@ -32,6 +32,12 @@ class SihnonFramework_Config {
*/
const TYPE_STRING_LIST = 'array(string)';
/**
* Hash type with string keys and mixed-type values
* @var array(string=>mixed)
*/
const TYPE_HASH = 'hash';
/**
* Backend to be used for this Config object
* @var Sihnon_Config_IPlugin
@@ -58,9 +64,11 @@ class SihnonFramework_Config {
protected static function pack($type, $value) {
switch ($type) {
case static::TYPE_STRING_LIST: {
case static::TYPE_STRING_LIST:
return join("\n", $value);
} break;
case static::TYPE_HASH:
return join("\n", array_map(function($k, $v) { return "{$k}:{$v}"; }, array_keys($value), array_values($value)));
default: {
return $value;
@@ -70,9 +78,17 @@ class SihnonFramework_Config {
protected static function unpack($type, $value) {
switch ($type) {
case self::TYPE_STRING_LIST:
case static::TYPE_STRING_LIST:
// foo
// bar
return array_map('trim', explode("\n", $value));
case static::TYPE_HASH:
// foo:bar
// baz:quz
preg_match_all("/^([^:]+):(.+)$/m", $value, $pairs);
return array_combine($pairs[1], $pairs[2]);
default:
return $value;
}