Language   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTitle() 0 11 3
1
<?php
2
3
namespace Asymptix\localization;
4
5
/**
6
 * Stores Language code and titles in different languages.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2010 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class Language {
16
    /**
17
     * ISO 639-1 standard two-letter language code.
18
     *
19
     * @var string
20
     */
21
    public $code;
22
23
    /**
24
     * Two-letter flag code of the language.
25
     *
26
     * @var string
27
     */
28
    public $flag;
29
30
    /**
31
     * List of language titles on different languages.
32
     *
33
     * @var array<string, string>
34
     */
35
    public $titles = [];
36
37
    /**
38
     * Inits Language object.
39
     *
40
     * @param string $code Code of the language.
41
     * @param array $titles List of languages titles on this language.
42
     * @param string $flag Flag file name.
43
     */
44
    public function __construct($code, $titles, $flag = "") {
45
        $this->code = $code;
46
        $this->titles = $titles;
47
        $this->flag = $flag;
48
    }
49
50
    /**
51
     * Returns title of the language by it's ISO code.
52
     * If code is not set then returns native title of the language.
53
     *
54
     * @param string $code 2-letters language ISO code (optional).
55
     *
56
     * @return string Title of the language.
57
     * @throws Exception If language code is incorrect.
58
     */
59
    public function getTitle($code = null) {
60
        if (empty($code)) {
61
            $code = $this->code;
62
        }
63
64
        if (isset($this->titles[$code])) {
65
            return $this->titles[$code];
66
        } else {
67
            throw new Exception("Invalid language code '" . $code . "'");
68
        }
69
    }
70
71
}
72