Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.43% covered (success)
91.43%
32 / 35
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
language_file_helper
91.43% covered (success)
91.43%
32 / 35
50.00% covered (danger)
50.00%
2 / 4
16.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_available_languages
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 get_language_data_from_composer_file
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_language_data_from_json
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\language;
15
16use DomainException;
17use phpbb\json\sanitizer as json_sanitizer;
18use Symfony\Component\Finder\Finder;
19
20/**
21 * Helper class for language file related functions
22 */
23class language_file_helper
24{
25    /**
26     * @var string    Path to phpBB's root
27     */
28    protected $phpbb_root_path;
29
30    /**
31     * Constructor
32     *
33     * @param string $phpbb_root_path         Path to phpBB's root
34     *
35     */
36    public function __construct(string $phpbb_root_path)
37    {
38        $this->phpbb_root_path = $phpbb_root_path;
39    }
40
41    /**
42     * Returns available languages
43     *
44     * @return array
45     *
46     * @throws DomainException When one of the languages in language directory
47     *                        could not be loaded or have invalid composer.json data
48     */
49    public function get_available_languages() : array
50    {
51        // Find available language packages
52        $finder = new Finder();
53        $finder->files()
54            ->name('composer.json')
55            ->depth('== 1')
56            ->followLinks()
57            ->in($this->phpbb_root_path . 'language');
58
59        $available_languages = array();
60        foreach ($finder as $file)
61        {
62            $json = $file->getContents();
63            $data = json_sanitizer::decode($json);
64
65            $available_languages[] = $this->get_language_data_from_json($data);
66        }
67
68        return $available_languages;
69    }
70
71    /**
72     * Collect some data from the composer.json file
73     *
74     * @param string $path
75     * @return array
76     *
77     * @throws DomainException When unable to language data from composer.json
78     */
79    public function get_language_data_from_composer_file(string $path) : array
80    {
81        $json_data = file_get_contents($path);
82        return $this->get_language_data_from_json(json_sanitizer::decode($json_data));
83    }
84
85    /**
86     * Collect some data from the composer.json data
87     *
88     * @param array $data
89     * @return array
90     *
91     * @throws DomainException When composer.json data is invalid for language files
92     */
93    protected function get_language_data_from_json(array $data) : array
94    {
95        if (!isset($data['extra']['language-iso']) || !isset($data['extra']['english-name']) || !isset($data['extra']['local-name']) || !isset($data['extra']['direction']) || !isset($data['extra']['user-lang']) || !isset($data['extra']['plural-rule']) || !isset($data['extra']['recaptcha-lang']))
96        {
97            throw new DomainException('INVALID_LANGUAGE_PACK');
98        }
99
100        $authors = [];
101        if (isset($data['authors']))
102        {
103            foreach ($data['authors'] as $author)
104            {
105                if (isset($author['name']) && $author['name'] !== '')
106                {
107                    $authors[] = $author['name'];
108                }
109            }
110        }
111
112        return [
113            'iso'                => $data['extra']['language-iso'],
114            'name'                => $data['extra']['english-name'],
115            'local_name'        => $data['extra']['local-name'],
116            'author'            => implode(', ', $authors),
117            'version'            => $data['version'],
118            'phpbb_version'        => $data['extra']['phpbb-version'],
119            'direction'            => $data['extra']['direction'],
120            'user_lang'            => $data['extra']['user-lang'],
121            'plural_rule'        => $data['extra']['plural-rule'],
122            'recaptcha_lang'    => $data['extra']['recaptcha-lang'],
123            'turnstile_lang'    => $data['extra']['turnstile-lang'] ?? '',
124        ];
125    }
126}