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