Failed Conditions
Push — master ( b549f2...090668 )
by Remco
10:21 queued 03:46
created

LocaleHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
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 61
ccs 11
cts 12
cp 0.9167
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 54 4
1
<?php
2
/**
3
 * Mollie locale helper.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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-2020 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
		// Supported locales.
36
		$supported = array(
37 11
			Locales::EN_US,
38
			Locales::NL_NL,
39
			Locales::NL_BE,
40
			Locales::FR_FR,
41
			Locales::FR_BE,
42
			Locales::DE_DE,
43
			Locales::DE_AT,
44
			Locales::DE_CH,
45
			Locales::ES_ES,
46
			Locales::CA_ES,
47
			Locales::PT_PT,
48
			Locales::IT_IT,
49
			Locales::NB_NO,
50
			Locales::SV_SE,
51
			Locales::FI_FI,
52
			Locales::DA_DK,
53
			Locales::IS_IS,
54
			Locales::HU_HU,
55
			Locales::PL_PL,
56
			Locales::LV_LV,
57
			Locales::LT_LT,
58
		);
59
60
		// Lower case.
61 11
		$locale = strtolower( $locale );
62
63
		// Is supported?
64 11
		$supported_lowercase = array_map( 'strtolower', $supported );
65
66 11
		$search = array_search( $locale, $supported_lowercase, true );
67
68
		// Locale not supported.
69 11
		if ( false === $search ) {
70 8
			return null;
71
		}
72
73
		/**
74
		 * As with all internal PHP functions as of 5.3.0, `array_search()`
75
		 * returns `NULL` if invalid parameters are passed to it.
76
		 *
77
		 * @link https://www.php.net/array_search
78
		 */
79 3
		if ( array_key_exists( $search, $supported ) ) {
80 3
			return $supported[ $search ];
81
		}
82
83
		return null;
84
	}
85
}
86