Index: library/Zend/Config/Ini.php =================================================================== --- library/Zend/Config/Ini.php (Revision 7199) +++ library/Zend/Config/Ini.php (Arbeitskopie) @@ -83,7 +83,7 @@ * @param boolean|array $config * @throws Zend_Config_Exception */ - public function __construct($filename, $section = null, $config = false) + public function __construct($filename, $section = null, $config = false, $detectTypes = null) { if (empty($filename)) { /** @see Zend_Config_Exception */ @@ -102,6 +102,9 @@ $this->_nestSeparator = (string) $config['nestSeparator']; } } + if (!is_bool($detectTypes)) { + $detectTypes = isset($config['detectTypes']) ? (bool) $config['detectTypes'] : false; + } $iniArray = parse_ini_file($filename, true); $preProcessedArray = array(); @@ -132,7 +135,7 @@ foreach ($preProcessedArray as $sectionName => $sectionData) { $dataArray[$sectionName] = $this->_processExtends($preProcessedArray, $sectionName); } - parent::__construct($dataArray, $allowModifications); + parent::__construct($dataArray, $allowModifications, $detectTypes); } elseif (is_array($section)) { $dataArray = array(); foreach ($section as $sectionName) { @@ -144,14 +147,14 @@ $dataArray = array_merge($this->_processExtends($preProcessedArray, $sectionName), $dataArray); } - parent::__construct($dataArray, $allowModifications); + parent::__construct($dataArray, $allowModifications, $detectTypes); } else { if (!isset($preProcessedArray[$section])) { /** @see Zend_Config_Exception */ require_once 'Zend/Config/Exception.php'; throw new Zend_Config_Exception("Section '$section' cannot be found in $filename"); } - parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications); + parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications, $detectTypes); } $this->_loadedSection = $section; Index: library/Zend/Config/Xml.php =================================================================== --- library/Zend/Config/Xml.php (Revision 7199) +++ library/Zend/Config/Xml.php (Arbeitskopie) @@ -53,7 +53,7 @@ * @param boolean $allowModifications * @throws Zend_Config_Exception */ - public function __construct($filename, $section = null, $allowModifications = false) + public function __construct($filename, $section = null, $allowModifications = false, $detectTypes = false) { if (empty($filename)) { /** @see Zend_Config_Exception */ @@ -68,7 +68,7 @@ foreach ($config as $sectionName => $sectionData) { $dataArray[$sectionName] = $this->_processExtends($config, $sectionName); } - parent::__construct($dataArray, $allowModifications); + parent::__construct($dataArray, $allowModifications, $detectTypes); } elseif (is_array($section)) { $dataArray = array(); foreach ($section as $sectionName) { @@ -79,7 +79,7 @@ } $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray); } - parent::__construct($dataArray, $allowModifications); + parent::__construct($dataArray, $allowModifications, $detectTypes); } else { if (!isset($config->$section)) { /** @see Zend_Config_Exception */ @@ -91,7 +91,7 @@ // section in the XML file contains just one top level string $dataArray = array($section=>$dataArray); } - parent::__construct($dataArray, $allowModifications); + parent::__construct($dataArray, $allowModifications, $detectTypes); } $this->_loadedSection = $section; Index: library/Zend/Config.php =================================================================== --- library/Zend/Config.php (Revision 7199) +++ library/Zend/Config.php (Arbeitskopie) @@ -37,6 +37,13 @@ protected $_allowModifications; /** + * Check and cast types, or not + * + * @var boolean + */ + protected $_detectTypes; + + /** * Iteration index * * @var integer @@ -87,15 +94,16 @@ * @param boolean $allowModifications * @return void */ - public function __construct($array, $allowModifications = false) + public function __construct($array, $allowModifications = false, $detectTypes = false) { $this->_allowModifications = (boolean) $allowModifications; + $this->_detectTypes = (boolean) $detectTypes; $this->_loadedSection = null; $this->_index = 0; $this->_data = array(); foreach ($array as $key => $value) { if (is_array($value)) { - $this->_data[$key] = new self($value, $this->_allowModifications); + $this->_data[$key] = new self($value, $this->_allowModifications, $this->_detectTypes); } else { $this->_data[$key] = $value; } @@ -115,6 +123,7 @@ $result = $default; if (array_key_exists($name, $this->_data)) { $result = $this->_data[$name]; + $result = $this->_detectTypes($result); } return $result; } @@ -167,7 +176,7 @@ if ($value instanceof Zend_Config) { $array[$key] = $value->toArray(); } else { - $array[$key] = $value; + $array[$key] = $this->_detectTypes($value); } } return $array; @@ -345,4 +354,33 @@ $this->_extends[$extendingSection] = $extendedSection; } + /** + * Checks $value in being one of the elemental data types and casts it accordingly. + * + * @param string $value + * @return mixed + */ + protected function _detectTypes($value) + { + if ($this->_detectTypes and !$value instanceof Zend_Config) { + switch(true) { + case strtolower($value) === "true": + $value = (bool) true; + break; + + case strtolower($value) === "false": + $value = (bool) false; + break; + + case $value === (string)(int) $value: + $value = (int) $value; + break; + + case $value === (string)(float) $value: + $value = (float) $value; + break; + } + } + return $value; + } }