Skip to content

Commit 8b23088

Browse files
author
Emmanuel ROECKER
committed
add tree summary fct
1 parent 7f38d7d commit 8b23088

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

src/GlHtml.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,53 @@ public function getSummary()
254254

255255
return $summary;
256256
}
257+
258+
private function convertHToTree(
259+
&$start,
260+
$summary,
261+
array &$summaryTree
262+
) {
263+
$end = count($summary);
264+
$number = 1;
265+
while ($start < $end) {
266+
$text = $summary[$start]->getNode()->getText();
267+
$id = $summary[$start]->getNode()->getAttribute('id');
268+
269+
$summaryTree[$text] = ['id' => $id, 'children' => []];
270+
if (($start + 1) < $end) {
271+
$currentLevel = $summary[$start]->getLevel();
272+
$nextLevel = $summary[$start + 1]->getLevel();
273+
if ($nextLevel > $currentLevel) {
274+
$start++;
275+
$diff = $this->convertHToTree(
276+
$start,
277+
$summary,
278+
$summaryTree[$text]['children']
279+
);
280+
if ($diff > 0) {
281+
return $diff - 1;
282+
}
283+
} else {
284+
if ($nextLevel < $currentLevel) {
285+
return ($currentLevel - $nextLevel - 1);
286+
}
287+
}
288+
}
289+
$number++;
290+
$start++;
291+
}
292+
293+
return 0;
294+
}
295+
296+
public function getSummaryTree()
297+
{
298+
$summary = $this->getSummary();
299+
$start = 0;
300+
$summaryTree = [];
301+
$this->convertHToTree($start, $summary, $summaryTree);
302+
reset($summaryTree); //reset array pointer
303+
304+
return $summaryTree;
305+
}
257306
}

tests/GlHtmlTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ public function testRecursiveHtml()
7171
$this->assertEquals("subtitle1-2-1", $summary[2]->getNode()->getText());
7272
$this->assertEquals(1, $summary[3]->getLevel());
7373
}
74-
75-
public function testRecursiveHtml2()
74+
75+
public function testHtmlSummaryTree()
7676
{
77+
7778
$html = file_get_contents(__DIR__ . '/testsummary.html');
78-
79+
7980
$dom = new GlHtml($html);
80-
$summary = $dom->getSummary();
81+
$summary = $dom->getSummaryTree();
82+
83+
$expected = unserialize(file_get_contents(__DIR__ . '/expectedSummaryTree.serialize'));
8184

82-
$this->assertEquals("Compatibilité", $summary[10]->getNode()->getText());
83-
$this->assertEquals(2, $summary[10]->getLevel());
85+
$this->assertEquals($expected,$summary);
8486
}
8587

8688
public function testDiv()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a:4:{s:13:"Présentation";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:11:"Description";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:4:"Code";a:2:{s:2:"id";s:0:"";s:8:"children";a:2:{s:14:"Initialisation";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:20:"Affichage des images";a:2:{s:2:"id";s:0:"";s:8:"children";a:5:{s:7:"Etape 1";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:7:"Etape 2";a:2:{s:2:"id";s:0:"";s:8:"children";a:1:{s:12:"sous etape 1";a:2:{s:2:"id";s:0:"";s:8:"children";a:1:{s:12:"sous etape 2";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}}}}}s:7:"Etape 3";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:7:"Etape 4";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}s:7:"Etape 5";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}}}}}s:14:"Compatibilité";a:2:{s:2:"id";s:0:"";s:8:"children";a:0:{}}}

tests/testsummary.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ <h3>Affichage des images</h3>
1515
<div>text1</div>
1616
<h4>Etape 1</h4>
1717
<h4>Etape 2</h4>
18+
<h5>sous etape 1</h5>
19+
<h6>sous etape 2</h6>
1820
<h4>Etape 3</h4>
1921
<h4>Etape 4</h4>
2022
<h4>Etape 5</h4>

0 commit comments

Comments
 (0)