-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTreeRenderer.php
More file actions
33 lines (29 loc) · 805 Bytes
/
Copy pathTreeRenderer.php
File metadata and controls
33 lines (29 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
namespace DesignPatterns\Structural\Adapter;
/**
* Client class that outputs a tree structure of any @see Node object.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
class TreeRenderer
{
/**
* Outputs a string representation of the tree structure object.
*
* @param Node $tree
* @param int $level
*
* @return string
*/
public function render(Node $tree, $level = 0)
{
// Output a current node at a current level.
$output = str_repeat('--', $level).$tree->label()."\n";
// For every child of current node
foreach ($tree->children() as $child) {
// Output it at the next level with all its children.
$output .= $this->render($child, $level + 1);
}
return $output;
}
}