From 6c5596e436af6a29869f269d6726f539d1d9c1ae Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 24 Sep 2011 16:13:23 +0100 Subject: [PATCH] Add support for hashes in the Config class. --- source/lib/SihnonFramework/Config.class.php | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/lib/SihnonFramework/Config.class.php b/source/lib/SihnonFramework/Config.class.php index 50a84e6..1f804ab 100644 --- a/source/lib/SihnonFramework/Config.class.php +++ b/source/lib/SihnonFramework/Config.class.php @@ -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; }