Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Language
96.30% covered (success)
96.30%
26 / 27
90.00% covered (success)
90.00%
9 / 10
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 setLatin
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLatin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLangId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLangId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEastAsia
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getEastAsia
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBidirectional
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getBidirectional
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validateLocale
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
8.09
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Style;
20
21use InvalidArgumentException;
22
23/**
24 * Language
25 * A couple of predefined values are defined here, see the websites below for more values.
26 *
27 * @see http://www.datypic.com/sc/ooxml/t-w_CT_Language.html
28 * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
29 */
30final class Language extends AbstractStyle
31{
32    const EN_US = 'en-US';
33    const EN_US_ID = 1033;
34
35    const EN_GB = 'en-GB';
36    const EN_GB_ID = 2057;
37
38    const FR_FR = 'fr-FR';
39    const FR_FR_ID = 1036;
40
41    const FR_BE = 'fr-BE';
42    const FR_BE_ID = 2060;
43
44    const FR_CH = 'fr-CH';
45    const FR_CH_ID = 4108;
46
47    const ES_ES = 'es-ES';
48    const ES_ES_ID = 3082;
49
50    const DE_DE = 'de-DE';
51    const DE_DE_ID = 1031;
52
53    const DE_CH = 'de-CH';
54    const DE_CH_ID = 2055;
55
56    const HE_IL = 'he-IL';
57    const HE_IL_ID = 1037;
58
59    const IT_IT = 'it-IT';
60    const IT_IT_ID = 1040;
61
62    const IT_CH = 'it-CH';
63    const IT_CH_ID = 2064;
64
65    const JA_JP = 'ja-JP';
66    const JA_JP_ID = 1041;
67
68    const KO_KR = 'ko-KR';
69    const KO_KR_ID = 1042;
70
71    const ZH_CN = 'zh-CN';
72    const ZH_CN_ID = 2052;
73
74    const HI_IN = 'hi-IN';
75    const HI_IN_ID = 1081;
76
77    const PT_BR = 'pt-BR';
78    const PT_BR_ID = 1046;
79
80    const NL_NL = 'nl-NL';
81    const NL_NL_ID = 1043;
82
83    const SV_SE = 'sv-SE';
84    const SV_SE_ID = 1053;
85
86    const UK_UA = 'uk-UA';
87    const UK_UA_ID = 1058;
88
89    const RU_RU = 'ru-RU';
90    const RU_RU_ID = 1049;
91
92    /**
93     * Language ID, used for RTF document generation.
94     *
95     * @var int
96     *
97     * @see https://technet.microsoft.com/en-us/library/cc179219.aspx
98     */
99    private $langId;
100
101    /**
102     * Latin Language.
103     *
104     * @var null|string
105     */
106    private $latin;
107
108    /**
109     * East Asian Language.
110     *
111     * @var null|string
112     */
113    private $eastAsia;
114
115    /**
116     * Complex Script Language.
117     *
118     * @var null|string
119     */
120    private $bidirectional;
121
122    /**
123     * Constructor.
124     */
125    public function __construct(?string $latin = null, ?string $eastAsia = null, ?string $bidirectional = null)
126    {
127        if (!empty($latin)) {
128            $this->setLatin($latin);
129        }
130        if (!empty($eastAsia)) {
131            $this->setEastAsia($eastAsia);
132        }
133        if (!empty($bidirectional)) {
134            $this->setBidirectional($bidirectional);
135        }
136    }
137
138    /**
139     * Set the Latin Language.
140     *
141     * @param string $latin
142     *            The value for the latin language
143     */
144    public function setLatin(?string $latin): self
145    {
146        $this->latin = $this->validateLocale($latin);
147
148        return $this;
149    }
150
151    /**
152     * Get the Latin Language.
153     */
154    public function getLatin(): ?string
155    {
156        return $this->latin;
157    }
158
159    /**
160     * Set the Language ID.
161     *
162     * @param int $langId
163     *            The value for the language ID
164     *
165     * @return self
166     *
167     * @see https://technet.microsoft.com/en-us/library/cc287874(v=office.12).aspx
168     */
169    public function setLangId($langId)
170    {
171        $this->langId = $langId;
172
173        return $this;
174    }
175
176    /**
177     * Get the Language ID.
178     *
179     * @return int
180     */
181    public function getLangId()
182    {
183        return $this->langId;
184    }
185
186    /**
187     * Set the East Asian Language.
188     *
189     * @param string $eastAsia
190     *            The value for the east asian language
191     *
192     * @return self
193     */
194    public function setEastAsia($eastAsia)
195    {
196        $this->eastAsia = $this->validateLocale($eastAsia);
197
198        return $this;
199    }
200
201    /**
202     * Get the East Asian Language.
203     *
204     * @return null|string
205     */
206    public function getEastAsia()
207    {
208        return $this->eastAsia;
209    }
210
211    /**
212     * Set the Complex Script Language.
213     *
214     * @param string $bidirectional
215     *            The value for the complex script language
216     *
217     * @return self
218     */
219    public function setBidirectional($bidirectional)
220    {
221        $this->bidirectional = $this->validateLocale($bidirectional);
222
223        return $this;
224    }
225
226    /**
227     * Get the Complex Script Language.
228     *
229     * @return null|string
230     */
231    public function getBidirectional()
232    {
233        return $this->bidirectional;
234    }
235
236    /**
237     * Validates that the language passed is in the format xx-xx.
238     *
239     * @param string $locale
240     *
241     * @return string
242     */
243    private function validateLocale($locale)
244    {
245        if ($locale !== null) {
246            $locale = str_replace('_', '-', $locale);
247        }
248
249        if ($locale !== null && strlen($locale) === 2) {
250            return strtolower($locale) . '-' . strtoupper($locale);
251        }
252        if ($locale === 'und') {
253            return 'en-EN';
254        }
255        if ($locale !== null && $locale !== 'zxx' && strstr($locale, '-') === false) {
256            throw new InvalidArgumentException($locale . ' is not a valid language code');
257        }
258
259        return $locale;
260    }
261}