Source for file Abstract.class.php

Documentation is available at Abstract.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.  * Abstract Converter Class
  22.  * 
  23.  * The Abstract Converter defines a particular set of rules to follow.  These
  24.  * will indicate how to extend the class and utilize a dynamic 'convert' method.
  25.  * The idea is centered around 'to' methods.  A 'to' method provides implementation
  26.  * to change a data type to something else.  There can be multiple 'to' methods
  27.  * inside a Converter class.  The method name (excluding 'to') will be passed into
  28.  * the 'to' argument.  The method will be executed, returning the results.
  29.  * 
  30.  * For example, the Converter_Array class defines a single 'to' method (toComposite).
  31.  * This will take an array and convert it into a Composite object.
  32.  * <pre>
  33.  * $convert = new Gumbo_Converter_Array ();
  34.  * $composite = $convert->convert ($data_array, "composite");
  35.  * </pre>
  36.  * 
  37.  * The idea is to define a class of a certain type (Array), and provide 'to' methods
  38.  * to change the type into different formats.
  39.  *
  40.  * @category Gumbo
  41.  * @package Converter
  42.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  43.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  44.  * @author Michael Luster <mluster79@yahoo.com>
  45.  * @link http://sourceforge.net/projects/phpgumbo
  46.  * @desc Abstract Converter Class
  47.  * @version 0.0.1
  48.  */
  49.  
  50. gumbo_load ("Interface_Converter");
  51.  
  52. abstract class Gumbo_Converter_Abstract implements Gumbo_Interface_Converter {
  53.     
  54.     /**
  55.      * Converts one variable type to another
  56.      * @param mixed $data 
  57.      * @param string $to action to perform (required)
  58.      * @return mixed 
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function convert ($data$to=null{
  62.         try {
  63.             // verify precondition
  64.             if (!is_string ($data)) {
  65.                 throw new Gumbo_Exception ("Invalid Argument 'data:str' => {$data}:gettype ($data));
  66.             }
  67.             if (!is_string ($to)) {
  68.                 throw new Gumbo_Exception ("Invalid Argument 'to:str' => {$to}:gettype ($to));
  69.             }
  70.             if (substr ($to02!== "to"$to "to" ucfirst ($to)}
  71.             if (!method_exists ($this$to)) {
  72.                 throw new Gumbo_Exception ("Converter Action Method Not Found: {$to}");
  73.             }
  74.             
  75.             return $this->$to ($data);
  76.         catch (Gumbo_Exception $e{
  77.             $e->setFunction (__METHOD__);
  78.             gumbo_trigger ($e);
  79.         }
  80.         return null;
  81.     }
  82.     
  83. }
  84.  
  85. ?>