Source for file Array.class.php

Documentation is available at Array.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Converter
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Array Converter Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Converter
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Array Converter Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Converter_Abstract");
  34.  
  35.     
  36.     /** ACTION METHODS **/
  37.     /**
  38.      * Returns a Composite
  39.      * @param array $data array data
  40.      * @return Gumbo_Interface_Composite 
  41.      * @throws Gumbo_Exception
  42.      * @uses Gumbo_Branch
  43.      */
  44.     public function toComposite ($data{
  45.         try {
  46.             // verify precondition
  47.             if (!is_array ($data)) {
  48.                 throw new Gumbo_Exception ("Invalid Argument 'data:arr' => {$data}:gettype ($data));
  49.             }
  50.             
  51.             return $this->_getComposite ($data);
  52.         catch (Gumbo_Exception $e{
  53.             $e->setFunction (__METHOD__);
  54.             gumbo_trigger ($e);
  55.         }
  56.         return new Gumbo_Branch ();
  57.     }
  58.     
  59.     
  60.     
  61.     /** ACCESSOR METHODS **/
  62.     /**
  63.      * Returns a single Composite object from an array
  64.      * This is a recusive method
  65.      * @param array $data 
  66.      * @param string $name name of the Composite
  67.      * @return Gumbo_Interface_Composite 
  68.      * @throws Gumbo_Exception
  69.      * @uses Gumbo_Interface_Composite
  70.      */
  71.     private function _getComposite ($data$name=null{
  72.         try {
  73.             // verify precondition
  74.             if (!is_array ($data)) {
  75.                 throw new Gumbo_Exception ("Invalid Argument 'data:arr' => {$data}:gettype ($data));
  76.             }
  77.             try {
  78.                 if (!is_null ($name&& !is_string ($name&& !is_numeric ($name)) {
  79.                     throw new Gumbo_Exception ("Invalid Argument 'name:num|str|null' => {$name}:gettype ($name));
  80.                 }
  81.             catch (Gumbo_Exception $e{
  82.                 $e->setFunction (__METHOD__);
  83.                 gumbo_trigger ($e);
  84.                 $name null;
  85.             }
  86.             
  87.             $tree new Gumbo_Branch ($name);
  88.             
  89.             // loop through the array
  90.             foreach ($data as $key=>$val{
  91.                 if (is_array ($val)) {
  92.                     $tmp $this->_getTree ($val$key);
  93.                     if ($tmp instanceof Gumbo_Interface_Composite{
  94.                         $tree->add ($tmp);
  95.                     }
  96.                 else {
  97.                     $tree->add (new Gumbo_Leaf ($key$val));
  98.                 }
  99.             }
  100.             
  101.             return $tree;
  102.         catch (Gumbo_Exception $e{
  103.             $e->getFunction (__METHOD__);
  104.             gumbo_trigger ($e);
  105.         }
  106.         return null;
  107.     }
  108.     
  109. }
  110.  
  111. ?>