home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Common.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.7 KB  |  465 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Base class for all HTML classes
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  * 
  15.  * @category    HTML
  16.  * @package     HTML_Common
  17.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  18.  * @copyright   2001-2007 The PHP Group
  19.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20.  * @version     CVS: $Id: Common.php,v 1.14 2007/05/16 20:06:44 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_Common/
  22.  */ 
  23.  
  24. /**
  25.  * Base class for all HTML classes
  26.  *
  27.  * @category    HTML
  28.  * @package     HTML_Common
  29.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  30.  * @version     Release: 1.2.4
  31.  * @abstract
  32.  */
  33. class HTML_Common
  34. {
  35.     /**
  36.      * Associative array of attributes
  37.      * @var     array
  38.      * @access  private
  39.      */
  40.     var $_attributes = array();
  41.  
  42.     /**
  43.      * Tab offset of the tag
  44.      * @var     int
  45.      * @access  private
  46.      */
  47.     var $_tabOffset = 0;
  48.  
  49.     /**
  50.      * Tab string
  51.      * @var       string
  52.      * @since     1.7
  53.      * @access    private
  54.      */
  55.     var $_tab = "\11";
  56.  
  57.     /**
  58.      * Contains the line end string
  59.      * @var       string
  60.      * @since     1.7
  61.      * @access    private
  62.      */
  63.     var $_lineEnd = "\12";
  64.  
  65.     /**
  66.      * HTML comment on the object
  67.      * @var       string
  68.      * @since     1.5
  69.      * @access    private
  70.      */
  71.     var $_comment = '';
  72.  
  73.     /**
  74.      * Class constructor
  75.      * @param    mixed   $attributes     Associative array of table tag attributes
  76.      *                                   or HTML attributes name="value" pairs
  77.      * @param    int     $tabOffset      Indent offset in tabs
  78.      * @access   public
  79.      */
  80.     function HTML_Common($attributes = null, $tabOffset = 0)
  81.     {
  82.         $this->setAttributes($attributes);
  83.         $this->setTabOffset($tabOffset);
  84.     } // end constructor
  85.  
  86.     /**
  87.      * Returns the current API version
  88.      * @access   public
  89.      * @returns  double
  90.      */
  91.     function apiVersion()
  92.     {
  93.         return 1.7;
  94.     } // end func apiVersion
  95.  
  96.     /**
  97.      * Returns the lineEnd
  98.      *
  99.      * @since     1.7
  100.      * @access    private
  101.      * @return    string
  102.      */
  103.     function _getLineEnd()
  104.     {
  105.         return $this->_lineEnd;
  106.     } // end func getLineEnd
  107.  
  108.     /**
  109.      * Returns a string containing the unit for indenting HTML
  110.      *
  111.      * @since     1.7
  112.      * @access    private
  113.      * @return    string
  114.      */
  115.     function _getTab()
  116.     {
  117.         return $this->_tab;
  118.     } // end func _getTab
  119.  
  120.     /**
  121.      * Returns a string containing the offset for the whole HTML code
  122.      *
  123.      * @return    string
  124.      * @access   private
  125.      */
  126.     function _getTabs()
  127.     {
  128.         return str_repeat($this->_getTab(), $this->_tabOffset);
  129.     } // end func _getTabs
  130.  
  131.     /**
  132.      * Returns an HTML formatted attribute string
  133.      * @param    array   $attributes
  134.      * @return   string
  135.      * @access   private
  136.      */
  137.     function _getAttrString($attributes)
  138.     {
  139.         $strAttr = '';
  140.  
  141.         if (is_array($attributes)) {
  142.             $charset = HTML_Common::charset();
  143.             foreach ($attributes as $key => $value) {
  144.                 $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
  145.             }
  146.         }
  147.         return $strAttr;
  148.     } // end func _getAttrString
  149.  
  150.     /**
  151.      * Returns a valid atrributes array from either a string or array
  152.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  153.      * @access   private
  154.      * @return   array
  155.      */
  156.     function _parseAttributes($attributes)
  157.     {
  158.         if (is_array($attributes)) {
  159.             $ret = array();
  160.             foreach ($attributes as $key => $value) {
  161.                 if (is_int($key)) {
  162.                     $key = $value = strtolower($value);
  163.                 } else {
  164.                     $key = strtolower($key);
  165.                 }
  166.                 $ret[$key] = $value;
  167.             }
  168.             return $ret;
  169.  
  170.         } elseif (is_string($attributes)) {
  171.             $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  172.                 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  173.             if (preg_match_all($preg, $attributes, $regs)) {
  174.                 for ($counter=0; $counter<count($regs[1]); $counter++) {
  175.                     $name  = $regs[1][$counter];
  176.                     $check = $regs[0][$counter];
  177.                     $value = $regs[7][$counter];
  178.                     if (trim($name) == trim($check)) {
  179.                         $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  180.                     } else {
  181.                         if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
  182.                             $value = substr($value, 1, -1);
  183.                         }
  184.                         $arrAttr[strtolower(trim($name))] = trim($value);
  185.                     }
  186.                 }
  187.                 return $arrAttr;
  188.             }
  189.         }
  190.     } // end func _parseAttributes
  191.  
  192.     /**
  193.      * Returns the array key for the given non-name-value pair attribute
  194.      *
  195.      * @param     string    $attr         Attribute
  196.      * @param     array     $attributes   Array of attribute
  197.      * @since     1.0
  198.      * @access    private
  199.      * @return    bool
  200.      */
  201.     function _getAttrKey($attr, $attributes)
  202.     {
  203.         if (isset($attributes[strtolower($attr)])) {
  204.             return true;
  205.         } else {
  206.             return null;
  207.         }
  208.     } //end func _getAttrKey
  209.  
  210.     /**
  211.      * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  212.      * @param    array   $attr1      Original attributes array
  213.      * @param    array   $attr2      New attributes array
  214.      * @access   private
  215.      */
  216.     function _updateAttrArray(&$attr1, $attr2)
  217.     {
  218.         if (!is_array($attr2)) {
  219.             return false;
  220.         }
  221.         foreach ($attr2 as $key => $value) {
  222.             $attr1[$key] = $value;
  223.         }
  224.     } // end func _updateAtrrArray
  225.  
  226.     /**
  227.      * Removes the given attribute from the given array
  228.      *
  229.      * @param     string    $attr           Attribute name
  230.      * @param     array     $attributes     Attribute array
  231.      * @since     1.4
  232.      * @access    private
  233.      * @return    void
  234.      */
  235.     function _removeAttr($attr, &$attributes)
  236.     {
  237.         $attr = strtolower($attr);
  238.         if (isset($attributes[$attr])) {
  239.             unset($attributes[$attr]);
  240.         }
  241.     } //end func _removeAttr
  242.  
  243.     /**
  244.      * Returns the value of the given attribute
  245.      *
  246.      * @param     string    $attr   Attribute name
  247.      * @since     1.5
  248.      * @access    public
  249.      * @return    string|null   returns null if an attribute does not exist
  250.      */
  251.     function getAttribute($attr)
  252.     {
  253.         $attr = strtolower($attr);
  254.         if (isset($this->_attributes[$attr])) {
  255.             return $this->_attributes[$attr];
  256.         }
  257.         return null;
  258.     } //end func getAttribute
  259.  
  260.     /**
  261.      * Sets the value of the attribute
  262.      *
  263.      * @param   string  Attribute name
  264.      * @param   string  Attribute value (will be set to $name if omitted)
  265.      * @access  public
  266.      */
  267.     function setAttribute($name, $value = null)
  268.     {
  269.         $name = strtolower($name);
  270.         if (is_null($value)) {
  271.             $value = $name;
  272.         }
  273.         $this->_attributes[$name] = $value;
  274.     } // end func setAttribute
  275.  
  276.     /**
  277.      * Sets the HTML attributes
  278.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  279.      * @access   public
  280.      */
  281.     function setAttributes($attributes)
  282.     {
  283.         $this->_attributes = $this->_parseAttributes($attributes);
  284.     } // end func setAttributes
  285.  
  286.     /**
  287.      * Returns the assoc array (default) or string of attributes
  288.      *
  289.      * @param     bool    Whether to return the attributes as string
  290.      * @since     1.6
  291.      * @access    public
  292.      * @return    mixed   attributes
  293.      */
  294.     function getAttributes($asString = false)
  295.     {
  296.         if ($asString) {
  297.             return $this->_getAttrString($this->_attributes);
  298.         } else {
  299.             return $this->_attributes;
  300.         }
  301.     } //end func getAttributes
  302.  
  303.     /**
  304.      * Updates the passed attributes without changing the other existing attributes
  305.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  306.      * @access   public
  307.      */
  308.     function updateAttributes($attributes)
  309.     {
  310.         $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  311.     } // end func updateAttributes
  312.  
  313.     /**
  314.      * Removes an attribute
  315.      *
  316.      * @param     string    $attr   Attribute name
  317.      * @since     1.4
  318.      * @access    public
  319.      * @return    void
  320.      */
  321.     function removeAttribute($attr)
  322.     {
  323.         $this->_removeAttr($attr, $this->_attributes);
  324.     } //end func removeAttribute
  325.  
  326.     /**
  327.      * Sets the line end style to Windows, Mac, Unix or a custom string.
  328.      *
  329.      * @param   string  $style  "win", "mac", "unix" or custom string.
  330.      * @since   1.7
  331.      * @access  public
  332.      * @return  void
  333.      */
  334.     function setLineEnd($style)
  335.     {
  336.         switch ($style) {
  337.             case 'win':
  338.                 $this->_lineEnd = "\15\12";
  339.                 break;
  340.             case 'unix':
  341.                 $this->_lineEnd = "\12";
  342.                 break;
  343.             case 'mac':
  344.                 $this->_lineEnd = "\15";
  345.                 break;
  346.             default:
  347.                 $this->_lineEnd = $style;
  348.         }
  349.     } // end func setLineEnd
  350.  
  351.     /**
  352.      * Sets the tab offset
  353.      *
  354.      * @param    int     $offset
  355.      * @access   public
  356.      */
  357.     function setTabOffset($offset)
  358.     {
  359.         $this->_tabOffset = $offset;
  360.     } // end func setTabOffset
  361.  
  362.     /**
  363.      * Returns the tabOffset
  364.      *
  365.      * @since     1.5
  366.      * @access    public
  367.      * @return    int
  368.      */
  369.     function getTabOffset()
  370.     {
  371.         return $this->_tabOffset;
  372.     } //end func getTabOffset
  373.  
  374.     /**
  375.      * Sets the string used to indent HTML
  376.      *
  377.      * @since     1.7
  378.      * @param     string    $string     String used to indent ("\11", "\t", '  ', etc.).
  379.      * @access    public
  380.      * @return    void
  381.      */
  382.     function setTab($string)
  383.     {
  384.         $this->_tab = $string;
  385.     } // end func setTab
  386.  
  387.     /**
  388.      * Sets the HTML comment to be displayed at the beginning of the HTML string
  389.      *
  390.      * @param     string
  391.      * @since     1.4
  392.      * @access    public
  393.      * @return    void
  394.      */
  395.     function setComment($comment)
  396.     {
  397.         $this->_comment = $comment;
  398.     } // end func setHtmlComment
  399.  
  400.     /**
  401.      * Returns the HTML comment
  402.      *
  403.      * @since     1.5
  404.      * @access    public
  405.      * @return    string
  406.      */
  407.     function getComment()
  408.     {
  409.         return $this->_comment;
  410.     } //end func getComment
  411.  
  412.     /**
  413.      * Abstract method.  Must be extended to return the objects HTML
  414.      *
  415.      * @access    public
  416.      * @return    string
  417.      * @abstract
  418.      */
  419.     function toHtml()
  420.     {
  421.         return '';
  422.     } // end func toHtml
  423.  
  424.     /**
  425.      * Displays the HTML to the screen
  426.      *
  427.      * @access    public
  428.      */
  429.     function display()
  430.     {
  431.         print $this->toHtml();
  432.     } // end func display
  433.  
  434.     /**
  435.      * Sets the charset to use by htmlspecialchars() function
  436.      *
  437.      * Since this parameter is expected to be global, the function is designed
  438.      * to be called statically:
  439.      * <code>
  440.      * HTML_Common::charset('utf-8');
  441.      * </code>
  442.      * or
  443.      * <code>
  444.      * $charset = HTML_Common::charset();
  445.      * </code>
  446.      *
  447.      * @param   string  New charset to use. Omit if just getting the 
  448.      *                  current value. Consult the htmlspecialchars() docs 
  449.      *                  for a list of supported character sets.
  450.      * @return  string  Current charset
  451.      * @access  public
  452.      * @static
  453.      */
  454.     function charset($newCharset = null)
  455.     {
  456.         static $charset = 'ISO-8859-1';
  457.  
  458.         if (!is_null($newCharset)) {
  459.             $charset = $newCharset;
  460.         }
  461.         return $charset;
  462.     } // end func charset
  463. } // end class HTML_Common
  464. ?>
  465.