Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.77% covered (danger)
30.77%
8 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CurrencyNames
30.77% covered (danger)
30.77%
8 / 26
0.00% covered (danger)
0.00%
0 / 2
35.88
0.00% covered (danger)
0.00%
0 / 1
 getNames
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 loadLanguage
19.05% covered (danger)
19.05%
4 / 21
0.00% covered (danger)
0.00%
0 / 1
32.99
1<?php
2
3namespace MediaWiki\Extension\CLDR;
4
5use MediaWiki\MediaWikiServices;
6
7/**
8 * A class for querying translated currency names from CLDR data.
9 *
10 * @author Niklas Laxström
11 * @author Ryan Kaldari
12 * @copyright Copyright © 2007-2012
13 * @license GPL-2.0-or-later
14 */
15class CurrencyNames {
16
17    /** @var array */
18    private static $cache = [];
19
20    /**
21     * Get localized currency names for a particular language, using fallback languages for missing
22     * items.
23     *
24     * @param string $code The language to return the list in
25     * @return array an associative array of currency codes and localized currency names
26     */
27    public static function getNames( $code ) {
28        // Load currency names localized for the requested language
29        $names = self::loadLanguage( $code );
30
31        // Load missing currency names from fallback languages
32        $fallbacks = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code );
33        foreach ( $fallbacks as $fallback ) {
34            // Overwrite the things in fallback with what we have already
35            $names = array_merge( self::loadLanguage( $fallback ), $names );
36        }
37
38        return $names;
39    }
40
41    /**
42     * Load currency names localized for a particular language. Helper function for getNames.
43     *
44     * @param string $code The language to return the list in
45     * @return array an associative array of currency codes and localized currency names
46     */
47    private static function loadLanguage( $code ) {
48        if ( !isset( self::$cache[$code] ) ) {
49            $langNameUtils = MediaWikiServices::getInstance()->getLanguageNameUtils();
50
51            if ( !$langNameUtils->isValidBuiltInCode( $code ) ) {
52                return [];
53            }
54
55            /* Load override for wrong or missing entries in cldr */
56            $override = __DIR__ . '/../LocalNames/' .
57                $langNameUtils->getFileName( 'LocalNames', $code );
58            if ( file_exists( $override ) ) {
59                $currencyNames = false;
60                require $override;
61                // @phan-suppress-next-line PhanImpossibleCondition
62                if ( is_array( $currencyNames ) ) {
63                    self::$cache[$code] = $currencyNames;
64                }
65            }
66
67            $filename = __DIR__ . '/../CldrMain/' .
68                $langNameUtils->getFileName( 'CldrMain', $code );
69            if ( file_exists( $filename ) ) {
70                $currencyNames = false;
71                require $filename;
72                // @phan-suppress-next-line PhanImpossibleCondition
73                if ( is_array( $currencyNames ) ) {
74                    self::$cache[$code] ??= [];
75                    // Add to existing list of localized currency names
76                    self::$cache[$code] += $currencyNames;
77                }
78            } else {
79                wfDebug( __METHOD__ . ": Unable to load currency names for $filename\n" );
80            }
81        }
82
83        return self::$cache[$code] ?? [];
84    }
85}