DoctypeDeclaration.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00029     class DoctypeDeclaration
00030     {
00031         const SPACER_MASK           = '[ \r\n\t]';
00032         const ID_FIRST_CHAR_MASK    = '[A-Za-z]';
00033         const ID_CHAR_MASK          = '[-_:.A-Za-z0-9]';
00034         
00035         protected $fpi          = null;
00036         
00037         private $rootElement    = null;
00038         
00039         private $inline         = false;
00040         private $declarations   = null; // unparsed
00041         
00042         private $public         = false;
00043         
00044         private $uri            = null;
00045         
00049         public static function create()
00050         {
00051             return new self;
00052         }
00053         
00057         public function setRootElement($rootElement)
00058         {
00059             $this->rootElement = $rootElement;
00060             
00061             return $this;
00062         }
00063         
00064         public function getRootElement()
00065         {
00066             return $this->rootElement;
00067         }
00068         
00072         public function setInline($isInline)
00073         {
00074             Assert::isBoolean($isInline);
00075             
00076             $this->inline = $isInline;
00077             $this->public = false;
00078             
00079             return $this;
00080         }
00081         
00082         public function isInline()
00083         {
00084             return $this->inline;
00085         }
00086         
00090         public function setPublic($isPublic)
00091         {
00092             Assert::isBoolean($isPublic);
00093             
00094             $this->public = $isPublic;
00095             $this->inline = false;
00096             
00097             return $this;
00098         }
00099         
00100         public function isPublic()
00101         {
00102             return $this->public;
00103         }
00104         
00105         public function isSystem()
00106         {
00107             return !$this->public;
00108         }
00109         
00113         public function setDeclarations($declarations)
00114         {
00115             $this->declarations = $declarations;
00116             
00117             return $this;
00118         }
00119         
00120         public function getDeclarations()
00121         {
00122             return $this->declarations;
00123         }
00124         
00128         public function setFpi($fpi)
00129         {
00130             $this->fpi = $fpi;
00131             
00132             return $this;
00133         }
00134         
00135         public function getFpi()
00136         {
00137             return $this->fpi;
00138         }
00139         
00143         public function setUri($uri)
00144         {
00145             $this->uri = $uri;
00146             
00147             return $this;
00148         }
00149         
00150         public function getUri()
00151         {
00152             return $this->uri;
00153         }
00154         
00160         public function parse($string)
00161         {
00162             $matches = array();
00163             
00164             if (
00165                 !preg_match(
00166                     '~^('.self::ID_FIRST_CHAR_MASK.self::ID_CHAR_MASK.'*)'
00167                     .self::SPACER_MASK.'+(.*)$~s',
00168                     $string, $matches
00169                 )
00170             ) {
00171                 return null;
00172             }
00173             
00174             $this->rootElement = $matches[1];
00175             $remainigString = $matches[2];
00176             
00177             if (
00178                 preg_match(
00179                     '~^PUBLIC'.self::SPACER_MASK.'+"(.+?)"'
00180                     .'('.self::SPACER_MASK.'*"(.+)")?$~is',
00181                     $remainigString, $matches
00182                 )
00183             ) {
00184                 $this->public = true;
00185                 
00186                 $this->inline = false;
00187                 $this->declarations = null;
00188                 
00189                 $this->setFpi($matches[1]);
00190                 
00191                 if (isset($matches[3]))
00192                     $this->uri = $matches[3];
00193                     
00194             } elseif (
00195                 preg_match(
00196                     '~^SYSTEM'.self::SPACER_MASK.'+"(.+?)"$~is',
00197                     $remainigString, $matches
00198                 )
00199             ) {
00200                 $this->public = false;
00201                 
00202                 $this->inline = false;
00203                 $this->declarations = null;
00204                 
00205                 $this->setFpi(null);
00206                 $this->uri = $matches[1];
00207                 
00208             } else {
00209                 $this->public = false;
00210                 
00211                 $this->inline = true;
00212                 $this->declarations = $remainigString;
00213                 
00214                 $this->setFpi(null);
00215                 $this->uri = null;
00216             }
00217             
00218             return $this;
00219         }
00220         
00221         public function toString()
00222         {
00223             if ($this->inline)
00224                 return $this->rootElement.' '.$this->declarations;
00225                 
00226             elseif ($this->public)
00227                 return
00228                     $this->rootElement.' PUBLIC "'.$this->getFpi().'"'
00229                     .(
00230                         $this->uri
00231                         ? ' "'.$this->uri.'"'
00232                         : null
00233                     );
00234             else
00235                 return
00236                     $this->rootElement.' SYSTEM "'.$this->getFpi().'"';
00237         }
00238     }
00239 ?>