Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.43% covered (danger)
30.43%
7 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminCategoryTreePresenter
30.43% covered (danger)
30.43%
7 / 23
0.00% covered (danger)
0.00%
0 / 2
18.12
0.00% covered (danger)
0.00%
0 / 1
 transform
36.84% covered (danger)
36.84%
7 / 19
0.00% covered (danger)
0.00%
0 / 1
8.03
 getSymbol
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Category tree presenter class
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-18
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Category\Presentation;
21
22use phpMyFAQ\Category\Tree\TreeBuilder;
23
24/**
25 * Presenter for admin category tree data (non-HTML).
26 * Produces entries equivalent to the legacy Category::transform additions.
27 */
28final class AdminCategoryTreePresenter
29{
30    /**
31     * @param array<int, array<string, mixed>> $categoryName Map id => row
32     * @param array<int, array<int, array<string, mixed>>> $childrenMap Parent => [childId => row]
33     * @return array<int, array<string, mixed>> linear list of entries
34     */
35    public function transform(TreeBuilder $treeBuilder, array $categoryName, array $childrenMap, int $categoryId): array
36    {
37        $entries = [];
38
39        $category = $categoryName[$categoryId] ?? [];
40        $parentId = (int) ($category['parent_id'] ?? 0);
41        $children = $childrenMap[$categoryId] ?? [];
42
43        if ($categoryId > 0 && $category !== []) {
44            $entry = $category;
45            $entry['level'] = $treeBuilder->computeLevel($categoryName, $categoryId);
46            $entry['children'] = array_keys($children);
47            $entry['tree'] = $treeBuilder->buildTree($categoryName, $childrenMap, $categoryId);
48            $entry['symbol'] = $this->getSymbol($childrenMap, $categoryId, $parentId);
49            $entries[] = $entry;
50        }
51
52        foreach (array_keys($children) as $childId) {
53            $entries = array_merge($entries, $this->transform(
54                $treeBuilder,
55                $categoryName,
56                $childrenMap,
57                (int) $childId,
58            ));
59        }
60
61        return $entries;
62    }
63
64    /**
65     * Returns the symbol for a category based on its position among its siblings.
66     *
67     * @param array<int, array<int, mixed>> $childrenMap
68     */
69    private function getSymbol(array $childrenMap, int $categoryId, int $parentId): string
70    {
71        $siblings = $childrenMap[$parentId] ?? [];
72        $keys = array_keys($siblings);
73        $last = end($keys);
74        return $categoryId === $last ? 'angle' : 'medium';
75    }
76}