locale_icu   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 239
rs 10
c 7
b 0
f 0
wmc 25

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A build_locale_select_menu() 0 11 2
A is_locale_configured() 0 3 2
A process_locales() 0 5 2
A icu_available_features() 0 3 2
A icu_requirements() 0 3 2
A set_intl_detected() 0 3 1
A icu_version_compare() 0 5 1
A numfmt_create() 0 3 1
A set_intl_info() 0 4 1
A assign_locale_to_template() 0 6 1
A locale_get_default() 0 3 2
A get_locale_list() 0 10 2
A get_php_extension_version() 0 9 2
A get_currency_symbol() 0 10 2
A numfmt_format_currency() 0 3 1
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\actions;
12
13
use phpbb\config\config;
14
use phpbb\template\template;
15
use phpbb\user;
16
17
class locale_icu
18
{
19
	protected $config;
20
	protected $template;
21
	protected $user;
22
23
	/**
24
	 * locale_icu constructor.
25
	 *
26
	 * @param config   $config
27
	 * @param template $template Template object
28
	 * @param user     $user     User object
29
	 *
30
	 * @access public
31
	 */
32
33
	public function __construct(config $config, template $template, user $user)
34
	{
35
		$this->config = $config;
36
		$this->template = $template;
37
		$this->user = $user;
38
	}
39
40
	/**
41
	 * Build pull down menu options of available locales
42
	 *
43
	 * @param string $config_value Locale identifier; default: ''
44
	 *
45
	 * @return void
46
	 * @access public
47
	 */
48
	public function build_locale_select_menu($config_value = ''): void
49
	{
50
		if (!$this->icu_requirements())
51
		{
52
			return;
53
		}
54
55
		// Grab the list of all available locales
56
		$locale_list = $this->get_locale_list();
57
58
		$this->process_locales($locale_list, $config_value);
59
	}
60
61
	/**
62
	 * Process each locale item for pull-down
63
	 *
64
	 * @param array  $locale_list
65
	 * @param string $config_value
66
	 */
67
	private function process_locales(array $locale_list, string $config_value): void
68
	{
69
		foreach ($locale_list as $locale => $locale_name)
70
		{
71
			$this->assign_locale_to_template($locale, $locale_name, $config_value);
72
		}
73
	}
74
75
	/**
76
	 * Assign locale options to the template
77
	 *
78
	 * @param string $locale
79
	 * @param string $locale_name
80
	 * @param string $config_value
81
	 */
82
	private function assign_locale_to_template(string $locale, string $locale_name, string $config_value): void
83
	{
84
		$this->template->assign_block_vars('locale_options', [
85
			'LOCALE_ID'        => $locale,
86
			'LOCALE_NAME'      => $locale_name,
87
			'S_LOCALE_DEFAULT' => $config_value === $locale,
88
		]);
89
	}
90
91
	/**
92
	 * Checks if the PHP PECL intl extension is fully available
93
	 *
94
	 * @return bool
95
	 * @access public
96
	 */
97
	public function icu_requirements(): bool
98
	{
99
		return $this->config['ppde_intl_version_valid'] && $this->config['ppde_intl_detected'];
100
	}
101
102
	/**
103
	 * Build an array of all locales
104
	 *
105
	 * @return array
106
	 * @access private
107
	 */
108
	private function get_locale_list(): array
109
	{
110
		foreach (\ResourceBundle::getLocales('') as $locale)
111
		{
112
			$locale_ary[$locale] = \Locale::getDisplayName($locale, $this->user->lang_name);
113
		}
114
115
		natsort($locale_ary);
116
117
		return $locale_ary;
118
	}
119
120
	/**
121
	 * Gets the default Locale
122
	 *
123
	 * @return string A string with the current Locale.
124
	 */
125
	public function locale_get_default(): string
126
	{
127
		return $this->icu_requirements() ? \locale_get_default() : '';
128
	}
129
130
	/**
131
	 * Gets the currency symbol based on ISO code
132
	 *
133
	 * @param string $currency_iso_code
134
	 *
135
	 * @return string
136
	 * @access public
137
	 */
138
	public function get_currency_symbol(string $currency_iso_code): string
139
	{
140
		try
141
		{
142
			$fmt = new \NumberFormatter($this->config['ppde_default_locale'] . '@currency=' . $currency_iso_code, \NumberFormatter::CURRENCY);
143
			return $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
144
		}
145
		catch (\Exception $e)
146
		{
147
			return "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
148
		}
149
	}
150
151
	/**
152
	 * Checks if the PPDE locale feature is configured
153
	 *
154
	 * @return bool
155
	 * @access public
156
	 */
157
	public function is_locale_configured(): bool
158
	{
159
		return $this->icu_requirements() && !empty($this->config['ppde_default_locale']);
160
	}
161
162
	/**
163
	 * Creates a number formatter
164
	 *
165
	 * @return \NumberFormatter NumberFormatter object or FALSE on error.
166
	 * @access public
167
	 */
168
	public function numfmt_create()
169
	{
170
		return numfmt_create($this->config['ppde_default_locale'], \NumberFormatter::CURRENCY);
171
	}
172
173
	/**
174
	 * Format a currency value
175
	 *
176
	 * @param \NumberFormatter $fmt
177
	 * @param float            $value
178
	 * @param string           $currency_iso_code
179
	 *
180
	 * @return false|string
181
	 * @access public
182
	 */
183
	public function numfmt_format_currency($fmt, float $value, string $currency_iso_code)
184
	{
185
		return numfmt_format_currency($fmt, $value, $currency_iso_code);
186
	}
187
188
	/**
189
	 * Sets config value for PHP Intl extension version
190
	 *
191
	 * @return void
192
	 * @throws \ReflectionException
193
	 * @access public
194
	 */
195
	public function set_intl_info(): void
196
	{
197
		$this->config->set('ppde_intl_version', $this->get_php_extension_version('intl', $this->icu_available_features()));
198
		$this->config->set('ppde_intl_version_valid', (int) $this->icu_version_compare());
199
	}
200
201
	/**
202
	 * Gets extension version
203
	 *
204
	 * @param string $name
205
	 * @param bool   $proceed
206
	 *
207
	 * @return string
208
	 * @throws \ReflectionException
209
	 * @access private
210
	 */
211
	private function get_php_extension_version($name, $proceed): string
212
	{
213
		$version = '';
214
		if ($proceed)
215
		{
216
			$ext = new \ReflectionExtension($name);
217
			$version = $ext->getVersion();
218
		}
219
		return $version;
220
	}
221
222
	/**
223
	 * Checks if the required function/class are available.
224
	 *
225
	 * @return bool
226
	 * @access private
227
	 */
228
	private function icu_available_features(): bool
229
	{
230
		return class_exists(\ResourceBundle::class) && function_exists('\locale_get_default');
231
	}
232
233
	/**
234
	 * Checks if ICU version matches with requirement
235
	 *
236
	 * @return bool|int
237
	 * @throws \ReflectionException
238
	 * @access private
239
	 */
240
	private function icu_version_compare()
241
	{
242
		$icu_min_version = '1.1.0';
243
		$icu_version = $this->get_php_extension_version('intl', $this->icu_available_features());
244
		return version_compare($icu_version, $icu_min_version, '>=');
245
	}
246
247
	/**
248
	 * Sets config value for cURL and fsockopen
249
	 *
250
	 * @return void
251
	 * @access public
252
	 */
253
	public function set_intl_detected(): void
254
	{
255
		$this->config->set('ppde_intl_detected', $this->icu_available_features());
256
	}
257
}
258