LocaleHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 33
c 4
b 0
f 0
dl 0
loc 65
ccs 11
cts 12
cp 0.9167
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 58 4
1
<?php
2
/**
3
 * Mollie locale helper.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
/**
14
 * Title: Mollie locale helper
15
 * Description:
16
 * Copyright: 2005-2022 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.0.9
21
 * @since   1.0.0
22
 */
23
class LocaleHelper {
24
	/**
25
	 * Get Mollie locale by the specified WordPress locale.
26
	 *
27
	 * @param string|null $locale Locale string (en_US) to transform to Mollie locale.
28
	 * @return string|null
29
	 */
30 12
	public static function transform( $locale ) {
31 12
		if ( ! \is_string( $locale ) ) {
32 1
			return null;
33
		}
34
35
		/**
36
		 * Supported locales.
37
		 *
38
		 * @var array<int, string>
39
		 */
40
		$supported = array(
41 11
			Locales::EN_US,
42
			Locales::NL_NL,
43
			Locales::NL_BE,
44
			Locales::FR_FR,
45
			Locales::FR_BE,
46
			Locales::DE_DE,
47
			Locales::DE_AT,
48
			Locales::DE_CH,
49
			Locales::ES_ES,
50
			Locales::CA_ES,
51
			Locales::PT_PT,
52
			Locales::IT_IT,
53
			Locales::NB_NO,
54
			Locales::SV_SE,
55
			Locales::FI_FI,
56
			Locales::DA_DK,
57
			Locales::IS_IS,
58
			Locales::HU_HU,
59
			Locales::PL_PL,
60
			Locales::LV_LV,
61
			Locales::LT_LT,
62
		);
63
64
		// Lowercase.
65 11
		$locale = \strtolower( $locale );
66
67
		// Is supported?
68 11
		$supported_lowercase = \array_map( 'strtolower', $supported );
69
70 11
		$search = \array_search( $locale, $supported_lowercase, true );
71
72
		// Locale not supported.
73 11
		if ( false === $search ) {
74 8
			return null;
75
		}
76
77
		/**
78
		 * As with all internal PHP functions as of 5.3.0, `array_search()`
79
		 * returns `NULL` if invalid parameters are passed to it.
80
		 *
81
		 * @link https://www.php.net/array_search
82
		 */
83 3
		if ( \array_key_exists( $search, $supported ) ) {
84 3
			return $supported[ $search ];
85
		}
86
87
		return null;
88
	}
89
}
90