Source for file Item.class.php

Documentation is available at Item.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 Template
  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.  * Template List Item Class (for producing <li>...</li> tags)
  22.  *
  23.  * @category Gumbo
  24.  * @package Template
  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 Template List Item Class (for producing <li>...</li> tags)
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Template_List");
  34.  
  35.     
  36.     /** ACTION METHODS **/
  37.     /**
  38.      * Returns the formatted text to the browser
  39.      * @param bool $reload reloads the output (not used)
  40.      * @return string 
  41.      */
  42.     public function fetch ($reload=false{
  43.         $txt null;
  44.  
  45.         $num count ($this->getElements ()) 1;
  46.         foreach ($this->getElements (as $key=>$val{
  47.             $txt .= "<li";
  48.             if ($key == 0$txt .= " class=\"first\""}
  49.             if ($num && $key == $num$txt .= " class=\"last\""}
  50.             $txt .= ">" $val "</li>";
  51.         }
  52.  
  53.         return $txt;
  54.     }
  55.     
  56.     /**
  57.      * Adds data to the end of the list
  58.      * @param args[] $data 
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function add ($data{
  62.         try {
  63.             // verify precondition
  64.             if (!is_string ($data&& !is_numeric ($data&& !is_array ($data)) {
  65.                 throw new Gumbo_Exception ("Invalid Argument 'data:str|num|arr' => {$data}:gettype ($data));
  66.             }
  67.             
  68.             if (func_num_args (1{
  69.                 $data func_get_args ();
  70.             }
  71.             
  72.             // recursive method call
  73.             if (is_array ($data)) {
  74.                 foreach ($data as $val{
  75.                     $this->add ($val);
  76.                 }
  77.                 return;
  78.             }
  79.             
  80.             $this->_elements [$data;
  81.         catch (Gumbo_Exception $e{
  82.             $e->setFunction (__METHOD__);
  83.             gumbo_trigger ($e);
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * Places the data at the specified location
  89.      * @param int $position 
  90.      * @param args[] $data 
  91.      * @throws Gumbo_Exception
  92.      */
  93.     public function place ($position$data{
  94.         try {
  95.             // verify precondition
  96.             if (!is_numeric ($position)) {
  97.                 throw new Gumbo_Exception ("Invalid Argument 'position:int' => {$position}:gettype ($position));
  98.             }
  99.             if (!is_string ($data&& !is_numeric ($data&& !is_array ($data)) {
  100.                 throw new Gumbo_Exception ("Invalid Argument 'data:num|str|arr' => {$data}:gettype ($data));
  101.             }
  102.             $position = (int) $position;
  103.             
  104.             // loading extra arguments
  105.             if (func_num_args (2{
  106.                 $data array ();
  107.                 foreach (func_get_args (as $key=>$val{
  108.                     if ($key == 0continue}
  109.                     $data [$val;
  110.                 }
  111.             }
  112.             
  113.             // adds to the end if position is out of range
  114.             if ($position || $position count ($this->getElements ())) {
  115.                 $this->add ($data);
  116.                 return;
  117.             }
  118.             
  119.             // recursize method call
  120.             if (is_array ($data)) {
  121.                 foreach ($data as $val{
  122.                     $this->place ($position$val);
  123.                     $position++;
  124.                 }
  125.                 return;
  126.             }
  127.             
  128.             // place the data in the right position
  129.             $tmp $this->getElements ();
  130.             $this->_elements = array ();
  131.             foreach ($tmp as $key=>$val{
  132.                 if ($position == $key+1{
  133.                     $this->_elements [$data;
  134.                 }
  135.                 $this->_elements [$val;
  136.             }
  137.         catch (Gumbo_Exception $e{
  138.             $e->setFunction (__METHOD__);
  139.             gumbo_trigger ($e);
  140.         }
  141.     }
  142.     
  143. }
  144.  
  145. ?>