[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/pear/HTML/ -> Common.php (source)

   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  // | [email protected] so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Author: Adam Daniel <[email protected]>                         |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id$
  20  
  21  /**
  22   * Base class for all HTML classes
  23   * 
  24   * @author    Adam Daniel <[email protected]>
  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 <[email protected]>
  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      public static function raiseError($message = null,
  93                                         $code = null,
  94                                         $mode = null,
  95                                         $options = null,
  96                                         $userinfo = null,
  97                                         $error_class = null,
  98                                         $skipmsg = false) {
  99          $pear = new PEAR();
 100          return $pear->raiseError($message, $code, $mode, $options, $userinfo, $error_class, $skipmsg);
 101      }
 102  
 103      /**
 104       * Returns the current API version
 105       * @access   public
 106       * @returns  double
 107       */
 108      function apiVersion()
 109      {
 110          return 1.7;
 111      } // end func apiVersion
 112  
 113      /**
 114       * Returns the lineEnd
 115       * 
 116       * @since     1.7
 117       * @access    private
 118       * @return    string
 119       * @throws
 120       */
 121      function _getLineEnd()
 122      {
 123          return $this->_lineEnd;
 124      } // end func getLineEnd
 125  
 126      /**
 127       * Returns a string containing the unit for indenting HTML
 128       * 
 129       * @since     1.7
 130       * @access    private
 131       * @return    string
 132       */
 133      function _getTab()
 134      {
 135          return $this->_tab;
 136      } // end func _getTab
 137  
 138      /**
 139       * Returns a string containing the offset for the whole HTML code
 140       * 
 141       * @return    string
 142       * @access   private
 143       */
 144      function _getTabs()
 145      {
 146          return str_repeat($this->_getTab(), $this->_tabOffset);
 147      } // end func _getTabs
 148  
 149      /**
 150       * Returns an HTML formatted attribute string
 151       * @param    array   $attributes
 152       * @return   string
 153       * @access   private
 154       */
 155      function _getAttrString($attributes)
 156      {
 157          $strAttr = '';
 158  
 159          if (is_array($attributes)) {
 160              foreach ($attributes as $key => $value) {
 161                  $strAttr .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
 162              }
 163          }
 164          return $strAttr;
 165      } // end func _getAttrString
 166  
 167      /**
 168       * Returns a valid atrributes array from either a string or array
 169       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 170       * @access   private
 171       */
 172      function _parseAttributes($attributes)
 173      {
 174          if (is_array($attributes)) {
 175              $ret = array();
 176              foreach ($attributes as $key => $value) {
 177                  if (is_int($key)) {
 178                      $key = $value = strtolower($value);
 179                  } else {
 180                      $key = strtolower($key);
 181                  }
 182                  $ret[$key] = $value;
 183              }
 184              return $ret;
 185  
 186          } elseif (is_string($attributes)) {
 187              $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
 188                  "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
 189              if (preg_match_all($preg, $attributes, $regs)) {
 190                  for ($counter=0; $counter<count($regs[1]); $counter++) {
 191                      $name  = $regs[1][$counter];
 192                      $check = $regs[0][$counter];
 193                      $value = $regs[7][$counter];
 194                      if (trim($name) == trim($check)) {
 195                          $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
 196                      } else {
 197                          if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
 198                              $value = substr($value, 1, -1);
 199                          }
 200                          $arrAttr[strtolower(trim($name))] = trim($value);
 201                      }
 202                  }
 203                  return $arrAttr;
 204              }
 205          }
 206      } // end func _parseAttributes
 207  
 208      /**
 209       * Returns the array key for the given non-name-value pair attribute
 210       * 
 211       * @param     string    $attr         Attribute
 212       * @param     array     $attributes   Array of attribute
 213       * @since     1.0
 214       * @access    private
 215       * @return    bool
 216       * @throws
 217       */
 218      function _getAttrKey($attr, $attributes)
 219      {
 220          if (isset($attributes[strtolower($attr)])) {
 221              return true;
 222          } else {
 223              return null;
 224          }
 225      } //end func _getAttrKey
 226  
 227      /**
 228       * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
 229       * @param    array   $attr1      Original attributes array
 230       * @param    array   $attr2      New attributes array
 231       * @access   private
 232       */
 233      function _updateAttrArray(&$attr1, $attr2)
 234      {
 235          if (!is_array($attr2)) {
 236              return false;
 237          }
 238          foreach ($attr2 as $key => $value) {
 239              $attr1[$key] = $value;
 240          }
 241      } // end func _updateAtrrArray
 242  
 243      /**
 244       * Removes the given attribute from the given array
 245       * 
 246       * @param     string    $attr           Attribute name
 247       * @param     array     $attributes     Attribute array
 248       * @since     1.4
 249       * @access    private
 250       * @return    void
 251       * @throws
 252       */
 253      function _removeAttr($attr, &$attributes)
 254      {
 255          $attr = strtolower($attr);
 256          if (isset($attributes[$attr])) {
 257              unset($attributes[$attr]);
 258          }
 259      } //end func _removeAttr
 260  
 261      /**
 262       * Returns the value of the given attribute
 263       * 
 264       * @param     string    $attr   Attribute name
 265       * @since     1.5
 266       * @access    public
 267       * @return    void
 268       * @throws
 269       */
 270      function getAttribute($attr)
 271      {
 272          $attr = strtolower($attr);
 273          if (isset($this->_attributes[$attr])) {
 274              return $this->_attributes[$attr];
 275          }
 276          return null;
 277      } //end func getAttribute
 278  
 279      /**
 280       * Sets the HTML attributes
 281       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 282       * @access   public
 283       */
 284      function setAttributes($attributes)
 285      {
 286          $this->_attributes = $this->_parseAttributes($attributes);
 287      } // end func setAttributes
 288  
 289      /**
 290       * Returns the assoc array (default) or string of attributes
 291       *
 292       * @param     bool    Whether to return the attributes as string 
 293       * @since     1.6
 294       * @access    public
 295       * @return    mixed   attributes
 296       */
 297      function getAttributes($asString = false)
 298      {
 299          if ($asString) {
 300              return $this->_getAttrString($this->_attributes);
 301          } else {
 302              return $this->_attributes;
 303          }
 304      } //end func getAttributes
 305  
 306      /**
 307       * Updates the passed attributes without changing the other existing attributes
 308       * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
 309       * @access   public
 310       */
 311      function updateAttributes($attributes)
 312      {
 313          $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
 314      } // end func updateAttributes
 315  
 316      /**
 317       * Removes an attribute
 318       * 
 319       * @param     string    $attr   Attribute name
 320       * @since     1.4
 321       * @access    public
 322       * @return    void
 323       * @throws
 324       */
 325      function removeAttribute($attr)
 326      {
 327          $this->_removeAttr($attr, $this->_attributes);
 328      } //end func removeAttribute
 329  
 330      /**
 331       * Sets the line end style to Windows, Mac, Unix or a custom string.
 332       * 
 333       * @param   string  $style  "win", "mac", "unix" or custom string.
 334       * @since   1.7
 335       * @access  public
 336       * @return  void
 337       */
 338      function setLineEnd($style)
 339      {
 340          switch ($style) {
 341              case 'win':
 342                  $this->_lineEnd = "\15\12";
 343                  break;
 344              case 'unix':
 345                  $this->_lineEnd = "\12";
 346                  break;
 347              case 'mac':
 348                  $this->_lineEnd = "\15";
 349                  break;
 350              default:
 351                  $this->_lineEnd = $style;
 352          }
 353      } // end func setLineEnd
 354  
 355      /**
 356       * Sets the tab offset
 357       *
 358       * @param    int     $offset
 359       * @access   public
 360       */
 361      function setTabOffset($offset)
 362      {
 363          $this->_tabOffset = $offset;
 364      } // end func setTabOffset
 365  
 366      /**
 367       * Returns the tabOffset
 368       * 
 369       * @since     1.5
 370       * @access    public
 371       * @return    int
 372       */
 373      function getTabOffset()
 374      {
 375          return $this->_tabOffset;
 376      } //end func getTabOffset
 377  
 378      /**
 379       * Sets the string used to indent HTML
 380       * 
 381       * @since     1.7
 382       * @param     string    $string     String used to indent ("\11", "\t", '  ', etc.).
 383       * @access    public
 384       * @return    void
 385       */
 386      function setTab($string)
 387      {
 388          $this->_tab = $string;
 389      } // end func setTab
 390  
 391      /**
 392       * Sets the HTML comment to be displayed at the beginning of the HTML string
 393       *
 394       * @param     string
 395       * @since     1.4
 396       * @access    public
 397       * @return    void
 398       */
 399      function setComment($comment)
 400      {
 401          $this->_comment = $comment;
 402      } // end func setHtmlComment
 403  
 404      /**
 405       * Returns the HTML comment
 406       * 
 407       * @since     1.5
 408       * @access    public
 409       * @return    string
 410       */
 411      function getComment()
 412      {
 413          return $this->_comment;
 414      } //end func getComment
 415  
 416      /**
 417       * Abstract method.  Must be extended to return the objects HTML
 418       *
 419       * @access    public
 420       * @return    string
 421       * @abstract
 422       */
 423      function toHtml()
 424      {
 425          return '';
 426      } // end func toHtml
 427  
 428      /**
 429       * Displays the HTML to the screen
 430       *
 431       * @access    public
 432       */
 433      function display()
 434      {
 435          print $this->toHtml();
 436      } // end func display
 437  
 438  } // end class HTML_Common
 439  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1