Converter::instance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Recca0120\Lodash\JString\Extensions;
4
5
use ArrayObject;
6
7
class Converter
8
{
9
    /**
10
     * $instance.
11
     *
12
     * @var static
13
     */
14
    protected static $instance;
15
16
    /**
17
     * $table.
18
     *
19
     * @var array
20
     */
21
    protected $table = [];
22
23
    /**
24
     * $default.
25
     *
26
     * @var string
27
     */
28
    protected $default = 'zh-tw';
29
30
    /**
31
     * __construct.
32
     */
33 2
    protected function __construct()
34
    {
35
36
        /**
37
         * 'zh-hans' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2Hans),
38
         * 'zh-hant' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2Hant),
39
         * 'zh-cn' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2CN),
40
         * 'zh-hk' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2HK),
41
         * 'zh-mo' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2HK),
42
         * 'zh-my' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2CN),
43
         * 'zh-sg' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2CN),
44
         * 'zh-tw' => new ArrayObject(\MediaWiki\Languages\Data\ZhConversion::$zh2TW),
45
         * 'zh' => new ArrayObject([]),.
46
         */
47 2
        $path = __DIR__.'/../../../resources/Mediawiki/Languages/Data/';
48
49 2
        $this->table = [
50 2
            'zh-hans' => new ArrayObject(require($path.'/zh2Hans.php')),
51 2
            'zh-hant' => new ArrayObject(require($path.'/zh2Hant.php')),
52 2
            'zh-cn' => new ArrayObject(require($path.'/zh2CN.php')),
53 2
            'zh-hk' => new ArrayObject(require($path.'/zh2HK.php')),
54 2
            'zh-tw' => new ArrayObject(require($path.'/zh2TW.php')),
55 2
            'zh' => new ArrayObject([]),
56
        ];
57
58 2
        $this->table['zh-mo'] = $this->table['zh-hk'];
59 2
        $this->table['zh-my'] = $this->table['zh-cn'];
60 2
        $this->table['zh-sg'] = $this->table['zh-cn'];
61
62 2
        $this->table['zh-cn'] = new ArrayObject(
63 2
            $this->table['zh-cn']->getArrayCopy() + $this->table['zh-hans']->getArrayCopy()
64 2
        );
65
66 2
        $this->table['zh-hk'] = new ArrayObject(
67 2
            $this->table['zh-hk']->getArrayCopy() + $this->table['zh-hant']->getArrayCopy()
68 2
        );
69
70 2
        $this->table['zh-my'] = new ArrayObject(
71 2
            $this->table['zh-my']->getArrayCopy() + $this->table['zh-hans']->getArrayCopy()
72 2
        );
73
74 2
        $this->table['zh-sg'] = new ArrayObject(
75 2
            $this->table['zh-sg']->getArrayCopy() + $this->table['zh-hans']->getArrayCopy()
76 2
        );
77
78 2
        $this->table['zh-tw'] = new ArrayObject(
79 2
            $this->table['zh-tw']->getArrayCopy() + $this->table['zh-hant']->getArrayCopy()
80 2
        );
81 2
    }
82
83
    /**
84
     * convertTo.
85
     *
86
     * @param string $str
87
     * @param string $variant
88
     * @return string
89
     */
90 2
    public function convertTo($str, $variant = 'zh-TW')
91
    {
92 2
        $variant = strtolower($variant);
93 2
        $arrayObject = isset($this->table[$variant]) === true ? $this->table[$variant] : $this->table[$this->default];
94 2
        $data = $arrayObject->getArrayCopy();
95
96 2
        return strtr($str, $data);
97
    }
98
99
    /**
100
     * instance.
101
     *
102
     * @return static
103
     */
104 2
    public static function instance()
105
    {
106 2
        return static::$instance ?: new static();
107
    }
108
}
109