Completed
Push — 1.0 ( 19d976...2156a0 )
by Valentin
05:35
created

LanguageObject::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Data;
11
12
use Transfer\Data\ValueObject;
13
use Transfer\EzPlatform\Exception\LanguageNotFoundException;
14
15
/**
16
 * Content type object.
17
 */
18
class LanguageObject extends ValueObject
19
{
20
    /**
21
     * Because a name is required.
22
     *
23
     * @var array
24
     */
25
    protected $defaultNameMapping = array(
26
        'ara-SA' => 'Arabic',
27
        'cat-ES' => 'Catalan',
28
        'chi-CN' => 'Simplified Chinese',
29
        'chi-HK' => 'Traditional Chinese (HongKong)',
30
        'chi-TW' => 'Traditional Chinese (Taiwan)',
31
        'cro-HR' => 'Croatian (Hrvatski)',
32
        'cze-CZ' => 'Czech',
33
        'dan-DK' => 'Danish',
34
        'dut-NL' => 'Dutch',
35
        'ell-GR' => 'Greek (Hellenic)',
36
        'eng-AU' => 'English (Australia)',
37
        'eng-CA' => 'English (Canada)',
38
        'eng-GB' => 'English (United Kingdom)',
39
        'eng-NZ' => 'English (New Zealand)',
40
        'eng-US' => 'English (American)',
41
        'epo-EO' => 'Esperanto',
42
        'esl-ES' => 'Spanish (Spain)',
43
        'esl-MX' => 'Spanish (Mexico)',
44
        'fin-FI' => 'Finnish',
45
        'fre-BE' => 'French (Belgium)',
46
        'fre-CA' => 'French (Canada)',
47
        'fre-FR' => 'French (France)',
48
        'ger-DE' => 'German',
49
        'heb-IL' => 'Hebrew',
50
        'hin-IN' => 'Hindi (India)',
51
        'hun-HU' => 'Hungarian',
52
        'ind-ID' => 'Indonesian',
53
        'ita-IT' => 'Italian',
54
        'jpn-JP' => 'Japanese',
55
        'kor-KR' => 'Korean',
56
        'nno-NO' => 'Norwegian (Nynorsk)',
57
        'nor-NO' => 'Norwegian (Bokmal)',
58
        'pol-PL' => 'Polish',
59
        'por-BR' => 'Portuguese (Brazil)',
60
        'por-MZ' => 'Portuguese (Mozambique)',
61
        'por-PT' => 'Portuguese (Portugal)',
62
        'rus-RU' => 'Russian',
63
        'ser-SR' => 'Serbian (Srpski)',
64
        'slk-SK' => 'Slovak',
65
        'srp-RS' => 'Serbian (Српски)',
66
        'swe-SE' => 'Swedish',
67
        'tur-TR' => 'Turkish',
68
        'ukr-UA' => 'Ukrainian',
69
    );
70
71
    /**
72
     * LanguageObject constructor.
73
     *
74
     * @param array $data
75
     *
76
     * @throws LanguageNotFoundException
77
     */
78 18
    public function __construct($data)
79
    {
80 18
        parent::__construct($data);
81 18
        if (!isset($this->data['name'])) {
82 18
            $this->data['name'] = $this->getDefaultName($this->data['code']);
83 18
        }
84 18
    }
85
86
    /**
87
     * @param string $code
88
     *
89
     * @return string
90
     *
91
     * @throws LanguageNotFoundException
92
     */
93 18
    public function getDefaultName($code)
94
    {
95 18
        if (!array_key_exists($code, $this->defaultNameMapping)) {
96 1
            throw new LanguageNotFoundException(sprintf('Default language name for code "%s" not found.', $code));
97
        }
98
99 18
        return $this->defaultNameMapping[$code];
100
    }
101
}
102