memory.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,354 行 · 第 1/4 页

PHP
1,354
字号
    */    function _walk( $walkFunction , &$curLevel , $returnType )    {        if (sizeof($curLevel)) {            foreach ($curLevel as $key=>$value) {                $ret = call_user_func( $walkFunction , $this->data[$key] );                switch ($returnType) {                    case 'array':   $this->walkReturn[] = $ret;                                    break;                    case 'ifArray': // this only adds the element if the $ret is an array and contains data                                    if (is_array($ret)) {                                        $this->walkReturn[] = $ret;                                    }                                    break;                    default:        $this->walkReturn.= $ret;                                    break;                }                $this->_walk( $walkFunction , $value , $returnType );            }        }        return $this->walkReturn;    } // end of function    /**    *   adds multiple elements    *   you have to pass those elements in a multidimensional array which represents the    *   tree structure as it shall be added (this array can of course also simply contain one element)    *   the following array $x passed as the parameter    *        $x[0] = array(  'name'=>'bla','parentId'=>'30',    *                        array(  'name'=>'bla1','comment'=>'foo',    *                                array('name'=>'bla2'),    *                                array('name'=>'bla2_1')    *                        ),    *                        array(  'name'=>'bla1_1'),    *                        )    *                      );    *        $x[1] = array(  'name'=>'fooBla','parentId'=>'30');    *    *   would add the following tree (or subtree, or node whatever you want to call it)    *   under the parent with the id 30 (since 'parentId'=30 in $x[0] and in $x[1])    *    +--bla    *    |   +--bla1    *    |   |    +--bla2    *    |   |    +--bla2_1    *    |   +--bla1_1    *    +--fooBla    *    *   @see        add()    *   @version    2001/12/19    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      array   $node   the tree to be inserted, represents the tree structure,    *                               see add() for the exact member of each node    *   @return     mixed   either boolean false on failure or the id of the inserted row    */    function addNode( $node )    {        if( sizeof($node) )        foreach( $node as $aNode )        {            $newNode = array();            foreach( $aNode as $name=>$value )      // this should always have data, if not the passed structure has an error            {                if( !is_array($value) )             // collect the data that need to go in the DB                    $newEntry[$name] = $value;                else                                // collect the children                    $newNode[] = $value;            }            $insertedId = $this->add( $newEntry );  // add the element and get the id, that it got, to have the parentId for the children            if( $insertedId!= false )               // if inserting suceeded, we have received the id under which we can insert the children            {                if( sizeof($newNode) )              // if there are children, set their parentId, so they kknow where they belong in the tree                foreach( $newNode as $key=>$aNewNode )                {                    $newNode[$key]['parentId'] = $insertedId;                }                $this->addNode( $newNode );         // call yourself recursively to insert the children, and its children and ...            }        }    } // end of function    /**    *   gets the path to the element given by its id    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *    *   @access     public    *   @version    2001/10/10    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the path for    *   @return     array   this array contains all elements from the root to the element given by the id    *    */    function getPath( $id )    {        $path = array();                            // empty the path, to be clean// FIXXME may its better to use a for(level) to count down,// since a while is always a little risky        while( @$this->data[$id]['parent'] )        // until there are no more parents        {            $path[] = &$this->data[$id];            // curElement is already a reference, so save it in path            $id = $this->data[$id]['parent']['id']; // get the next parent id, for the while to retreive the parent's parent        }        $path[] = &$this->data[$id];                // dont forget the last one        return array_reverse($path);    } // end of function    /**    *   sets the remove-recursively mode, either true or false    *    *   @version    2001/10/09    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      boolean $newValues set to true if removing a tree level shall remove all it's children and theit children ...    *    */    function setRemoveRecursively( $case=true )    {        $this->removeRecursively = $case;    } // end of function    /**    *    *    *   @version    2002/01/21    *   @access     private    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *    */    function &_getElement( $id , $what='' )    {        if( $what=='' )        {            return $this->data[$id];        }        $elementId = $this->_getElementId( $id , $what );        if( $elementId !== NULL )            return $this->data[$elementId];        return NULL;    // we should not return false, since that might be a value of the element that is requested    } // end of function    /**    *    *    *   @version    2002/01/21    *   @access     private    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *    */    function _getElementId( $id , $what )    {        if( @$this->data[$id][$what] )          // use @ since the key $what might not exist            return $this->data[$id][$what]['id'];        return NULL;    } // end of function    /**    *   gets an element as a reference    *    *   @version    2002/01/21    *   @access     private    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *    */    function &getElement( $id )    {        return $this->_getElement( $id );    }    /**    *    *    *   @version    2002/02/06    *   @access     private    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   either the id of an element or the path to the element    *    */    function getElementContent( $idOrPath , $fieldName )    {        if( is_string($idOrPath) )        {            $id = $this->getIdByPath($idOrPath);        }        return $this->data[$id][$fieldName];    }    /**    *    *    *   @version    2002/02/06    *   @access     private    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *    */    function getElementsContent( $ids , $fieldName )    {// i dont know if this method is not just overloading the file, since it only serves my lazyness// is this effective here? i can also loop in the calling code!?        $fields = array();        if(is_array($ids) && sizeof($ids))        foreach( $ids as $aId )            $fields[] = $this->getElementContent( $aId , $fieldName );        return $fields;    }    /**    *   gets an element given by it's path as a reference    *    *   @version    2002/01/21    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      string  $path       the path to search for    *   @param      integer $startId    the id where to search for the path    *   @param      string  $nodeName   the name of the key that contains the node name    *   @param      string  $seperator  the path seperator    *   @return     integer the id of the searched element    *    */    function &getElementByPath( $path , $startId=0 , $nodeName='name' , $seperator='/' )    {        $id = $this->getIdByPath( $path , $startId );        if( $id )            return $this->getElement( $id );        return NULL;                                // return NULL since false might be interpreted as id 0    }    /**    *   gets an element ID    *    *   @version    2002/01/21    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param    *    *//* we already have a method getIdByPath, which one should we use ????    function &getElementIdByPath( $id )    {        return $this->_getElement( $id );    }*/    /**    *   get the level, which is how far below the root are we?    *    *   @version    2001/11/25    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the level for    *    */    function getLevel( $id )    {        return $this->data[$id]['level'];    } // end of function    /**    *   returns the child if the node given has one    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *    *   @version    2001/11/27    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the child for    *    */    function &getChild( $id )    {        return $this->_getElement( $id , 'child' );    } // end of function    /**    *   returns the child if the node given has one    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *    *   @version    2001/11/27    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the child for    *    */    function &getParent( $id )    {        return $this->_getElement( $id , 'parent' );    } // end of function    /**    *   returns the next element if the node given has one    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *    *   @version    2002/01/17    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the child for    *   @return     mixed   reference to the next element or false if there is none    */    function &getNext( $id )    {        return $this->_getElement( $id , 'next' );    } // end of function    /**    *   returns the previous element if the node given has one    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *    *   @version    2002/02/05    *   @access     public    *   @author     Wolfram Kriesing <wolfram@kriesing.de>    *   @param      mixed   $id     the id of the node to get the child for    *   @return     mixed   reference to the next element or false if there is none    */    function &getPrevious( $id )    {        return $this->_getElement( $id , 'previous' );    } // end of function    /**    *   returns the node for the given id    *   !!! ATTENTION watch out that you never change any of the data returned,    *   since they are references to the internal property $data    *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?