Convert into Values in Zend_Config_Ini ?!?!?

May 3, 2010 by Reboot · Leave a Comment 

Anyone know why the

“convert the current values in $config into an array”

exists in Zend_Config_Ini (ZF 1.10.4)?

/**
     * Assign the key's value to the property list. Handles the
     * nest separator for sub-properties.
     *
     * @param  array  $config
     * @param  string $key
     * @param  string $value
     * @throws Zend_Config_Exception
     * @return array
     */
    protected function _processKey($config, $key, $value)
    {
    	/*
		var_dump($config);
		var_dump($key);
		var_dump($value);
		echo '--';
		*/    	

        if (strpos($key, $this->_nestSeparator) !== false) {
            $pieces = explode($this->_nestSeparator, $key, 2);
            if (strlen($pieces[0]) && strlen($pieces[1])) {
                if (!isset($config[$pieces[0]])) {
                    if ($pieces[0] === '0' && !empty($config)) {
                        // convert the current values in $config into an array

                        /* Example ini section.
                        	[initest]
							thats.nice = 'then'
							thats.0.converted = 'ok'
                         */
                    	echo 'converted, but why';

                    	$config = array($pieces[0] => $config);
                    } else {
                        $config[$pieces[0]] = array();
                    }
                } elseif (!is_array($config[$pieces[0]])) {
                    /**
                     * @see Zend_Config_Exception
                     */
                    require_once 'Zend/Config/Exception.php';
                    throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
                }
                $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
            } else {
                /**
                 * @see Zend_Config_Exception
                 */
                require_once 'Zend/Config/Exception.php';
                throw new Zend_Config_Exception("Invalid key '$key'");
            }
        } else {
            $config[$key] = $value;
        }

        // dump the result
        var_dump($config);

        return $config;
    }

the result is:

array(1) {
  ["thats"]=>
  array(1) {
    [0]=>
    array(2) {
      ["nice"]=>
      string(6) "'then'"
      ["converted"]=>
      string(4) "'ok'"
    }
  }
}

About Reboot
Software Engineer - http://www.linkedin.com/in/orlissenberg

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!