Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TreeVisualizer
90.00% covered (success)
90.00%
9 / 10
50.00% covered (danger)
50.00%
1 / 2
5.03
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
 buildTree
88.89% covered (success)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
4.02
1<?php
2
3/**
4 * Builds visual representations of category trees.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2025 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2025-10-19
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Category\Tree;
21
22final readonly class TreeVisualizer
23{
24    public function __construct(
25        private TreePathResolver $treePathResolver,
26    ) {
27    }
28
29    /**
30     * Builds branch visuals (vertical/space) for a category path.
31     *
32     * @param array<int, array<string, mixed>> $categoryName
33     * @param array<int, array<int, array<string, mixed>>> $childrenMap
34     * @return array<int, string>
35     */
36    public function buildTree(array $categoryName, array $childrenMap, int $categoryId): array
37    {
38        $ascendants = $this->treePathResolver->getNodes($categoryName, $categoryId);
39        $tree = [];
40        foreach ($ascendants as $i => $ascendantId) {
41            if ($ascendantId === 0) {
42                break;
43            }
44
45            $brothers = $this->treePathResolver->getBrothers($categoryName, $childrenMap, (int) $ascendantId);
46            $last = end($brothers);
47            $tree[(int) $i] = $ascendantId === $last ? 'space' : 'vertical';
48        }
49
50        return $tree;
51    }
52}