Source for file Setting.class.php

Documentation is available at Setting.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 Setting
  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.  * Setting Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Setting
  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 Setting Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Setting");
  34. gumbo_load ("List");
  35.  
  36. class Gumbo_Setting extends Gumbo_List implements Gumbo_Interface_Setting {
  37.     
  38.     /**
  39.      * Constructor
  40.      * @param array $args key=>val pairs
  41.      */
  42.     public function __construct ($args=null) {
  43.         if (is_null ($args)) {
  44.             $args = array ();
  45.         }
  46.         if (is_array ($args) && count ($args) > 0) {
  47.             $this->set ($args);
  48.         }
  49.     }
  50.     
  51.     
  52.     
  53.     /** ACTION METHODS **/
  54.     /**
  55.      * Adds a pair value to the setting
  56.      * @postcondition remove all non-alphanumeric (excluding underscores) characters from $key
  57.      * @param mixed $data 
  58.      * @param string $key (required)
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function add ($data, $key=null) {
  62.         try {
  63.             // verify precondition
  64.             if (!is_string ($key)) {
  65.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:" . gettype ($key));
  66.             }
  67.             
  68.             $key = ereg_replace ("[^A-Za-z0-9_]", "", $key);
  69.             $this->_list [$key] = $val;
  70.         } catch (Gumbo_Exception $e) {
  71.             $e->setFunction (__METHOD__);
  72.             gumbo_trigger ($e);
  73.         }
  74.     }
  75.     
  76.     /**
  77.      * Removes a value from the List (not used)
  78.      * @param mixed $data 
  79.      */
  80.     public function removeValue ($data) {
  81.         return;
  82.     }
  83.     
  84.     
  85.     
  86.     /** MUTATOR METHODS **/
  87.     /**
  88.      * Sets the setting values
  89.      * @param string|array$data (string represents a 'key') associative array of key=>val pairs
  90.      * @param mixed $val 
  91.      * @throws Gumbo_Exception
  92.      */
  93.     public function set ($data, $val=null) {
  94.         try {
  95.             // verify precondition
  96.             if (!is_string ($data) && !is_array ($data)) {
  97.                 throw new Gumbo_Exception ("Invalid Argument 'data:str|arr' => {$data}:" . gettype ($data));
  98.             }
  99.             
  100.             // recursive call
  101.             if (is_array ($data)) {
  102.                 foreach ($data as $key=>$val) {
  103.                     if (is_array ($val)) {
  104.                         $this->set ($val);
  105.                     } else {
  106.                         $this->set ($key, $val);
  107.                     }
  108.                 }
  109.                 return;
  110.             }
  111.             
  112.             $this->add ($val, $data);
  113.         } catch (Gumbo_Exception $e) {
  114.             $e->setFunction (__METHOD__);
  115.             gumbo_trigger ($e);
  116.         }
  117.     }
  118.     
  119. }
  120.  
  121. ?>