Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
20 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MenuBuilder
90.91% covered (success)
90.91%
20 / 22
66.67% covered (warning)
66.67%
2 / 3
8.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRoot
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addLeaf
90.00% covered (success)
90.00%
18 / 20
0.00% covered (danger)
0.00%
0 / 1
6.04
1<?php
2namespace Apie\Common\MenuStructure;
3
4use Apie\Core\Lists\StringList;
5use Apie\Core\Translator\ValueObjects\MenuHeader;
6
7class MenuBuilder
8{
9    private MenuNode $root;
10
11    public function __construct(private readonly string $prefix = '')
12    {
13        $this->root = new MenuNode(MenuHeader::createRoot(path: rtrim($this->prefix, '.-')));
14    }
15
16    public function getRoot(): MenuNode
17    {
18        return $this->root;
19    }
20
21    public function addLeaf(string|StringList $tag, MenuNode $leaf): self
22    {
23        if (is_string($tag)) {
24            $tag = new StringList([$tag]);
25        }
26        $parent = null;
27        $current = $this->root;
28        foreach ($tag as $part) {
29            $parent = $current;
30            if ($part) {
31                if (!isset($current->children[$part])) {
32                    $children = $current->children;
33                    $children[$part] = new MenuNode($current->name->createChildNode($part));
34                    $current->children = $children;
35                }
36                $current = $current->children[$part];
37            }
38        }
39        if ($parent === null) {
40            $this->root = $leaf;
41            return $this;
42        }
43        $current->name = $leaf->name;
44        $current->route = $leaf->route;
45        $current->icon = $leaf->icon;
46        $current->allowed = $leaf->allowed;
47
48        return $this;
49    }
50}