Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
20 / 20
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryCache
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
20 / 20
21
100.00% covered (success)
100.00%
1 / 1
 getCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addCategory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategoryNames
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCategoryNames
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addCategoryName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCategoryName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTreeTab
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTreeTab
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addTreeTabEntry
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateTreeTabEntry
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getTreeTabEntry
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 countTreeTab
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCatTree
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCatTree
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
 setChildren
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addChild
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getChildrenOfParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Cache for category data.
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
22/**
23 * Manages cached category data structures.
24 * Consolidates all internal cache arrays from the legacy Category class.
25 */
26final class CategoryCache
27{
28    /**
29     * The categories as an array.
30     * @var array<int, array<string, mixed>>
31     */
32    private array $categories = [];
33
34    /**
35     * The category names as an array indexed by category ID.
36     * @var array<int, array<string, mixed>>
37     */
38    private array $categoryNames = [];
39
40    /**
41     * The tree with the tabs.
42     * @var array<int, array<string, mixed>>
43     */
44    private array $treeTab = [];
45
46    /**
47     * The category tree.
48     * @var array<int, array<string, mixed>>
49     */
50    private array $catTree = [];
51
52    /**
53     * The children nodes: parentId => [childId => &categoryRow]
54     * @var array<int, array<int, array<string, mixed>>>
55     */
56    private array $children = [];
57
58    /** @return array<int, array<string, mixed>> */
59    public function getCategories(): array
60    {
61        return $this->categories;
62    }
63
64    /** @param array<int, array<string, mixed>> $categories */
65    public function setCategories(array $categories): void
66    {
67        $this->categories = $categories;
68    }
69
70    /** @param array<string, mixed> $category */
71    public function addCategory(int $id, array $category): void
72    {
73        $this->categories[$id] = $category;
74    }
75
76    /** @return array<int, array<string, mixed>> */
77    public function getCategoryNames(): array
78    {
79        return $this->categoryNames;
80    }
81
82    /** @param array<int, array<string, mixed>> $categoryNames */
83    public function setCategoryNames(array $categoryNames): void
84    {
85        $this->categoryNames = $categoryNames;
86    }
87
88    /** @param array<string, mixed> $categoryName */
89    public function addCategoryName(int $id, array $categoryName): void
90    {
91        $this->categoryNames[$id] = $categoryName;
92    }
93
94    /** @return array<string, mixed> */
95    public function getCategoryName(int $id): array
96    {
97        return $this->categoryNames[$id] ?? [];
98    }
99
100    /** @return array<int, array<string, mixed>> */
101    public function getTreeTab(): array
102    {
103        return $this->treeTab;
104    }
105
106    /** @param array<int, array<string, mixed>> $treeTab */
107    public function setTreeTab(array $treeTab): void
108    {
109        $this->treeTab = $treeTab;
110    }
111
112    /** @param array<string, mixed> $entry */
113    public function addTreeTabEntry(array $entry): void
114    {
115        $this->treeTab[] = $entry;
116    }
117
118    /** @param array<string, mixed> $entry */
119    public function updateTreeTabEntry(int $index, array $entry): void
120    {
121        if (array_key_exists($index, $this->treeTab)) {
122            $this->treeTab[$index] = array_merge($this->treeTab[$index], $entry);
123        }
124    }
125
126    /** @return array<string, mixed>|null */
127    public function getTreeTabEntry(int $index): ?array
128    {
129        return $this->treeTab[$index] ?? null;
130    }
131
132    public function countTreeTab(): int
133    {
134        return count($this->treeTab);
135    }
136
137    /** @return array<int, array<string, mixed>> */
138    public function getCatTree(): array
139    {
140        return $this->catTree;
141    }
142
143    /** @param array<int, array<string, mixed>> $catTree */
144    public function setCatTree(array $catTree): void
145    {
146        $this->catTree = $catTree;
147    }
148
149    /** @return array<int, array<int, array<string, mixed>>> */
150    public function getChildren(): array
151    {
152        return $this->children;
153    }
154
155    /** @param array<int, array<int, array<string, mixed>>> $children */
156    public function setChildren(array $children): void
157    {
158        $this->children = $children;
159    }
160
161    /** @param array<string, mixed> $categoryRef */
162    public function addChild(int $parentId, int $childId, array &$categoryRef): void
163    {
164        $this->children[$parentId][$childId] = &$categoryRef;
165    }
166
167    /** @return array<int, array<string, mixed>> */
168    public function getChildrenOfParent(int $parentId): array
169    {
170        return $this->children[$parentId] ?? [];
171    }
172
173    /**
174     * Clears all cached data.
175     */
176    public function clear(): void
177    {
178        $this->categories = [];
179        $this->categoryNames = [];
180        $this->treeTab = [];
181        $this->catTree = [];
182        $this->children = [];
183    }
184}