Source for file Converter.class.php

Documentation is available at Converter.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.  * Converter Class
  22.  * 
  23.  * This class is responsible for loading the appropriate Converter object.  The name
  24.  * given must be a Gumbo_Interface_Converter, otherwise the returned value will be
  25.  * the Blank Converter.  The Blank Converter simply returns the data given.  If the
  26.  * name given is not a class, the Factory will attempt to search for a Gumbo_Converter_*
  27.  * class.  Since the Factory will always return a Converter object, the information
  28.  * can be gathered as:
  29.  * <pre>
  30.  * $data = $fact->factory ("Array", ... )->convert ($data_array, "composite");
  31.  * </pre>
  32.  *
  33.  * @category Gumbo
  34.  * @package Converter
  35.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  36.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  37.  * @author Michael Luster <mluster79@yahoo.com>
  38.  * @link http://sourceforge.net/projects/phpgumbo
  39.  * @desc Converter Class
  40.  * @version 0.0.1
  41.  */
  42.  
  43. gumbo_load ("Factory");
  44. gumbo_load ("Converter_Blank");
  45.  
  46. class Gumbo_Converter extends Gumbo_Factory {
  47.     
  48.     /** ACTION METHODS **/
  49.     /**
  50.      * Returns an instantiated object (accept Constructor arguments)
  51.      * @param string|int$name name of Class or key string
  52.      * @param mixed $args additional arguments
  53.      * @return Gumbo_Interface_Converter 
  54.      * @throws Gumbo_Exception
  55.      * @uses Gumbo_Converter_Blank
  56.      */
  57.     public function factory ($name=null$args=null{
  58.         try {
  59.             // prepend Gumbo_Converter_ to the name if class not found
  60.             if (is_string ($name&& !class_exists ($namefalse)) {
  61.                 $name "Gumbo_Converter_" ucfirst (str_replace ("Gumbo_Converter_"""$name));
  62.             }
  63.             
  64.             $args array ();
  65.             $args [$name;
  66.             foreach (func_get_args (as $key=>$val{
  67.                 if ($key == 0continue}
  68.                 $args [$val;
  69.             }
  70.             
  71.             // retrieve an object
  72.             $obj call_user_func_array (array ("parent""factory")$args);
  73.             
  74.             if (!($obj instanceof Gumbo_Interface_Converter)) {
  75.                 throw new Gumbo_Exception ("Invalid Converter: " get_class ($obj));
  76.             }
  77.             
  78.             return $obj;
  79.         catch (Gumbo_Exception $e{
  80.             $e->setFunction (__METHOD__);
  81.             gumbo_trigger ($e);
  82.         }
  83.         return new Gumbo_Converter_Blank ();
  84.     }
  85.     
  86.     
  87.     
  88.     /** STATIC METHODS **/
  89.     /**
  90.      * Converts new line characters into HTML line breaks
  91.      * @param string $txt 
  92.      * @return string 
  93.      * @uses Gumbo_Converter_Factory
  94.      */
  95.     public static function nl2br ($txt{
  96.         $fact new Gumbo_Converter ();
  97.         return $fact->factory ("NewLineBreak")->convert ($txt"br");
  98.     }
  99.     
  100.     /**
  101.      * Converts HTML line breaks into new line characters
  102.      * @param string $txt 
  103.      * @param string $eol End of Line characters (default PHP_EOL)
  104.      * @return string 
  105.      */
  106.     public static function br2nl ($txt$eol=PHP_EOL{
  107.         $fact new Gumbo_Converter ();
  108.         return $fact->factory ("NewLineBreak"$eol)->convert ($txt"nl");
  109.     }
  110.     
  111.     
  112.     
  113.     /**
  114.      * Converts the characters into HTML entities, including single-double quotes
  115.      * @param string $txt 
  116.      * @param int $quote_style ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES
  117.      * @param string $charset character set
  118.      * @return string 
  119.      */
  120.     public static function htmlencode ($txt$quote_style=ENT_QUOTES$charset=null{
  121.         $fact new Gumbo_Converter ();
  122.         return $fact->factory ("Html"$quote_style$charset)->convert ($txt"encode");
  123.     }
  124.     
  125. /**
  126.      * Converts the HTML entities, including single-double quotes, into a characters
  127.      * @param string $txt 
  128.      * @param int $quote_style ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES
  129.      * @param string $charset character set
  130.      * @return string 
  131.      */
  132.     public static function htmldecode ($txt$quote_style=ENT_QUOTES$charset=null{
  133.         $fact new Gumbo_Converter ();
  134.         return $fact->factory ("Html"$quote_style$charset)->convert ($txt"decode");
  135.     }
  136.     
  137.     
  138.     
  139.     /**
  140.      * Adds slashes to the string
  141.      * @param string $txt 
  142.      * @return string 
  143.      */
  144.     public static function addslashes ($txt{
  145.         $fact new Gumbo_Converter ();
  146.         return $fact->factory ("Slashes")->convert ($txt"add");
  147.     }
  148.     
  149.     /**
  150.      * Strips slashes to the string
  151.      * @param string $txt 
  152.      * @return string 
  153.      */
  154.     public static function stripslashes ($txt{
  155.         $fact new Gumbo_Converter ();
  156.         return $fact->factory ("Slashes")->convert ($txt"strip");
  157.     }
  158.     
  159. }
  160.  
  161. ?>