Allow passing short lang code

This commit is contained in:
troosan 2018-07-14 17:21:30 +02:00
parent 6475812e82
commit 87498e43e1
3 changed files with 27 additions and 8 deletions

View File

@ -3,7 +3,7 @@ Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
v0.15.0 (?? ??? 2018)
v0.15.0 (14 Jul 2018)
----------------------
### Added
- Parsing of `align` HTML attribute - @troosan #1231
@ -25,6 +25,7 @@ v0.15.0 (?? ??? 2018)
- Several improvements to charts @JAEK-S #1332
- Add parsing of html image in base64 format @jgpATs2w #1382
- Added Support for Indentation & Tabs on RTF Writer. @smaug1985 #1405
- Allows decimal numbers in HTML line-height style @jgpATs2w #1413
### Fixed
- Fix reading of docx default style - @troosan #1238

View File

@ -123,8 +123,7 @@ final class Language extends AbstractStyle
*/
public function setLatin($latin)
{
$this->validateLocale($latin);
$this->latin = $latin;
$this->latin = $this->validateLocale($latin);
return $this;
}
@ -173,8 +172,7 @@ final class Language extends AbstractStyle
*/
public function setEastAsia($eastAsia)
{
$this->validateLocale($eastAsia);
$this->eastAsia = $eastAsia;
$this->eastAsia = $this->validateLocale($eastAsia);
return $this;
}
@ -198,8 +196,7 @@ final class Language extends AbstractStyle
*/
public function setBidirectional($bidirectional)
{
$this->validateLocale($bidirectional);
$this->bidirectional = $bidirectional;
$this->bidirectional = $this->validateLocale($bidirectional);
return $this;
}
@ -218,12 +215,18 @@ final class Language extends AbstractStyle
* Validates that the language passed is in the format xx-xx
*
* @param string $locale
* @return bool
* @return string
*/
private function validateLocale($locale)
{
if (strlen($locale) === 2) {
return strtolower($locale) . '-' . strtoupper($locale);
}
if ($locale !== null && strstr($locale, '-') === false) {
throw new \InvalidArgumentException($locale . ' is not a valid language code');
}
return $locale;
}
}

View File

@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Style;
use PHPUnit\Framework\Assert;
/**
* Test class for PhpOffice\PhpWord\Style\Language
*
@ -56,7 +58,20 @@ class LanguageTest extends \PHPUnit\Framework\TestCase
*/
public function testWrongLanguage()
{
$language = new Language();
$language->setLatin('fra');
}
/**
* Tests that a language can be set with just a 2 char code
*/
public function testShortLanguage()
{
//when
$language = new Language();
$language->setLatin('fr');
//then
Assert::assertEquals('fr-FR', $language->getLatin());
}
}