adaniel         Wed Mar 21 20:14:43 2001 EDT

  Added files:                 
    /php4/pear/HTML     Common.php 
  Log:
  orginal commit.  class containing common html tools
  

Index: php4/pear/HTML/Common.php
+++ php4/pear/HTML/Common.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | [EMAIL PROTECTED] so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Adam Daniel <[EMAIL PROTECTED]>                        |
// +----------------------------------------------------------------------+
//
// $Id: Common.php,v 1.1 2001/03/22 04:14:42 adaniel Exp $

/**
* Base class for all HTML classes
*
* @author      Adam Daniel <[EMAIL PROTECTED]>
* @version     1.3
* @copyright   Copyright 2001 Ethicon Endo-Surgery
* @since       PHP 4.0.3pl1
*/
class HTML_Common {
    /**
    * Associative array of table attributes
    * @var  array
    * @access   private
    */
    var $_attributes = array();
    /**
    * Tab offset of the table
    * @var  int
    * @access   private
    */
    var $_tabOffset = 0;
    /**
    * Class constructor
    * @param    mixed   $attributes     Associative array of table tag attributes 
    *                                   or HTML attributes name="value" pairs
    * @access   public
    */
    function HTML_Common($attributes=null, $tabOffset=0)
    {
        $this->setTabOffset($tabOffset);
        $this->setAttributes($attributes);
    }
    /**
    * Returns the current API version
    * @access   public
    * @returns  double
    */
    function apiVersion()
    {
        return 1.3;
    }
    /**
    * Returns a string of \t for the tabOffset property
    * @access   private
    */
    function _getTabs()
    {
        return $this->_tabOffset > 0 ? str_repeat("\t", $this->_tabOffset) : "";
    }
    /**
    * Returns an HTML formatted attribute string
    * @param    array   $attributes
    * @return   string
    * @access   private
    */
    function _getAttrString($attributes)
    {
        $strAttr = "";
        if (is_array($attributes)) {
            while (list($key, $value) = each($attributes)) {
                if (is_int($key)) {
                    $strAttr .= " " . strtoupper($value);
                } else {
                    $strAttr .= " " . strtolower($key) . "=\"$value\"";
                }
            }
        }
        return $strAttr;
    }
    /**
    * Returns a valid atrributes array from either a string or array
    * @param    mixed   $attributes     Either a typical HTML attribute string or an 
associative array
    * @access   private
    */
    function _parseAttributes($attributes)
    {
        if (is_array($attributes)) {
            return $attributes;
        } elseif (is_string($attributes)) {
            $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
                "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ 
\\n\\t\\r]*))?/";
            if (preg_match_all($preg, $attributes, $regs)) {
                $valCounter = 0;
                for ($counter=0; $counter<count($regs[1]); $counter++) {
                    $name = $regs[1][$counter];
                    $check = $regs[0][$counter];
                    $value = $regs[7][$valCounter];
                    if (trim($name) == trim($check)) {
                        $arrAttr[] = strtoupper(trim($name));
                    } else {
                        if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == 
"'") {
                            $value = substr($value, 1, -1);
                        }
                        $arrAttr[strtolower(trim($name))] = trim($value);
                        $valCounter++;
                    }
                }
                return $arrAttr;
            }
        }
    }
    /**
    * Updates the attributes in $attr1 with the values in $attr2 without changing the 
other existing attributes
    * @param    array   $attr1      Original attributes array
    * @param    array   $attr2      New attrbiutes array
    * @access   private
    * @return   array
    */
    function _updateAttrArray(&$attr1, &$attr2)
    {
        while (list($key, $value) = each($attr2)) {
            if (!is_int($key)) {
                $attr1[$key] = $value;
            } else {
                $key = null;
                if (is_array($attr1)) {
                    while (list($currKey, $currValue) = each($attr1)) {
                        if ($currValue == $value) {
                            $key = $currKey;
                            break;
                        }
                    }
                }
                if (isset($key)) {
                    $attr1[$key] = $value;
                } else {
                    if (is_array($attr1)) {
                        array_unshift($attr1, $value);
                    } else {
                        $attr1[] = $value;
                    }
                }
            }
        }
    }
    /**
    * Sets the HTML attributes
    * @param    mixed   $attributes     Either a typical HTML attribute string or an 
associative array
    * @access   public
    */
    function setAttributes($attributes) 
    {
        $this->_attributes = $this->_parseAttributes($attributes);
    }
    /**
    * Updates the passed attributes without changing the other existing attributes
    * @param    mixed   $attributes     Either a typical HTML attribute string or an 
associative array
    * @access   public
    */
    function updateAttributes($attributes)
    {
        $attributes = $this->_parseAttributes($attributes);
        $this->_updateAttrArray($this->_attributes, $attributes);
    }
    /**
    * Sets the tab offset
    * @param    int     $offset
    * @access   public
    */
    function setTabOffset($offset)
    {
        $this->_tabOffset = $offset;
    }
}
?>



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to