Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MenuNode | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| prune | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\MenuStructure; |
| 3 | |
| 4 | use Apie\Core\Dto\DtoInterface; |
| 5 | use Apie\Core\Translator\ValueObjects\MenuHeader; |
| 6 | |
| 7 | final class MenuNode implements DtoInterface |
| 8 | { |
| 9 | public readonly string $id; |
| 10 | public MenuNodeChildren $children; |
| 11 | public function __construct( |
| 12 | public MenuHeader $name, |
| 13 | public ?string $route = null, |
| 14 | public ?string $icon = null, |
| 15 | ?MenuNodeChildren $children = null, |
| 16 | public bool $allowed = true, |
| 17 | ) { |
| 18 | $this->id = $name->asId(); |
| 19 | $this->children = $children ?? new MenuNodeChildren(); |
| 20 | } |
| 21 | |
| 22 | public function prune(): self |
| 23 | { |
| 24 | $newChildren = []; |
| 25 | foreach ($this->children as $child) { |
| 26 | $child->prune(); |
| 27 | if ($child->allowed || $child->children->count() > 0) { |
| 28 | $newChildren[] = $child; |
| 29 | } |
| 30 | } |
| 31 | $this->children = new MenuNodeChildren($newChildren); |
| 32 | |
| 33 | return $this; |
| 34 | } |
| 35 | } |