Merge pull request #1428 from troosan/parse_short_lang_code

Allow passing short lang code
This commit is contained in:
troosan 2018-07-14 18:11:50 +02:00 committed by GitHub
commit fe454dbaf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 9 deletions

View File

@ -13,7 +13,7 @@ php:
matrix: matrix:
include: include:
- php: 5.6 - php: 7.0
env: COVERAGE=1 env: COVERAGE=1
cache: cache:

View File

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

View File

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

View File

@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Style; namespace PhpOffice\PhpWord\Style;
use PHPUnit\Framework\Assert;
/** /**
* Test class for PhpOffice\PhpWord\Style\Language * Test class for PhpOffice\PhpWord\Style\Language
* *
@ -56,7 +58,20 @@ class LanguageTest extends \PHPUnit\Framework\TestCase
*/ */
public function testWrongLanguage() 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 = new Language();
$language->setLatin('fr'); $language->setLatin('fr');
//then
Assert::assertEquals('fr-FR', $language->getLatin());
} }
} }