Passed
Pull Request — develop-3.2.x (#54)
by Mario
05:24 queued 03:13
created

locale_icu::icu_requirements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2019 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\actions;
12
13
use Locale;
14
use phpbb\config\config;
15
use phpbb\template\template;
16
use phpbb\user;
17
use ResourceBundle;
18
19
class locale_icu
20
{
21
	protected $config;
22
	protected $template;
23
	protected $user;
24
25
	/**
26
	 * locale_icu constructor.
27
	 *
28
	 * @param config   $config
29
	 * @param template $template Template object
30
	 * @param user     $user     User object
31
	 *
32
	 * @access public
33
	 */
34
35
	public function __construct(config $config, template $template, user $user)
36
	{
37
		$this->config = $config;
38
		$this->template = $template;
39
		$this->user = $user;
40
	}
41
42
	/**
43
	 * Build pull down menu options of available locales
44
	 *
45
	 * @param string $config_value Locale identifier; default: ''
46
	 *
47
	 * @return void
48
	 * @access public
49
	 */
50
	public function build_locale_select_menu($config_value = '')
51
	{
52
		// Grab the list of all available locales
53
		$locale_list = $this->get_locale_list();
54
55
		// Process each locale item for pull-down
56
		foreach ($locale_list as $locale => $locale_name)
57
		{
58
			// Set output block vars for display in the template
59
			$this->template->assign_block_vars('locale_options', array(
60
				'LOCALE_ID'        => $locale,
61
				'LOCALE_NAME'      => $locale_name,
62
				'S_LOCALE_DEFAULT' => $config_value === $locale,
63
			));
64
		}
65
		unset ($locale, $locale_list, $locale_name);
66
	}
67
68
	/**
69
	 * Build an array of all locales
70
	 *
71
	 * @return mixed
72
	 * @access private
73
	 */
74
	private function get_locale_list()
75
	{
76
		$locale_items = ResourceBundle::getLocales('');
77
		foreach ($locale_items as $locale)
78
		{
79
			$locale_ary[$locale] = Locale::getDisplayName($locale, $this->user->lang_name);
80
		}
81
		unset ($locale_items, $locale);
82
83
		natsort($locale_ary);
84
85
		return $locale_ary;
86
	}
87
88
	/**
89
	 * Gets the default Locale
90
	 *
91
	 * @return string A string with the current Locale.
92
	 */
93
	public function locale_get_default()
94
	{
95
		return $this->icu_requirements() ? \locale_get_default() : '';
96
	}
97
98
	/**
99
	 * Checks if the PHP PECL intl extension is fully available
100
	 *
101
	 * @return bool
102
	 * @access public
103
	 */
104
	public function icu_requirements()
105
	{
106
		return (bool) $this->config['ppde_intl_version_valid'] && $this->config["ppde_intl_detected"];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ppde_intl_detected 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...
107
	}
108
109
	/**
110
	 * Gets the currency symbol based on ISO code
111
	 *
112
	 * @param $currency_iso_code
113
	 *
114
	 * @return string
115
	 * @access public
116
	 */
117
	public function get_currency_symbol($currency_iso_code)
118
	{
119
		$fmt = new \NumberFormatter($this->config['ppde_default_locale'] . "@currency=" . $currency_iso_code, \NumberFormatter::CURRENCY);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal @currency= 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...
120
		return $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
121
	}
122
123
	/**
124
	 * Checks if the PPDE locale feature is configured
125
	 *
126
	 * @return bool.
0 ignored issues
show
Documentation Bug introduced by
The doc comment bool. at position 0 could not be parsed: Unknown type name 'bool.' at position 0 in bool..
Loading history...
127
	 * @access public
128
	 */
129
	public function is_locale_configured()
130
	{
131
		return $this->icu_requirements() && !empty($this->config['ppde_default_locale']);
132
	}
133
134
	/**
135
	 * Creates a number formatter
136
	 *
137
	 * @return \NumberFormatter NumberFormatter object or FALSE on error.
138
	 * @access public
139
	 */
140
	public function numfmt_create()
141
	{
142
		return numfmt_create($this->config['ppde_default_locale'], \NumberFormatter::CURRENCY);
143
	}
144
145
	/**
146
	 * Format a currency value
147
	 *
148
	 * @param \NumberFormatter $fmt
149
	 * @param float            $value
150
	 * @param string           $currency_iso_code
151
	 *
152
	 * @return \phpbb\config\config
153
	 * @access public
154
	 */
155
	public function numfmt_format_currency($fmt, $value, $currency_iso_code)
156
	{
157
		return numfmt_format_currency($fmt, (float) $value, (string) $currency_iso_code);
158
	}
159
160
	/**
161
	 * Sets config value for PHP Intl extension version
162
	 *
163
	 * @return void
164
	 * @throws \ReflectionException
165
	 * @access public
166
	 */
167
	public function set_intl_info()
168
	{
169
		$this->config->set('ppde_intl_version', $this->get_php_extension_version('intl', $this->icu_available_features()));
170
		$this->config->set('ppde_intl_version_valid', (int) $this->icu_version_compare());
171
	}
172
173
	/**
174
	 * Gets extension version
175
	 *
176
	 * @param string $name
177
	 * @param bool   $proceed
178
	 *
179
	 * @return string
180
	 * @throws \ReflectionException
181
	 * @access private
182
	 */
183
	private function get_php_extension_version($name, $proceed)
184
	{
185
		$version = '';
186
		if ($proceed)
187
		{
188
			$ext = new \ReflectionExtension($name);
189
			$version = $ext->getVersion();
190
		}
191
		return $version;
192
	}
193
194
	/**
195
	 * Checks if the required function/class are available.
196
	 *
197
	 * @return bool
198
	 * @access private
199
	 */
200
	private function icu_available_features()
201
	{
202
		return class_exists('\ResourceBundle') && function_exists('\locale_get_default');
203
	}
204
205
	/**
206
	 * Checks if ICU version matches with requirement
207
	 *
208
	 * @return bool
209
	 * @throws \ReflectionException
210
	 * @access private
211
	 */
212
	private function icu_version_compare()
213
	{
214
		$icu_min_version = '1.1.0';
215
		$icu_version = $this->get_php_extension_version('intl', $this->icu_available_features());
216
		return version_compare($icu_version, $icu_min_version, '>=');
217
	}
218
219
	/**
220
	 * Sets config value for cURL and fsockopen
221
	 *
222
	 * @return void
223
	 * @access public
224
	 */
225
	public function set_intl_detected()
226
	{
227
		$this->config->set('ppde_intl_detected', $this->icu_available_features());
228
	}
229
}
230