Lines 63.79% 37 / 58
Methods 66.66% 2 / 3
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 renderSelectLanguage 0.00% 0 / 21 0.00% 0 / 1 42
 getAvailableLanguages 100.00% 19 / 19 100.00% 1 / 1 5
 renderLanguageOptions 100.00% 18 / 18 100.00% 1 / 1 8
30class LanguageHelper
31{
32    /**
33     * This function displays the <select> box for the available languages
34     * optionally filtered by excluding some provided languages.
35     *
36     * @param  string[]  $excludedLanguages
37     */
38    public static function renderSelectLanguage(
39        string $default,
40        bool $submitOnChange = false,
41        array $excludedLanguages = [],
42        string $id = 'language',
43    ): string {
44        $output = sprintf(
45            '<select class="form-select" name="%s" aria-label="%s" id="%s" %s>',
46            $id,
47            ucfirst($id),
48            $id,
49            $submitOnChange ? ' onchange="this.form.submit();"' : '',
50        );
51        $languages = self::getAvailableLanguages();
52
53        if ($languages === []) {
54            $output .= sprintf('<option value="en">%s</option>', LanguageCodes::get('en'));
55            return $output . '</select>';
56        }
57
58        foreach ($languages as $lang => $value) {
59            if (in_array($lang, $excludedLanguages, strict: true)) {
60                continue;
61            }
62
63            $output .= sprintf(
64                '<option value="%s" %s>%s</option>',
65                $lang,
66                $lang === $default ? 'selected' : '',
67                $value,
68            );
69        }
70
71        return $output . '</select>';
72    }
73
74    /**
75     * This function returns the available languages.
76     *
77     * @return string[]
78     */
79    public static function getAvailableLanguages(): array
80    {
81        $search = ['language_', '.php'];
82        $languages = [];
83        $languageFiles = [];
84
85        $dir = new DirectoryIterator(PMF_TRANSLATION_DIR);
86        foreach ($dir as $fileInfo) {
87            if ($fileInfo->isDot()) {
88                continue;
89            }
90
91            $languageFiles[] = strtoupper(str_replace(
92                search: $search,
93                replace: '',
94                subject: trim($fileInfo->getFilename()),
95            ));
96        }
97
98        foreach ($languageFiles as $languageFile) {
99            // Check if the file is related to a (real) language before using it
100            $isValidLanguage = LanguageCodes::get($languageFile);
101            if ($isValidLanguage !== null) {
102                $languages[strtolower($languageFile)] = $isValidLanguage;
103            }
104        }
105
106        // Sort the languages list
107        asort($languages);
108        reset($languages);
109
110        return $languages;
111    }
112
113    /**
114     * Function for displaying all languages in <option>.
115     *
116     * @param string $lang              the language to be selected
117     * @param bool   $onlyThisLang      print only the passed language?
118     * @param bool   $fileLanguageValue print the <language file> instead of the <language code> as value?
119     */
120    public static function renderLanguageOptions(
121        string $lang = '',
122        bool $onlyThisLang = false,
123        bool $fileLanguageValue = false,
124    ): string {
125        $output = '';
126        foreach (LanguageHelper::getAvailableLanguages() as $key => $value) {
127            $normalizedKey = strtolower((string) $key);
128            if ($onlyThisLang && $normalizedKey !== $lang) {
129                continue;
130            }
131
132            $languageKey = $normalizedKey;
133            if ($onlyThisLang) {
134                $languageKey = strtolower($lang);
135            }
136
137            $optionValue = $languageKey;
138            if ($fileLanguageValue) {
139                $optionValue = 'language_' . $languageKey . '.php';
140            }
141
142            $output .= "\t<option value=\"" . $optionValue . '"';
143
144            if ($normalizedKey === $lang) {
145                $output .= ' selected="selected"';
146            }
147
148            $output .= '>' . $value . "</option>\n";
149
150            if ($onlyThisLang) {
151                break;
152            }
153        }
154
155        return $output;
156    }
157}