@@ -18,7 +18,7 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | public function parseLine( Line$line ):parent |
| 20 | 20 | { |
| 21 | - $this->content.= $line->fullContent."\n"; |
|
| 21 | + $this->content.=$line->fullContent."\n"; |
|
| 22 | 22 | return $this; |
| 23 | 23 | } |
| 24 | 24 | } |
@@ -56,9 +56,9 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @param array $config |
| 58 | 58 | */ |
| 59 | - public function __construct( array$config=[] ) |
|
| 59 | + public function __construct( array$config=[ ] ) |
|
| 60 | 60 | { |
| 61 | - $this->config= array_replace_recursive($this->getDefaultConfigs(),$config); |
|
| 61 | + $this->config=array_replace_recursive($this->getDefaultConfigs(), $config); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | public function parse( string$content ):string |
| 76 | 76 | { |
| 77 | - return $this->execute(new StringBuffer($this,$content)); |
|
| 77 | + return $this->execute(new StringBuffer($this, $content)); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -91,12 +91,12 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function compile( string$fromFile, string$toFile='' ) |
| 93 | 93 | { |
| 94 | - $fromFile= $this->getFilePath($fromFile); |
|
| 94 | + $fromFile=$this->getFilePath($fromFile); |
|
| 95 | 95 | |
| 96 | - $result= $this->execute(new FileBuffer($this,$fromFile)); |
|
| 96 | + $result=$this->execute(new FileBuffer($this, $fromFile)); |
|
| 97 | 97 | |
| 98 | 98 | if( $toFile ){ |
| 99 | - return file_put_contents($toFile,$result); |
|
| 99 | + return file_put_contents($toFile, $result); |
|
| 100 | 100 | }else{ |
| 101 | 101 | return $result; |
| 102 | 102 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | */ |
| 132 | 132 | public function setBasePath( string$basePath ):self |
| 133 | 133 | { |
| 134 | - $this->basePath= '/'===substr($basePath,-1) ? substr($basePath,0,-1) : $basePath; |
|
| 134 | + $this->basePath='/'===substr($basePath, -1)? substr($basePath, 0, -1) : $basePath; |
|
| 135 | 135 | |
| 136 | 136 | return $this; |
| 137 | 137 | } |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | protected function execute( ABuffer$buffer ):string |
| 165 | 165 | { |
| 166 | - return (new Document($this,$buffer))->content; |
|
| 166 | + return (new Document($this, $buffer))->content; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -179,13 +179,13 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function getConfig( string...$keys ) |
| 181 | 181 | { |
| 182 | - $result= $this->config; |
|
| 182 | + $result=$this->config; |
|
| 183 | 183 | |
| 184 | 184 | foreach( $keys as $key ){ |
| 185 | - if( !isset($result[$key]) ) |
|
| 185 | + if( !isset($result[ $key ]) ) |
|
| 186 | 186 | { return null; } |
| 187 | 187 | |
| 188 | - $result= $result[$key]; |
|
| 188 | + $result=$result[ $key ]; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | return $result; |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | }else{ |
| 220 | 220 | if( !strlen($path) ) |
| 221 | 221 | { return $this->basePath.'/'.$filePath; } |
| 222 | - elseif( '/'===substr($path,-1) ) |
|
| 222 | + elseif( '/'===substr($path, -1) ) |
|
| 223 | 223 | { return $path.$filePath; } |
| 224 | 224 | else |
| 225 | 225 | { return $path.'/'.$filePath; } |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | public function getFileContent( string$filePath ):string |
| 242 | 242 | { |
| 243 | - return isset($this->fileGetter) ? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 243 | + return isset($this->fileGetter)? $this->fileGetter($filePath) : file_get_contents($filePath); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @var array |
| 52 | 52 | */ |
| 53 | - private $attributes=[]; |
|
| 53 | + private $attributes=[ ]; |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * Real constructor. |
@@ -62,14 +62,14 @@ discard block |
||
| 62 | 62 | protected function construct():parent |
| 63 | 63 | { |
| 64 | 64 | |
| 65 | - $name= $this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 65 | + $name=$this->line->pregGet('/(?<=^-)[\w-:]+/'); |
|
| 66 | 66 | $this->name=$name; |
| 67 | 67 | |
| 68 | - $this->loadConfig($name,$this->document); |
|
| 68 | + $this->loadConfig($name, $this->document); |
|
| 69 | 69 | |
| 70 | - $this->tagName=$this->config['name']??$name; |
|
| 71 | - $this->isEmpty= $this->line->getChar(-1)==='/' || $this->document->getConfig('empty_tags',$this->tagName); |
|
| 72 | - isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'],function( $value,$key ){ $this->setAttribute($key,$value); }); |
|
| 70 | + $this->tagName=$this->config[ 'name' ]??$name; |
|
| 71 | + $this->isEmpty=$this->line->getChar(-1)==='/' || $this->document->getConfig('empty_tags', $this->tagName); |
|
| 72 | + isset($this->config[ 'default_attributes' ]) and array_walk($this->config[ 'default_attributes' ], function( $value, $key ){ $this->setAttribute($key, $value); }); |
|
| 73 | 73 | |
| 74 | 74 | return $this; |
| 75 | 75 | } |
@@ -83,16 +83,16 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | public function open():string |
| 85 | 85 | { |
| 86 | - if( isset($this->config['opener']) ) |
|
| 87 | - { return $this->config['opener']; } |
|
| 86 | + if( isset($this->config[ 'opener' ]) ) |
|
| 87 | + { return $this->config[ 'opener' ]; } |
|
| 88 | 88 | |
| 89 | 89 | $this->parsePrivateAttributes(); |
| 90 | 90 | $this->parseCommonAttributes(); |
| 91 | 91 | |
| 92 | - if( isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function']) ) |
|
| 93 | - { $this->config['scope_function']->call($this,$this->document->scope); } |
|
| 92 | + if( isset($this->config[ 'in_scope' ]) && isset($this->config[ 'scope_function' ]) && is_callable($this->config[ 'scope_function' ]) ) |
|
| 93 | + { $this->config[ 'scope_function' ]->call($this, $this->document->scope); } |
|
| 94 | 94 | |
| 95 | - $finisher= $this->isEmpty ? ' />' : '>'; |
|
| 95 | + $finisher=$this->isEmpty? ' />' : '>'; |
|
| 96 | 96 | |
| 97 | 97 | return "<{$this->tagName}{$this->attributesString}{$finisher}"; |
| 98 | 98 | } |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | public function close( Line$closerLine ):string |
| 110 | 110 | { |
| 111 | - return $this->isEmpty ? '' : $this->config['closer']??"</{$this->tagName}>"; |
|
| 111 | + return $this->isEmpty? '' : $this->config[ 'closer' ]??"</{$this->tagName}>"; |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | /** |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function getEmbed():string |
| 122 | 122 | { |
| 123 | - return $this->config['embedding']??''; |
|
| 123 | + return $this->config[ 'embedding' ]??''; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | */ |
| 133 | 133 | public function getScope() |
| 134 | 134 | { |
| 135 | - return $this->config['scope']??null; |
|
| 135 | + return $this->config[ 'scope' ]??null; |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | /** |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | protected function parsePrivateAttributes():self |
| 146 | 146 | { |
| 147 | 147 | foreach( self::PRIVATE_ATTRIBUTES as $attribute ){ |
| 148 | - isset($this->config[$attribute]) and $this->{'parse'.Helper\camel_case($attribute)}(); |
|
| 148 | + isset($this->config[ $attribute ]) and $this->{'parse'.Helper\camel_case($attribute)}(); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | return $this; |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @todo Make this const private when php 7.1 |
| 162 | 162 | */ |
| 163 | - const PRIVATE_ATTRIBUTES= [ |
|
| 163 | + const PRIVATE_ATTRIBUTES=[ |
|
| 164 | 164 | 'params', |
| 165 | 165 | 'name_value', |
| 166 | 166 | 'link', |
@@ -177,11 +177,11 @@ discard block |
||
| 177 | 177 | */ |
| 178 | 178 | protected function parseParams():self |
| 179 | 179 | { |
| 180 | - $params= preg_split('/(?<!\\\\)\\|/',$this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/',1)); |
|
| 180 | + $params=preg_split('/(?<!\\\\)\\|/', $this->line->pregGet('/^-[\w-:]+\((.*?)\)(?= |(\\{>)?$)/', 1)); |
|
| 181 | 181 | |
| 182 | - if( ($m= count($params)) != ($n= count($this->config['params'])) ){$this->document->throw("Tag $this->name has $n parameters $m given.");} |
|
| 182 | + if( ($m=count($params))!=($n=count($this->config[ 'params' ])) ){$this->document->throw("Tag $this->name has $n parameters $m given."); } |
|
| 183 | 183 | |
| 184 | - array_map(function( $key, $value ){return $this->setAttribute($key,$this->checkExpression(str_replace('\\|','|',$value)));},$this->config['params'],$params); |
|
| 184 | + array_map(function( $key, $value ){return $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))); },$this->config[ 'params' ], $params); |
|
| 185 | 185 | |
| 186 | 186 | return $this; |
| 187 | 187 | } |
@@ -195,9 +195,9 @@ discard block |
||
| 195 | 195 | */ |
| 196 | 196 | protected function parseNameValue():self |
| 197 | 197 | { |
| 198 | - $params= $this->line->pregGet('/ <(.*?)>(?= |$)/',1) |
|
| 199 | - and $params= preg_split('/(?<!\\\\)\\|/',$params) |
|
| 200 | - and array_map(function( $key, $value ){return isset($key)&&isset($value) ? $this->setAttribute($key,$this->checkExpression(str_replace('\\|','|',$value))) : '';},$this->config['name_value'],$params); |
|
| 198 | + $params=$this->line->pregGet('/ <(.*?)>(?= |$)/', 1) |
|
| 199 | + and $params=preg_split('/(?<!\\\\)\\|/', $params) |
|
| 200 | + and array_map(function( $key, $value ){return isset($key) && isset($value)? $this->setAttribute($key, $this->checkExpression(str_replace('\\|', '|', $value))) : ''; },$this->config[ 'name_value' ], $params); |
|
| 201 | 201 | |
| 202 | 202 | return $this; |
| 203 | 203 | } |
@@ -239,13 +239,13 @@ discard block |
||
| 239 | 239 | */ |
| 240 | 240 | protected function parseLink():self |
| 241 | 241 | { |
| 242 | - return $this->setMultinameAttribute('link',$this->sectionLedBy('@',true),function( string$link ){ |
|
| 243 | - if( isset($this->config['target']) && ':'===$link{0} ){ |
|
| 242 | + return $this->setMultinameAttribute('link', $this->sectionLedBy('@', true), function( string$link ){ |
|
| 243 | + if( isset($this->config[ 'target' ]) && ':'===$link{0} ){ |
|
| 244 | 244 | return 'javascript'.$link; |
| 245 | - }elseif( '//'===($firstTwoLetters=substr($link,0,2)) ){ |
|
| 245 | + }elseif( '//'===($firstTwoLetters=substr($link, 0, 2)) ){ |
|
| 246 | 246 | return 'http:'.$link; |
| 247 | 247 | }elseif( '\\\\'===$firstTwoLetters ){ |
| 248 | - return 'https://'.substr($link,2); |
|
| 248 | + return 'https://'.substr($link, 2); |
|
| 249 | 249 | }else{ |
| 250 | 250 | return $this->checkExpression($link); |
| 251 | 251 | } |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | */ |
| 262 | 262 | protected function parseTarget():self |
| 263 | 263 | { |
| 264 | - return $this->setMultinameAttribute('target',$this->sectionLedBy('>',true)); |
|
| 264 | + return $this->setMultinameAttribute('target', $this->sectionLedBy('>', true)); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | protected function parseAlt():self |
| 275 | 275 | { |
| 276 | - return $this->setMultinameAttribute('alt',$this->sectionLedBy('_',true)); |
|
| 276 | + return $this->setMultinameAttribute('alt', $this->sectionLedBy('_', true)); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /** |
@@ -289,8 +289,8 @@ discard block |
||
| 289 | 289 | */ |
| 290 | 290 | private function setMultinameAttribute( string$name, string$value, callable$processer=null ):self |
| 291 | 291 | { |
| 292 | - if( strlen($value) && !empty($this->config[$name]) ){ |
|
| 293 | - return $this->setAttribute($this->config[$name],call_user_func($processer??[$this,'checkExpression',],$value)); |
|
| 292 | + if( strlen($value) && !empty($this->config[ $name ]) ){ |
|
| 293 | + return $this->setAttribute($this->config[ $name ], call_user_func($processer??[ $this, 'checkExpression', ], $value)); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | return $this; |
@@ -305,33 +305,33 @@ discard block |
||
| 305 | 305 | */ |
| 306 | 306 | protected function parseCommonAttributes():string |
| 307 | 307 | { |
| 308 | - $attributes= ''; |
|
| 308 | + $attributes=''; |
|
| 309 | 309 | |
| 310 | - $id= $this->sectionLedBy('#') |
|
| 311 | - and $this->setAttribute('id',$id); |
|
| 310 | + $id=$this->sectionLedBy('#') |
|
| 311 | + and $this->setAttribute('id', $id); |
|
| 312 | 312 | |
| 313 | - $classes= $this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 314 | - and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/',$classes,$matches) |
|
| 315 | - and $classes= implode(' ',array_filter(array_map(function( $className ){return $this->checkExpression($className);},$matches[1]))) |
|
| 316 | - and $this->setAttribute('class',$classes); |
|
| 313 | + $classes=$this->line->pregGet('/ \.[^ ]+(?= |$)/') |
|
| 314 | + and preg_match_all('/\.((?(?!\()[^.]+|(?<exp>\((?:[^()]+|(?&exp)?)+?\))))/', $classes, $matches) |
|
| 315 | + and $classes=implode(' ', array_filter(array_map(function( $className ){return $this->checkExpression($className); },$matches[ 1 ]))) |
|
| 316 | + and $this->setAttribute('class', $classes); |
|
| 317 | 317 | |
| 318 | - $title= $this->sectionLedBy('^',true) |
|
| 319 | - and $this->setAttribute('title',$this->checkExpression($title)); |
|
| 318 | + $title=$this->sectionLedBy('^', true) |
|
| 319 | + and $this->setAttribute('title', $this->checkExpression($title)); |
|
| 320 | 320 | |
| 321 | - $style= $this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/',1) |
|
| 322 | - and $this->setAttribute('style',$style); |
|
| 321 | + $style=$this->line->pregGet('/ \[([^\]]+;)(?=\]( |$))/', 1) |
|
| 322 | + and $this->setAttribute('style', $style); |
|
| 323 | 323 | |
| 324 | - $eventListeners= $this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/',function( $string, $name, $code ){ |
|
| 325 | - $this->setAttribute('on'.$name,str_replace('"','"',$code)); |
|
| 324 | + $eventListeners=$this->line->pregMap('/ %(\w+)\{>(.*?)<\}(?= |$)/', function( $string, $name, $code ){ |
|
| 325 | + $this->setAttribute('on'.$name, str_replace('"', '"', $code)); |
|
| 326 | 326 | }) |
| 327 | - and implode('',$eventListeners); |
|
| 327 | + and implode('', $eventListeners); |
|
| 328 | 328 | |
| 329 | - $other= $this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 329 | + $other=$this->line->pregGet('/(?<=\{).*?(?=;\}( |$))/') |
|
| 330 | 330 | and array_map(function( $keyValue ){ |
| 331 | - preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/',function($matches){ |
|
| 332 | - $this->setAttribute($matches[1],(isset($matches[3])? $this->checkExpression($matches[3]) : $matches[1]),$matches[2]??null); |
|
| 331 | + preg_replace_callback('/^([\w-:]+)(?:\?(.+?))?(?:\=(.*))?$/', function( $matches ){ |
|
| 332 | + $this->setAttribute($matches[ 1 ], (isset($matches[ 3 ])? $this->checkExpression($matches[ 3 ]) : $matches[ 1 ]), $matches[ 2 ]??null); |
|
| 333 | 333 | },$keyValue); |
| 334 | - },explode(';',$other)); |
|
| 334 | + },explode(';', $other)); |
|
| 335 | 335 | |
| 336 | 336 | return $attributes; |
| 337 | 337 | } |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | */ |
| 348 | 348 | protected function checkExpression( string$value ):string |
| 349 | 349 | { |
| 350 | - return preg_match('/^\(.*\)$/',$value) ? '<?=str_replace(\'"\',\'"\','.substr($value,1,-1).')?>' : str_replace('"','"',$value); |
|
| 350 | + return preg_match('/^\(.*\)$/', $value)? '<?=str_replace(\'"\',\'"\','.substr($value, 1, -1).')?>' : str_replace('"', '"', $value); |
|
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | /** |
@@ -360,13 +360,13 @@ discard block |
||
| 360 | 360 | protected function getAttributesString():string |
| 361 | 361 | { |
| 362 | 362 | ksort($this->attributes); |
| 363 | - return implode('',array_map(static function( string$key, array$data ){ |
|
| 364 | - return (isset($data['condition'])&&strlen($data['condition'])? |
|
| 365 | - "<?php if( {$data['condition']} ){?> $key=\"{$data['value']}\"<?php }?>" |
|
| 363 | + return implode('', array_map(static function( string$key, array$data ){ |
|
| 364 | + return (isset($data[ 'condition' ]) && strlen($data[ 'condition' ])? |
|
| 365 | + "<?php if( {$data[ 'condition' ]} ){?> $key=\"{$data[ 'value' ]}\"<?php }?>" |
|
| 366 | 366 | : |
| 367 | - " $key=\"{$data['value']}\"" |
|
| 367 | + " $key=\"{$data[ 'value' ]}\"" |
|
| 368 | 368 | ); |
| 369 | - },array_keys($this->attributes),$this->attributes)); |
|
| 369 | + },array_keys($this->attributes), $this->attributes)); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /** |
@@ -380,10 +380,10 @@ discard block |
||
| 380 | 380 | */ |
| 381 | 381 | protected function setAttribute( string$key, string$value, string$condition=null ):self |
| 382 | 382 | { |
| 383 | - if( isset($this->attributes[$key]) ) |
|
| 383 | + if( isset($this->attributes[ $key ]) ) |
|
| 384 | 384 | { $this->document->throw("Attribute $key of $this->name cannot redeclare."); } |
| 385 | 385 | |
| 386 | - $this->attributes[$key]=[ |
|
| 386 | + $this->attributes[ $key ]=[ |
|
| 387 | 387 | 'value'=> $value, |
| 388 | 388 | 'condition'=> $condition, |
| 389 | 389 | ]; |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | */ |
| 408 | 408 | public function offsetExists( $offset ):bool |
| 409 | 409 | { |
| 410 | - return isset($this->attributes[$offset]); |
|
| 410 | + return isset($this->attributes[ $offset ]); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | /** |
@@ -421,7 +421,7 @@ discard block |
||
| 421 | 421 | */ |
| 422 | 422 | public function offsetGet( $offset ) |
| 423 | 423 | { |
| 424 | - return $this->attributes[$offset]??null; |
|
| 424 | + return $this->attributes[ $offset ]??null; |
|
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | /** |
@@ -434,7 +434,7 @@ discard block |
||
| 434 | 434 | */ |
| 435 | 435 | public function offsetSet( $offset, $value ) |
| 436 | 436 | { |
| 437 | - $this->setAttribute($offset,$value); |
|
| 437 | + $this->setAttribute($offset, $value); |
|
| 438 | 438 | } |
| 439 | 439 | |
| 440 | 440 | /** |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | */ |
| 447 | 447 | public function offsetUnset( $offset ) |
| 448 | 448 | { |
| 449 | - if( isset($this->attributes[$offset]) ) |
|
| 450 | - { unset($this->attributes[$offset]); } |
|
| 449 | + if( isset($this->attributes[ $offset ]) ) |
|
| 450 | + { unset($this->attributes[ $offset ]); } |
|
| 451 | 451 | } |
| 452 | 452 | } |