Tree
[ class tree: Tree ] [ index: Tree ] [ all elements ]

Source for file Array.php

Documentation is available at Array.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. //  $Id: Array.php 320703 2011-12-08 22:08:40Z danielc $
  20.  
  21. require_once 'Tree/Error.php';
  22.  
  23. /**
  24.  * EXPERIMENTAL
  25.  *
  26.  * @access     public
  27.  * @author     Wolfram Kriesing <wolfram@kriesing.de>
  28.  * @version    2002/08/30
  29.  * @package    Tree
  30.  */
  31. {
  32.  
  33.     var $data = array();
  34.  
  35.     /**
  36.      * this is the internal id that will be assigned if no id is given
  37.      * it simply counts from 1, so we can check if($id) i am lazy :-)
  38.      */
  39.     var $_id = 1;
  40.  
  41.     // {{{ Tree_Memory_Array()
  42.  
  43.     /**
  44.      * set up this object
  45.      *
  46.      * @version    2002/08/30
  47.      * @access     public
  48.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  49.      * @param      string  $dsn    the path on the filesystem
  50.      * @param      array   $options  additional options you can set
  51.      */
  52.     function Tree_Memory_Array(&$array$options = array())
  53.     {
  54.         $this->_array &$array;
  55.         $this->_options $options// not in use currently
  56.     }
  57.  
  58.     // }}}
  59.     // {{{ setup()
  60.  
  61.     /**
  62.      *
  63.      *
  64.      * @version    2002/08/30
  65.      * @access     public
  66.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  67.      * @return     boolean     true on success
  68.      */
  69.     function setup()
  70.     {
  71.         unset($this->data);
  72.         if (is_array($this->_array)) {
  73.             $this->data[0= null;
  74.             $theData = array(&$this->_array);
  75.             $this->_setup($theData);
  76.         }
  77.         return $this->data;
  78.     }
  79.  
  80.     // }}}
  81.     // {{{ _setup()
  82.  
  83.     /**
  84.      * we modify the $this->_array in here, we also add the id
  85.      * so methods like 'add' etc can find the elements they are searching for,
  86.      * if you dont like your data to be modified dont pass them as reference!
  87.      */
  88.     function _setup(&$array$parentId = 0)
  89.     {
  90.         foreach ($array as $nodeKey => $aNode{
  91.             $newData $aNode;
  92.             // if the current element has no id, we generate one
  93.             if (!isset($newData['id']|| !$newData['id']{
  94.                 // build a unique numeric id
  95.                 $newData['id'$this->_id++;
  96.                 // set the id
  97.                 $array[$nodeKey]['id'$newData['id'];
  98.             else {
  99.                 $idAsInt = (int)$newData['id'];
  100.                 if ($idAsInt $this->_id{
  101.                     $this->_id $idAsInt;
  102.                 }
  103.             }
  104.             // set the parent-id, since we only have a 'children' array
  105.             $newData['parentId'$parentId;
  106.             $children = null;
  107.             // remove the 'children' array, since this is only info for
  108.             // this class
  109.             foreach ($newData as $key => $val{
  110.                 if ($key == 'children'{
  111.                     unset($newData[$key]);
  112.                 }
  113.             }
  114.  
  115.             $this->data[$newData['id']] $newData;
  116.             if (isset($aNode['children']&& $aNode['children']{
  117.                 if (!isset($array[$nodeKey]['children'])) {
  118.                     $array[$nodeKey]['children'= array();
  119.                 }
  120.                 $this->_setup($array[$nodeKey]['children']$newData['id']);
  121.             }
  122.         }
  123.     }
  124.  
  125.  
  126.     // }}}
  127.     // {{{ setData()
  128.  
  129.     /**
  130.      * this is mostly used by switchDataSource
  131.      * this method put data gotten from getNode() in the $this->_array
  132.      *
  133.      */
  134.     function setData($data)
  135.     {
  136.         $unsetKeys = array('childId''left''right');
  137.  
  138.         foreach ($data as $aNode{
  139.             foreach ($aNode as $key => $val{
  140.                 if (is_array($val|| in_array($key,$unsetKeys)) {
  141.                     unset($aNode[$key]);
  142.                 }
  143.             }
  144.             $this->add($aNode,$aNode['parentId']);
  145.         }
  146.         $this->_array $this->_array['children'][0];
  147.     }
  148.  
  149.     // }}}
  150.     // {{{ _prepareResults()
  151.  
  152.     /**
  153.      * prepare multiple results
  154.      *
  155.      * @see        _prepareResult()
  156.      * @access     private
  157.      * @version    2002/03/03
  158.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  159.      */
  160.     function _prepareResults($results)
  161.     {
  162.         $newResults = array();
  163.         foreach($results as $aResult{
  164.             $newResults[$this->_prepareResult($aResult);
  165.         }
  166.         return $newResults;
  167.     }
  168.  
  169.     // }}}
  170.     // {{{ _prepareResult()
  171.  
  172.     /**
  173.      * map back the index names to get what is expected
  174.      *
  175.      * @access     private
  176.      * @version    2002/03/03
  177.      * @author     Wolfram Kriesing <wolfram@kriesing.de>
  178.      */
  179.     function _prepareResult($result)
  180.     {
  181.         $map $this->getOption('columnNameMaps');
  182.  
  183.         if ($map{
  184.             foreach($map as $key => $columnName{
  185.                 $result[$key$result[$columnName];
  186.                 unset($result[$columnName]);
  187.             }
  188.         }
  189.         return $result;
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ add()
  194.  
  195.     /**
  196.      * add a new item to the tree
  197.      * what is tricky here, we also need to add it to the source array
  198.      *
  199.      * @param  array   the data for the new node
  200.      * @param  int     the ID of the parent node
  201.      * @param  int     the ID of the previous node
  202.      */
  203.     function add($data$parentId$previousId = null)
  204.     {
  205.         if (!isset($data['id'])) {
  206.             $data['id'= ++$this->_id;
  207.         elseif((int)$data['id'$this->_id{
  208.             // Since we dont want to overwrite anything. just in case update
  209.             // the $this->_id if the data['id'] has a higher number.
  210.             $this->_id = (int)$data['id'];
  211.         }
  212.         $data['parentId'$parentId;
  213.         $this->data[$data['id']] $data;
  214.  
  215.         // there might not be a root element yet
  216.         if (!isset($this->_array['children'])) {
  217.             $data['parentId'= 0;
  218.             $this->_array['children'][$data;
  219.         else {
  220.             array_walk($this->_array['children'],
  221.                         array(&$this,'_add'),
  222.                         array($data,$parentId,$previousId)
  223.                     );
  224.         }
  225.         return $data['id'];
  226.     }
  227.  
  228.     // }}}
  229.     // {{{ _add()
  230.  
  231.     /**
  232.      * we need to add the node to the source array
  233.      * for this we have this private method which loops through
  234.      * the source array and adds it in the right place
  235.      *
  236.      * @param  mixed   the value of the array, as a reference. So we work
  237.      *                  right on the source
  238.      * @param  mixed   the key of the node
  239.      * @param  array   an array which contains the following
  240.      *                  new data,
  241.      *                  parent ID under which to add the node,
  242.      *                  the prvious ID
  243.      */
  244.     function _add(&$val$key$data)
  245.     {
  246.         // is the id of the current elment ($val) == to the parentId ($data[1])
  247.         if ($val['id'== $data[1]{
  248.             if (isset($data[2]&& $data[2=== 0{
  249.                 // if the previousId is 0 means, add it as the first member
  250.                 $val['children'array_merge(array($data[0])$val['children']);
  251.             else {
  252.                 $val['children'][$data[0];
  253.             }
  254.         else {        // if we havent found the new element go on searching
  255.             if (isset($val['children'])) {
  256.                 array_walk($val['children'],array(&$this,'_add'),$data);
  257.             }
  258.         }
  259.     }
  260.  
  261.     // }}}
  262.     // {{{ update()
  263.  
  264.     /**
  265.      * update an entry with the given id and set the data as given
  266.      * in the array $data
  267.      *
  268.      * @param  int     the id of the element that shall be updated
  269.      * @param  array   the data, [key]=>[value]
  270.      * @return void 
  271.      */
  272.     function update($id,$data)
  273.     {
  274.         if ($this->_array['id']==$id{
  275.             foreach ($data as $key=>$newVal{
  276.                 $this->_array[$key$newVal;
  277.             }
  278.         else {
  279.             array_walk($this->_array['children'],
  280.                         array(&$this,'_update'),
  281.                         array($id,$data)
  282.                     );
  283.         }
  284.     }
  285.  
  286.     // }}}
  287.     // {{{ _update()
  288.  
  289.     /**
  290.      * update the element with the given id
  291.      *
  292.      * @param  array    a reference to an element inside $this->_array
  293.      *                   has to be a reference, so we can really modify
  294.      *                   the actual data
  295.      * @param  int      not in use, but array_walk passes this param
  296.      * @param  array    [0] is the id we are searching for
  297.      *                   [1] are the new data we shall set
  298.      * @return void 
  299.      */
  300.     function _update(&$val$key$data)
  301.     {
  302.         // is the id of the current elment ($val) == to the parentId ($data[1])
  303.         if ($val['id'== $data[0]{
  304.             foreach ($data[1as $key => $newVal{
  305.                 $val[$key$newVal;
  306.             }
  307.         else {
  308.             // if we havent found the new element go on searching
  309.             // in the children
  310.             if (isset($val['children'])) {
  311.                 array_walk($val['children'],array(&$this,'_update')$data);
  312.             }
  313.         }
  314.     }
  315.  
  316.     // }}}
  317.     // {{{ remove()
  318.  
  319.     /**
  320.      * remove an element from the tree
  321.      * this removes all the children too
  322.      *
  323.      * @param  int the id of the element to be removed
  324.      */
  325.     function remove($id)
  326.     {
  327.         // we only need to search for element that do exist :-)
  328.         // otherwise we save some processing time
  329.         if ($this->data[$id]{
  330.             $this->_remove($this->_array,$id);
  331.         }
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ _remove()
  336.  
  337.     /**
  338.      * remove the element with the given id
  339.      * this will definitely remove all the children too
  340.      *
  341.      * @param  array    a reference to an element inside $this->_array
  342.      *                   has to be a reference, so we can really modify
  343.      *                   the actual data
  344.      * @param  int      the id of the element to be removed
  345.      * @return void 
  346.      */
  347.     function _remove(&$val$id)
  348.     {
  349.         if (isset($val['children'])) {
  350.             foreach ($val['children'as $key => $aVal{
  351.                 if ($aVal['id'== $id{
  352.                     if (sizeof($val['children']< 2{
  353.                         unset($val['children']);
  354.                     else {
  355.                         unset($val['children'][$key]);
  356.                     }
  357.                 else {
  358.                     $this->_remove($val['children'][$key],$id);
  359.                 }
  360.             }
  361.         }
  362.     }
  363.  
  364.     // }}}
  365. }

Documentation generated on Mon, 11 Mar 2019 15:47:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.