Skip to content

Commit eb245de

Browse files
committed
improve the parse method and add the inverse parse
1 parent 6a8f8b4 commit eb245de

File tree

8 files changed

+223
-23
lines changed

8 files changed

+223
-23
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# dom-parser
22

3-
A PHP library , use to parse normal and special DOM string like vue react . to DOM Tree .
3+
A PHP library , use to parse normal and special DOM string like vue react. to DOM Tree.
4+
By modifying config variables, can parse other HTML-like markup languages string too.
45

56
## Installation
67
composer require faitheir/dom-parser
78

89
## Base Usage
9-
10+
#### Parse Method
11+
parse Dom string to DOM Tree (\Faitheir\DomParser\Node $dom).
1012
```php
1113
$html = <<< 'HTML'
1214
<template>
@@ -37,4 +39,29 @@ $parser = new \Faitheir\DomParser\DomParser();
3739
$dom = $parser->setConfig([])->parse($html);
3840

3941
print_r($dom);
42+
```
43+
44+
#### Inverse Parse Method
45+
parse Dom Tree(\Faitheir\DomParser\Node $dom) to DOM string.
46+
```php
47+
$string = $parser->setConfig([])->invParse($dom);
48+
or
49+
Config::getInstance()->setConfig([
50+
'tag_indent' => ' '
51+
]);
52+
$string = (string) $dom;
53+
```
54+
55+
## Config Method
56+
```php
57+
# demo
58+
# match string '{{php id="world"}}'
59+
60+
$configs = [
61+
'start_tag_reg' => '/^\s*{{([^}\s\/!]+)/is'
62+
];
63+
# other confs see DomPaser/Config.php
64+
65+
$parser = $parser->setConfig($configs);
66+
$dom = $parser->parse($html);
4067
```

examples/inverse_parse_com.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: WangFei
5+
* Date: 4/24/2020
6+
* Time: 5:37 PM
7+
*/
8+
9+
require_once '../vendor/autoload.php';
10+
11+
$parser = new \Faitheir\DomParser\DomParser();
12+
13+
$html = <<< 'HTML'
14+
<template>
15+
<!-- doc hello
16+
world
17+
-->
18+
<view class="container" id="main-view" data-genid="12323">
19+
<navigator url="/pages/show?id=1">
20+
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
21+
</navigator>
22+
<view class="layout">
23+
<view class="scan">
24+
<text class="iconfont icon-saoyisao" @click="scan()"></text>
25+
</view>
26+
<search-box class="search" :lable="search_lable" :value="serch" />
27+
<view class="view-box">
28+
Hello World !
29+
</view>
30+
</view>
31+
</view>
32+
</template>
33+
HTML;
34+
35+
$dom = $parser->parse($html);
36+
37+
$string = $parser->invParse($dom);
38+
echo $string;

examples/parse_dom_string.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: WangFei
5+
* Date: 4/24/2020
6+
* Time: 5:34 PM
7+
*/
8+
9+
require_once '../vendor/autoload.php';
10+
11+
$parser = new \Faitheir\DomParser\DomParser();
12+
13+
$html = <<< 'HTML'
14+
<template>
15+
<!-- doc hello
16+
world
17+
-->
18+
<view class="container" id="main-view" data-genid="12323">
19+
<navigator url="/pages/show?id=1">
20+
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
21+
</navigator>
22+
<view class="layout">
23+
<view class="scan">
24+
<text class="iconfont icon-saoyisao" @click="scan()"></text>
25+
</view>
26+
<search-box class="search" :lable="search_lable" :value="serch" />
27+
<view class="view-box">
28+
Hello World !
29+
</view>
30+
</view>
31+
</view>
32+
</template>
33+
HTML;
34+
35+
36+
echo '<pre>';
37+
$dom = $parser->parse($html);
38+
39+
print_r($dom);

src/DomParser/Config.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@
44

55
class Config
66
{
7+
static private $instance;
78

9+
private function __clone(){}
810
# parser configs
911
private $_config = [
12+
# parse
1013
'start_tag_reg' => '/^\s*<([^>\s\/!]+)/is',
1114
'start_end_tag_reg' => '/(^\s*>)|(^\s*\/>)/is',
1215
'end_tag_reg' => '/^\s*<([^>\s\/]+)/is',
1316
'content_reg' => '/^\s*([^<]+)|(<!--.*-->)/is',
1417
'attrs_reg' => '/^\s*([^=>< ]+)="([^"]*)"|\s([^=><\s]+)(?=\s|>)/iU',
18+
# inverse parse
19+
'tag_indent' => ' ',
20+
'hide_genid' => false,
1521
];
1622

17-
/**
18-
* Config constructor.
19-
* @param array $confs
20-
*/
21-
public function __construct($confs = [])
23+
private function __construct($confs = [])
2224
{
2325
if (!empty($confs))
2426
$this->setConfig($confs);
2527
}
28+
static public function getInstance($confs = [])
29+
{
30+
if (!self::$instance instanceof self) {
31+
self::$instance = new self($confs);
32+
}
33+
if (!empty($confs))
34+
self::$instance->setConfig($confs);
35+
36+
return self::$instance;
37+
}
2638

2739
/**
2840
* dynamic settings

src/DomParser/DomParser.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* DomParser Index File
54
*/
@@ -12,9 +11,9 @@ class DomParser
1211
private $orgDomString = '';
1312

1413
# config object
15-
public $config;
14+
private $config;
1615
# parser object
17-
public $parser;
16+
private $parser;
1817

1918

2019
/**
@@ -24,7 +23,7 @@ class DomParser
2423
*/
2524
public function __construct($domString = '', $config = [])
2625
{
27-
$this->config = new Config($config);
26+
$this->config = Config::getInstance($config);
2827
$this->parser = new Parser();
2928

3029
if ($domString)
@@ -50,7 +49,16 @@ public function parse($domString = '')
5049
if ($domString)
5150
$this->orgDomString = $domString;
5251

53-
return $this->parser->config($this->config)->parse($this->orgDomString);
52+
return $this->parser->parse($this->orgDomString);
5453
}
5554

55+
/**
56+
* inverse method
57+
* @param $domTree
58+
* @return string
59+
*/
60+
public function invParse(Node $domTree)
61+
{
62+
return (string) $domTree;
63+
}
5664
}

src/DomParser/Node.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,78 @@ public static function generContentNode($content = '')
137137
return (new self('content'))->setNodeId()->setContent($content);
138138
}
139139

140+
/**
141+
* @return string
142+
*/
143+
public function __toString()
144+
{
145+
# config
146+
$config = Config::getInstance();
147+
$tagIndent = $config->get('tag_indent');
148+
$hideGenid = $config->get('hide_genid');
149+
$tagIndentString = str_repeat($tagIndent, $this->level);
150+
151+
# content
152+
if (in_array($this->nodeName, ['content'])) {
153+
return $tagIndentString . $this->content . PHP_EOL;
154+
}
155+
# root
156+
if (in_array($this->nodeName, ['root'])) {
157+
return $this->_invParseChilds($this->childs);
158+
}
159+
if (empty($this->nodeName))
160+
return '';
161+
162+
# normal
163+
$string = $tagIndentString . '<' . $this->nodeName . ' ';
164+
# id
165+
if (!empty($this->domId))
166+
$string .= ' id="' . $this->domId . '" ';
167+
# gener id
168+
if ($hideGenid == false)
169+
$string .= ' data-genid="' . $this->nodeId . '" ';
170+
# class
171+
if (!empty($this->domAttrs['class'])) {
172+
$string .= ' class="' . implode(' ', $this->domAttrs['class']) . '" ';
173+
unset($this->domAttrs['class']);
174+
}
175+
# style
176+
if (!empty($this->domAttrs['style'])) {
177+
// $string .= implode(' ', $this->domAttrs['class']);
178+
unset($this->domAttrs['style']);
179+
}
180+
# attrs
181+
if (!empty($this->domAttrs)) {
182+
foreach ($this->domAttrs as $k => $v) {
183+
$string .= ' ' . $k . '="' . $v . '" ';
184+
}
185+
}
186+
# is single
187+
if ($this->isSingle == true)
188+
return $string . ' />' . PHP_EOL;
189+
190+
# start tag end
191+
$string .= ' >' . PHP_EOL ;
192+
193+
if (!empty($this->childs))
194+
$string .= $this->_invParseChilds($this->childs);
195+
196+
# end
197+
$string .= $tagIndentString . '</' . $this->nodeName . '>' . PHP_EOL;
198+
199+
return $string;
200+
}
201+
202+
private function _invParseChilds($childs)
203+
{
204+
if (empty($childs))
205+
return '';
206+
207+
$string = '';
208+
foreach ($childs as $child) {
209+
$string .= (string) $child;
210+
}
211+
return $string;
212+
}
140213

141214
}

src/DomParser/Parser.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ class Parser
88
# config obj
99
private $_config;
1010

11-
# set config obj
12-
public function config($config = null)
11+
#
12+
private function _getConfig()
1313
{
14-
$this->_config = $config;
15-
14+
$this->_config = Config::getInstance();
1615
#
1716
$this->_startTagReg = $this->_config->get('start_tag_reg');
1817
$this->_endTagReg = $this->_config->get('end_tag_reg');
@@ -30,6 +29,9 @@ public function config($config = null)
3029
*/
3130
public function parse($domString = '')
3231
{
32+
# init parse config
33+
$this->_getConfig();
34+
3335
$root = Node::generRootNode();
3436

3537
$domString = trim($domString);
@@ -53,13 +55,11 @@ private function _parseChildNodes($domString = '', $parent = null)
5355
$stringLen = strlen($domString);
5456
# match start tad
5557
list($domString, $node) = $this->_parseStartTag($domString, $parent);
56-
// $domString = $this->_parseStartTag($domString, $parent);
5758

5859
# match content
5960
$domString = $this->_parseContent($domString, $parent);
6061

6162
# match end tag
62-
// $domString = $this->_parseEndTag($domString, $parent);
6363
$domString = $this->_parseEndTag($domString, $node);
6464

6565
if ($stringLen == strlen($domString))

tests/DomParserTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
$html = <<< 'HTML'
1010
<template>
11-
<!-- doc commnet -->
12-
<view class="container">
11+
<!-- doc hello
12+
world
13+
-->
14+
<view class="container" id="main-view" data-genid="12323">
1315
<navigator url="/pages/show?id=1">
14-
<image src="../../static/images/home-by.jpg" mode="widthFix"></image>
16+
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
1517
</navigator>
1618
<view class="layout">
1719
<view class="scan">
@@ -26,7 +28,8 @@
2628
</template>
2729
HTML;
2830

29-
echo '<pre>';
30-
$dom = $parser->setConfig([])->parse($html);
31+
//echo '<pre>';
32+
$dom = $parser->setConfig([
33+
])->parse($html);
3134

3235
print_r($dom);

0 commit comments

Comments
 (0)