hi Irina,

On 9/28/06, Irina Khmelinina < [EMAIL PROTECTED]> wrote:
Hi, Ahmed.

Yes, I already did it. But it seems to me this peculiarity of
Zend_Config isn't good thing.

yes, this have to be better.
I hacked the class and added a check to see if the false is a value in the array or that's returned by next() at the end of the array.
what I did is highlighted.
- in __construct() and __get()  replaced the boolean false to a special string ' ZF~FALSE' . and vice-versa in
__set()

class Zend_Config implements Countable, Iterator
{
    /**
     * Whether in-memory modifications to configuration data are allowed
     *
     * @var boolean
     */
    protected $_allowModifications;

    /**
     * Whether the iteration pointer is valid
     *
     * @var boolean
     */
    protected $_iterationPointerValid;

    /**
     * Contains array of configuration data
     *
     * @var array
     */
    protected $_data;

    /**
     * Zend_Config provides a property based interface to
     * an array. The data are read-only unless $allowModifications
     * is set to true on construction.
     *
     * Zend_Config also implements Countable and Iterator to
     * facilitate easy access to the data.
     *
     * @param array $array
     * @param boolean $allowModifications
     * @throws Zend_Config_Exception
     */
    public function __construct($array, $allowModifications = false)
    {
        $this->_allowModifications = (boolean) $allowModifications;
        $this->_data = array();
        foreach ($array as $key => $value) {
            if ($this->_isValidKeyName($key)) {
                if (is_array($value)) {
                    $this->_data[$key] = new Zend_Config($value, $this->_allowModifications);
                } else {
                        if($value === false) {
                           $this->_data[$key] = ' ZF~FALSE';
                        } else {
                            $this->_data[$key] = $value;
                        }
                }
            } else {
                throw new Zend_Config_Exception("Invalid key: '$key'");
            }
        }
    }

    /**
     * Ensure that the key is a valid PHP property name
     *
     * @param string $key
     * @return boolean
     */
    protected function _isValidKeyName($key)
    {
        return (bool) preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key);
    }

    /**
     * Magic function so that $obj->value will work.
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        $result = null;
        if (isset($this->_data[$name])) {
            if( $this->_data[$name] === 'ZF~FALSE' ) {
               $result = false ;
            } else {
               $result = $this->_data[$name];
            }
        }
        return $result;
    }

    /**
     * Only allow setting of a property if $allowModifications
     * was set to true on construction. Otherwise, throw an exception.
     *
     * @param string $name
     * @param mixed $value
     * @throws Zend_Config_Exception
     */
    public function __set($name, $value)
    {
        if ($this->_allowModifications) {
            if (is_array($value)) {
                $this->_data[$name] = new Zend_Config($value, true);
            } else {
                if($value === false) {
                   $this->_data[$name] = 'ZF~FALSE';
                } else {
                    $this->_data[$name] = $value;
                }
            }
        } else {
            throw new Zend_Config_Exception('Zend_Config is read only');
        }
    }

    /**
     * Return an associative array of the stored data.
     *
     * @return array
     */
    public function asArray()
    {
        $array = array();
        foreach ($this->_data as $key=>$value) {
            if (is_object($value)) {
                $array[$key] = $value->asArray();
            } else {
                $array[$key] = $value;
            }
        }
        return $array;
    }

    /**
     * Support isset() overloading on PHP 5.1
     *
     * @param string $name
     * @return boolean
     */
    protected function __isset($name)
    {
        return isset($this->_data[$name]);
    }

    /**
     * Defined by Countable interface
     *
     * @return int
     */
    public function count()
    {
        return count($this->_data);
    }

    /**
     * Defined by Iterator interface
     *
     * @return mixed
     */
    public function current()
    {
        return current($this->_data);
    }

    /**
     * Defined by Iterator interface
     *
     * @return mixed
     */
    public function key()
    {
        return key($this->_data);
    }

    /**
     * Defined by Iterator interface
     *
     */
    public function next()
    {
        if (next($this->_data) === false) {
            $this->_iterationPointerValid = false;
        } else {
            $this->_iterationPointerValid = true;
        }
    }

    /**
     * Defined by Iterator interface
     *
     */
    public function rewind()
    {
        reset($this->_data);
        $this->_iterationPointerValid = true;
    }

    /**
     * Defined by Iterator interface
     *
     * @return boolean
     */
    public function valid()
    {
        return $this->_iterationPointerValid;
    }

}

I think that this is the only solution, what do you think ?

Best Regards
--
Ahmed Shreef
Egypt

Reply via email to