|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace loophp\phptree\Exporter; |
|
11
|
|
|
|
|
12
|
|
|
use CachingIterator; |
|
13
|
|
|
use loophp\phptree\Node\NodeInterface; |
|
14
|
|
|
use RecursiveArrayIterator; |
|
15
|
|
|
use RecursiveTreeIterator; |
|
16
|
|
|
|
|
17
|
|
|
use const PHP_EOL; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Ascii. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class Ascii implements ExporterInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
public function export(NodeInterface $node): string |
|
25
|
|
|
{ |
|
26
|
1 |
|
$exporter = new SimpleArray(); |
|
27
|
|
|
|
|
28
|
1 |
|
$tree = new RecursiveTreeIterator( |
|
29
|
1 |
|
new RecursiveArrayIterator( |
|
30
|
1 |
|
$exporter->export($node) |
|
31
|
|
|
), |
|
32
|
1 |
|
RecursiveTreeIterator::SELF_FIRST, |
|
33
|
1 |
|
CachingIterator::CATCH_GET_CHILD, |
|
34
|
1 |
|
RecursiveTreeIterator::SELF_FIRST |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_LEFT, ''); |
|
38
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_END_HAS_NEXT, '├─'); |
|
39
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_END_LAST, '└─'); |
|
40
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_RIGHT, ''); |
|
41
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_LAST, ' '); |
|
42
|
1 |
|
$tree->setPrefixPart(RecursiveTreeIterator::PREFIX_MID_HAS_NEXT, '│ '); |
|
43
|
|
|
|
|
44
|
1 |
|
$output = ''; |
|
45
|
|
|
|
|
46
|
1 |
|
foreach ($tree as $value) { |
|
47
|
1 |
|
$entry = ('Array' === $entry = $tree->getEntry()) ? |
|
48
|
1 |
|
'┐' : |
|
49
|
1 |
|
' ' . $entry; |
|
50
|
|
|
|
|
51
|
1 |
|
$output .= $tree->getPrefix() . $entry . $tree->getPostfix() . PHP_EOL; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $output; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|