Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryTreeFacade
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildLinearTree
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildAdminCategoryTree
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChildren
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChildNodes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLevelOf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNodes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Facade for category tree building operations.
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-20
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Category;
21
22use phpMyFAQ\Category\Tree\TreeBuilder;
23
24/**
25 * Facade for category tree building operations.
26 * Consolidates tree-building methods from the legacy Category class.
27 */
28final class CategoryTreeFacade
29{
30    public function __construct(
31        private TreeBuilder $treeBuilder = new TreeBuilder(),
32    ) {
33    }
34
35    /**
36     * Builds a linear category tree with indentation.
37     *
38     * @param array<int, array<string, mixed>> $categories
39     * @return array<int, array<string, mixed>>
40     */
41    public function buildLinearTree(array $categories, int $parentId = 0, int $indent = 0): array
42    {
43        return $this->treeBuilder->buildLinearTree($categories, $parentId, $indent);
44    }
45
46    /**
47     * Builds the admin category tree.
48     *
49     * @param array<int, array<string, mixed>> $categories
50     * @return array<int, array<array-key, mixed>>
51     */
52    public function buildAdminCategoryTree(array $categories, int $parentId = 0): array
53    {
54        return $this->treeBuilder->buildAdminCategoryTree($categories, $parentId);
55    }
56
57    /**
58     * Gets direct children of a category.
59     *
60     * @param array<int, array<int, array<string, mixed>>> $childrenMap
61     * @return array<int>
62     */
63    public function getChildren(array $childrenMap, int $categoryId): array
64    {
65        return $this->treeBuilder->getChildren($childrenMap, $categoryId);
66    }
67
68    /**
69     * Gets all descendant nodes of a category.
70     *
71     * @param array<int, array<int, array<string, mixed>>> $childrenMap
72     * @return array<int>
73     */
74    public function getChildNodes(array $childrenMap, int $categoryId): array
75    {
76        return $this->treeBuilder->getChildNodes($childrenMap, $categoryId);
77    }
78
79    /**
80     * Computes the depth level of a category.
81     *
82     * @param array<int, array<string, mixed>> $categoryNames
83     */
84    public function getLevelOf(array $categoryNames, int $categoryId): int
85    {
86        return $this->treeBuilder->computeLevel($categoryNames, $categoryId);
87    }
88
89    /**
90     * Gets the path from root to a category.
91     *
92     * @param array<int, array<string, mixed>> $categoryNames
93     * @return array<int>
94     */
95    public function getNodes(array $categoryNames, int $categoryId): array
96    {
97        return $this->treeBuilder->getNodes($categoryNames, $categoryId);
98    }
99}