Completed
Pull Request — 1.0 (#53)
by Harald
05:33
created

LanguageObject::getMapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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\Repository\Values;
11
12
use Transfer\EzPlatform\Exception\LanguageNotFoundException;
13
use Transfer\EzPlatform\Repository\Values\Mapper\LanguageMapper;
14
15
/*
16
17
** Available keys: **
18
19
    $data = [
20
        code => string
21
        name => string
22
    ],
23
    $properties = [
24
        action => int {@link see \Transfer\EzPlatform\Data\Action\Enum\Action}
25
    ]
26
27
28
** Required on `create`:
29
**** Required by transfer:
30
    code
31
32
**** Required by eZ:
33
    code
34
    name
35
36
** Required on `update`:
37
**** Required by transfer:
38
    code
39
    name
40
41
**** Required by eZ:
42
    code
43
    name
44
45
*/
46
47
/**
48
 * Content type object.
49
 */
50
class LanguageObject extends EzPlatformObject
51
{
52
    /**
53
     * @var LanguageMapper
54
     */
55
    private $mapper;
56
57
    /**
58
     * @return LanguageMapper
59
     */
60 14
    public function getMapper()
61
    {
62 14
        if (!$this->mapper) {
63 14
            $this->mapper = new LanguageMapper($this);
64 14
        }
65
66 14
        return $this->mapper;
67
    }
68
69
    /**
70
     * Because a name is required.
71
     *
72
     * @var array
73
     */
74
    protected $defaultNameMapping = array(
75
        'ara-SA' => 'Arabic',
76
        'cat-ES' => 'Catalan',
77
        'chi-CN' => 'Simplified Chinese',
78
        'chi-HK' => 'Traditional Chinese (HongKong)',
79
        'chi-TW' => 'Traditional Chinese (Taiwan)',
80
        'cro-HR' => 'Croatian (Hrvatski)',
81
        'cze-CZ' => 'Czech',
82
        'dan-DK' => 'Danish',
83
        'dut-NL' => 'Dutch',
84
        'ell-GR' => 'Greek (Hellenic)',
85
        'eng-AU' => 'English (Australia)',
86
        'eng-CA' => 'English (Canada)',
87
        'eng-GB' => 'English (United Kingdom)',
88
        'eng-NZ' => 'English (New Zealand)',
89
        'eng-US' => 'English (American)',
90
        'epo-EO' => 'Esperanto',
91
        'esl-ES' => 'Spanish (Spain)',
92
        'esl-MX' => 'Spanish (Mexico)',
93
        'fin-FI' => 'Finnish',
94
        'fre-BE' => 'French (Belgium)',
95
        'fre-CA' => 'French (Canada)',
96
        'fre-FR' => 'French (France)',
97
        'ger-DE' => 'German',
98
        'heb-IL' => 'Hebrew',
99
        'hin-IN' => 'Hindi (India)',
100
        'hun-HU' => 'Hungarian',
101
        'ind-ID' => 'Indonesian',
102
        'ita-IT' => 'Italian',
103
        'jpn-JP' => 'Japanese',
104
        'kor-KR' => 'Korean',
105
        'nno-NO' => 'Norwegian (Nynorsk)',
106
        'nor-NO' => 'Norwegian (Bokmal)',
107
        'pol-PL' => 'Polish',
108
        'por-BR' => 'Portuguese (Brazil)',
109
        'por-MZ' => 'Portuguese (Mozambique)',
110
        'por-PT' => 'Portuguese (Portugal)',
111
        'rus-RU' => 'Russian',
112
        'ser-SR' => 'Serbian (Srpski)',
113
        'slk-SK' => 'Slovak',
114
        'srp-RS' => 'Serbian (Српски)',
115
        'swe-SE' => 'Swedish',
116
        'tur-TR' => 'Turkish',
117
        'ukr-UA' => 'Ukrainian',
118
    );
119
120
    /**
121
     * LanguageObject constructor.
122
     *
123
     * @param array $data
124
     *
125
     * @throws LanguageNotFoundException
126
     */
127 14
    public function __construct($data, $properties = [])
128
    {
129 14
        parent::__construct($data, $properties);
130 14
        if (!isset($this->data['name'])) {
131 14
            $this->data['name'] = $this->getDefaultName($this->data['code']);
132 14
        }
133 14
    }
134
135
    /**
136
     * @param string $code
137
     *
138
     * @return string
139
     *
140
     * @throws LanguageNotFoundException
141
     */
142 14
    public function getDefaultName($code)
143
    {
144 14
        if (!array_key_exists($code, $this->defaultNameMapping)) {
145
            throw new LanguageNotFoundException(sprintf('Default language name for code "%s" not found.', $code));
146
        }
147
148 14
        return $this->defaultNameMapping[$code];
149
    }
150
}
151