home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / HTML / Common.php < prev    next >
Encoding:
PHP Script  |  2005-09-26  |  11.5 KB  |  429 lines

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