Language::language()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of slick/i18n package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\I18n;
11
12
use Slick\I18n\Exception\UnknownLanguageCodeException;
13
14
/**
15
 * Language
16
 *
17
 * @package Slick\I18n
18
 */
19
final class Language
20
{
21
22
    private static $codes;
23
24
    /**
25
     * @var string
26
     */
27
    private $language;
28
29
    /**
30
     * @var string
31
     */
32
    private $languageCode;
33
34
    /**
35
     * @var string
36
     */
37
    private $regionName;
38
39
    /**
40
     * @var string
41
     */
42
    private $regionCode;
43
44
    /**
45
     * Language
46
     *
47
     * @param string $code
48
     */
49
    public function __construct($code)
50
    {
51
        list($langCode, $langName) = self::parseLanguage($code);
52
        list($regCode, $regName) = self::parseRegion($code);
53
        $this->language = $langName;
54
        $this->languageCode = $langCode;
55
        $this->regionCode = $regCode;
56
        $this->regionName = $this->clear($regName);
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function __toString()
63
    {
64
        return $this->identifier();
65
    }
66
67
    /**
68
     * Get language name
69
     *
70
     * @return string
71
     */
72
    public function language()
73
    {
74
        return $this->language;
75
    }
76
77
    /**
78
     * Get ISO 639-1 2 letter language code
79
     *
80
     * @return string
81
     */
82
    public function languageCode()
83
    {
84
        return $this->languageCode;
85
    }
86
87
    /**
88
     * Get region name
89
     *
90
     * @return mixed
91
     */
92
    public function regionName()
93
    {
94
        return $this->regionName;
95
    }
96
97
    /**
98
     * Get region 2 letter country code
99
     *
100
     * @return string
101
     */
102
    public function regionCode()
103
    {
104
        return $this->regionCode;
105
    }
106
107
    /**
108
     * Clears the name of passed region
109
     *
110
     * @param string $regName
111
     *
112
     * @return string
113
     */
114
    private function clear($regName)
115
    {
116
        $name = str_replace(
117
            [$this->language(), '(', ')'],
118
            '',
119
            $regName
120
        );
121
        return trim($name);
122
    }
123
124
    /**
125
     * Get language code
126
     *
127
     * @param string $code
128
     *
129
     * @return array
130
     */
131
    private static function parseLanguage($code)
132
    {
133
        $lang = substr($code, 0, 2);
134
        foreach (self::codes() as $code) {
135
            if ($code['code'] == strtolower($lang)) {
136
                return [strtolower($lang), $code['description']];
137
            }
138
        }
139
140
        throw new UnknownLanguageCodeException(
141
            "The language code '{$code}'' is unknown."
142
        );
143
    }
144
145
    /**
146
     * Get regina name and code
147
     *
148
     * @param string $code
149
     *
150
     * @return array
151
     */
152
    private static function parseRegion($code)
153
    {
154
        $lang = substr($code, 0, 2);
155
        $reg =  strtoupper(substr($code, 3, 2));
156
        $needle = "{$lang}-{$reg}";
157
        foreach (self::codes() as $code) {
158
            if ($code['code'] == $needle) {
159
                return [$reg, $code['description']];
160
            }
161
        }
162
163
        throw new UnknownLanguageCodeException(
164
            "The region code '{$code}'' is unknown."
165
        );
166
    }
167
168
    /**
169
     * Language database
170
     *
171
     * @return array
172
     */
173
    private static function codes()
174
    {
175
        if (!self::$codes) {
176
            self::$codes = include 'language-db.php';
177
        }
178
        return self::$codes;
179
    }
180
181
    /**
182
     * Get the language RFC 3066 identifier
183
     *
184
     * @return string
185
     */
186
    public function identifier()
187
    {
188
        return "{$this->languageCode()}_{$this->regionCode()}";
189
    }
190
}