Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryLanguageService
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 getLanguagesToTranslate
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getExistingTranslations
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 getAllExistingTranslations
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 getLanguagesInUse
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Category language service 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\Language;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Database;
24use phpMyFAQ\Helper\LanguageHelper;
25use phpMyFAQ\Language\LanguageCodes;
26
27final class CategoryLanguageService
28{
29    /**
30     * Returns languages available to translate for a given category id.
31     * Key = language code (lowercase), Value = display name.
32     *
33     * @return array<string, string>
34     */
35    public function getLanguagesToTranslate(Configuration $configuration, int $categoryId): array
36    {
37        $existing = $configuration->getLanguage()->isLanguageAvailable($categoryId, table: 'faqcategories');
38        $existingLower = array_map(strtolower(...), $existing);
39
40        $result = [];
41        foreach (LanguageHelper::getAvailableLanguages() as $lang => $name) {
42            $langLower = strtolower((string) $lang);
43            if (!in_array($langLower, $existingLower, strict: true)) {
44                $result[$langLower] = $name;
45            }
46        }
47
48        return $result;
49    }
50
51    /**
52     * Returns existing translation languages for a given category id.
53     * Key = language code (lowercase), Value = display name.
54     *
55     * @return array<string, string>
56     */
57    public function getExistingTranslations(Configuration $configuration, int $categoryId): array
58    {
59        $existing = $configuration->getLanguage()->isLanguageAvailable($categoryId, table: 'faqcategories');
60        $available = LanguageHelper::getAvailableLanguages();
61
62        $result = [];
63        foreach ($existing as $code) {
64            $codeLower = strtolower($code);
65            $result[$codeLower] = $available[$codeLower] ?? LanguageCodes::get($codeLower) ?? $codeLower;
66        }
67
68        ksort($result);
69        return $result;
70    }
71
72    /**
73     * Returns the translated language codes for every category in one query.
74     *
75     * @return array<int, string[]> category id => language codes
76     */
77    public function getAllExistingTranslations(Configuration $configuration): array
78    {
79        $query = sprintf('SELECT id, lang FROM %sfaqcategories', Database::getTablePrefix());
80        $result = $configuration->getDb()->query($query);
81
82        $map = [];
83        while (true) {
84            $row = $configuration->getDb()->fetchObject($result);
85            if (!$row instanceof \stdClass) {
86                break;
87            }
88
89            $id = (int) $row->id;
90            $map[$id][] = strtolower((string) $row->lang);
91        }
92
93        return $map;
94    }
95
96    /**
97     * Returns all languages currently used by categories (distinct langs in faqcategories).
98     * Key = language code (lowercase), Value = display name.
99     *
100     * @return array<string, string>
101     */
102    public function getLanguagesInUse(Configuration $configuration): array
103    {
104        $all = $configuration->getLanguage()->isLanguageAvailable(identifier: 0, table: 'faqcategories');
105        $result = [];
106        foreach ($all as $code) {
107            $codeLower = strtolower($code);
108            $result[$codeLower] = LanguageCodes::get($codeLower) ?? $codeLower;
109        }
110
111        asort($result);
112        return $result;
113    }
114}