Source for file Html.class.php

Documentation is available at Html.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.  * HTML Encode/Decode 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 HTML Encode/Decode Converter Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Converter_Abstract");
  34.  
  35.     
  36.     /** @var int $_quote_style ENT_COMPAT, ENT_QUOTES, ENT_NOQUOTES */
  37.     private $_quote_style = ENT_QUOTES;
  38.     /** @var string $_charset character set */
  39.     private $_charset;
  40.     
  41.     
  42.     
  43.     /**
  44.      * Constructor
  45.      * @param int $quote_style 
  46.      * @param string $charset 
  47.      */
  48.     public function __construct ($quote_style=null$charset=null{
  49.         if (!is_null ($quote_style)) $this->setQuoteStyle ($quote_style)}
  50.         if (!is_null ($charset)) $this->setCharset ($charset)}
  51.     }
  52.     
  53.     
  54.     
  55.     /** ACTION METHODS **/
  56.     /**
  57.      * Encodes the HTML string
  58.      * @param string $txt 
  59.      * @return string 
  60.      * @throws Gumbo_Exception
  61.      */
  62.     public function toEncode ($txt{
  63.         try {
  64.             // verify precondition
  65.             if (!is_string ($txt)) {
  66.                 throw new Gumbo_Exception ("Invalid Argument 'txt:str' => {$txt}:gettype ($txt));
  67.             }
  68.             
  69.             return htmlentities ($this->decode ($txt)$this->getQuoteStyle ()$this->getCharset ());
  70.         catch (Gumbo_Exception $e{
  71.             $e->setFunction (__METHOD__);
  72.             gumbo_trigger ($e);
  73.         }
  74.         return null;
  75.     }
  76.     
  77.     /**
  78.      * Decodes the HTML string
  79.      * @param string $txt 
  80.      * @return string 
  81.      * @throws Gumbo_Exception
  82.      */
  83.     public function toDecode ($txt{
  84.         try {
  85.             // verify precondition
  86.             if (!is_string ($txt)) {
  87.                 throw new Gumbo_Exception ("Invalid Argument 'txt:str' => {$txt}:gettype ($txt));
  88.             }
  89.             
  90.             return html_entity_decode ($txt$this->getQuoteStyle ()$this->getCharset ());
  91.         catch (Gumbo_Exception $e{
  92.             $e->setFunction (__METHOD__);
  93.             gumbo_trigger ($e);
  94.         }
  95.     }
  96.     
  97.     
  98.     
  99.     /** MUTATOR METHODS **/
  100.     /**
  101.      * Sets the Quote Style
  102.      * @param int $style ENT_QUOTES, ENT_COMPAT, ENT_NOQUOTES
  103.      * @throws Gumbo_Exception
  104.      */
  105.     public function setQuoteStyle ($style{
  106.         try {
  107.             if (!is_int ($style)) {
  108.                 throw new Gumbo_Exception ("Invalid Argument 'style:int' => {$style}:gettype ($style));
  109.             }
  110.             
  111.             switch ($style{
  112.                 case ENT_COMPAT break;
  113.                 case ENT_QUOTES break;
  114.                 case ENT_NOQUOTES break;
  115.                 default : throw new Gumbo_Exception ("Invalid Argument ENT_Value: {$style}");
  116.             }
  117.             
  118.             $this->_quote_style = $style;
  119.         catch (Gumbo_Exception $e{
  120.             $e->setFunction (__METHOD__);
  121.             gumbo_trigger ($e);
  122.         }
  123.     }
  124.     
  125.     /**
  126.      * Sets the character set
  127.      * @param string $charset 
  128.      * @throws Gumbo_Exception
  129.      */
  130.     public function setCharset ($charset{
  131.         try {
  132.             // verify precondition
  133.             if (!is_string ($charset)) {
  134.                 throw new Gumbo_Exception ("Invalid Argument 'charset:str' => {$charset}:gettype ($charset));
  135.             }
  136.             
  137.             $this->_charset = $charset;
  138.         catch (Gumbo_Exception $e{
  139.             $e->setFunction (__METHOD__);
  140.             gumbo_trigger ($e);
  141.         }
  142.     }
  143.     
  144.     
  145.     
  146.     /** ACCESSOR METHODS **/
  147.     /**
  148.      * Returns the Quote Style
  149.      * @return int 
  150.      */
  151.     public function getQuoteStyle ({
  152.         return $this->_quote_style;
  153.     }
  154.     
  155.     /**
  156.      * Returns the Character Set
  157.      * @return string 
  158.      */
  159.     public function getCharset ({
  160.         return $this->_charset;
  161.     }
  162.     
  163. }
  164.  
  165. ?>