Skip to content

Commit fe454db

Browse files
authored
Merge pull request #1428 from troosan/parse_short_lang_code
Allow passing short lang code
2 parents 27946ca + 8fa1d4f commit fe454db

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ php:
1313

1414
matrix:
1515
include:
16-
- php: 5.6
16+
- php: 7.0
1717
env: COVERAGE=1
1818

1919
cache:

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Change Log
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6-
v0.15.0 (?? ??? 2018)
6+
v0.15.0 (14 Jul 2018)
77
----------------------
88
### Added
99
- Parsing of `align` HTML attribute - @troosan #1231
@@ -25,6 +25,7 @@ v0.15.0 (?? ??? 2018)
2525
- Several improvements to charts @JAEK-S #1332
2626
- Add parsing of html image in base64 format @jgpATs2w #1382
2727
- Added Support for Indentation & Tabs on RTF Writer. @smaug1985 #1405
28+
- Allows decimal numbers in HTML line-height style @jgpATs2w #1413
2829

2930
### Fixed
3031
- Fix reading of docx default style - @troosan #1238

src/PhpWord/Style/Language.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public function __construct($latin = null, $eastAsia = null, $bidirectional = nu
123123
*/
124124
public function setLatin($latin)
125125
{
126-
$this->validateLocale($latin);
127-
$this->latin = $latin;
126+
$this->latin = $this->validateLocale($latin);
128127

129128
return $this;
130129
}
@@ -173,8 +172,7 @@ public function getLangId()
173172
*/
174173
public function setEastAsia($eastAsia)
175174
{
176-
$this->validateLocale($eastAsia);
177-
$this->eastAsia = $eastAsia;
175+
$this->eastAsia = $this->validateLocale($eastAsia);
178176

179177
return $this;
180178
}
@@ -198,8 +196,7 @@ public function getEastAsia()
198196
*/
199197
public function setBidirectional($bidirectional)
200198
{
201-
$this->validateLocale($bidirectional);
202-
$this->bidirectional = $bidirectional;
199+
$this->bidirectional = $this->validateLocale($bidirectional);
203200

204201
return $this;
205202
}
@@ -218,12 +215,18 @@ public function getBidirectional()
218215
* Validates that the language passed is in the format xx-xx
219216
*
220217
* @param string $locale
221-
* @return bool
218+
* @return string
222219
*/
223220
private function validateLocale($locale)
224221
{
222+
if (strlen($locale) === 2) {
223+
return strtolower($locale) . '-' . strtoupper($locale);
224+
}
225+
225226
if ($locale !== null && strstr($locale, '-') === false) {
226227
throw new \InvalidArgumentException($locale . ' is not a valid language code');
227228
}
229+
230+
return $locale;
228231
}
229232
}

tests/PhpWord/Style/LanguageTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PHPUnit\Framework\Assert;
21+
2022
/**
2123
* Test class for PhpOffice\PhpWord\Style\Language
2224
*
@@ -56,7 +58,20 @@ public function testGetSetProperties()
5658
*/
5759
public function testWrongLanguage()
5860
{
61+
$language = new Language();
62+
$language->setLatin('fra');
63+
}
64+
65+
/**
66+
* Tests that a language can be set with just a 2 char code
67+
*/
68+
public function testShortLanguage()
69+
{
70+
//when
5971
$language = new Language();
6072
$language->setLatin('fr');
73+
74+
//then
75+
Assert::assertEquals('fr-FR', $language->getLatin());
6176
}
6277
}

0 commit comments

Comments
 (0)