Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array keys and new method #7

Merged
merged 3 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/vendor
composer.lock

# PhpStorm IDE
/.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ echo $iso->nativeByCode3('eng'); // English
echo $iso->nativeByCode3('ind'); // Bahasa Indonesia
echo $iso->nativeByCode3('jav'); // basa Jawa

// Get language array from ISO-639-2b code
echo $iso->getLanguageByIsoCode2b('eng'); // ['en', 'eng', 'eng', 'eng', 'English', 'English']
echo $iso->getLanguageByIsoCode2b('ind'); // ['id', 'ind', 'ind', 'ind', 'Indonesian', 'Bahasa Indonesia']
echo $iso->getLanguageByIsoCode2b('jav'); // ['jv', 'jav', 'jav', 'jav', 'Javanese', 'basa Jawa']
```

## To Do
Expand Down
147 changes: 70 additions & 77 deletions src/ISO639.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ class ISO639
array('zu', 'zul', 'zul', 'zul', 'Zulu', 'isiZulu'),
);

public $indexIso639_1 = 0;
public $indexIso639_2t = 1;
public $indexIso639_2b = 2;
public $indexIso639_3 = 3;
public $indexEnglishName = 4;
public $indexNativeName = 5;

/*
* Get all language data
*
Expand All @@ -213,18 +220,15 @@ public function allLanguages()
*/
public function languageByCode1($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[0] == $code) {
$result = $lang[4];
break;
if ($lang[$this->indexIso639_1] === $code) {
return $lang[$this->indexEnglishName];
}
}

return $result;
return '';
}

/*
Expand All @@ -234,18 +238,15 @@ public function languageByCode1($code)
*/
public function nativeByCode1($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[0] == $code) {
$result = $lang[5];
break;
if ($lang[$this->indexIso639_1] === $code) {
return $lang[$this->indexNativeName];
}
}

return $result;
return '';
}

/*
Expand All @@ -255,18 +256,15 @@ public function nativeByCode1($code)
*/
public function languageByCode2t($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[1] == $code) {
$result = $lang[4];
break;
if ($lang[$this->indexIso639_2t] === $code) {
return $lang[$this->indexEnglishName];
}
}

return $result;
return '';
}

/*
Expand All @@ -276,18 +274,15 @@ public function languageByCode2t($code)
*/
public function nativeByCode2t($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[1] == $code) {
$result = $lang[5];
break;
if ($lang[$this->indexIso639_2t] === $code) {
return $lang[$this->indexNativeName];
}
}

return $result;
return '';
}

/*
Expand All @@ -297,18 +292,15 @@ public function nativeByCode2t($code)
*/
public function languageByCode2b($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[2] == $code) {
$result = $lang[4];
break;
if ($lang[$this->indexIso639_2b] === $code) {
return $lang[$this->indexEnglishName];
}
}

return $result;
return '';
}

/*
Expand All @@ -318,18 +310,15 @@ public function languageByCode2b($code)
*/
public function nativeByCode2b($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[2] == $code) {
$result = $lang[5];
break;
if ($lang[$this->indexIso639_2b] === $code) {
return $lang[$this->indexNativeName];
}
}

return $result;
return '';
}

/*
Expand All @@ -339,18 +328,15 @@ public function nativeByCode2b($code)
*/
public function languageByCode3($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[3] == $code) {
$result = $lang[4];
break;
if ($lang[$this->indexIso639_3] === $code) {
return $lang[$this->indexEnglishName];
}
}

return $result;
return '';
}

/*
Expand All @@ -360,18 +346,15 @@ public function languageByCode3($code)
*/
public function nativeByCode3($code)
{
$code = strtolower($code);

$result = '';
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if ($lang[3] == $code) {
$result = $lang[5];
break;
if ($lang[$this->indexIso639_3] === $code) {
return $lang[$this->indexNativeName];
}
}

return $result;
return '';
}

/*
Expand All @@ -383,16 +366,13 @@ public function code1ByLanguage($language)
{
$language_key = ucwords(strtolower($language));

$result = '';

foreach ($this->languages as $lang) {
if (in_array($language_key, explode(', ', $lang[4]))) {
$result = $lang[0];
break;
if (in_array($language_key, explode(', ', $lang[$this->indexEnglishName]))) {
return $lang[$this->indexIso639_1];
}
}

return $result;
return '';
}

/*
Expand All @@ -404,16 +384,14 @@ public function code2tByLanguage($language)
{
$language_key = ucwords(strtolower($language));

$result = '';

foreach ($this->languages as $lang) {
if (in_array($language_key, explode(', ', $lang[4]))) {
$result = $lang[1];
break;
if (in_array($language_key, explode(', ', $lang[$this->indexEnglishName]))) {
return $lang[$this->indexIso639_2t];
}
}

return $result;
return '';
}

/*
Expand All @@ -425,16 +403,14 @@ public function code2bByLanguage($language)
{
$language_key = ucwords(strtolower($language));

$result = '';

foreach ($this->languages as $lang) {
if (in_array($language_key, explode(', ', $lang[4]))) {
$result = $lang[2];
if (in_array($language_key, explode(', ', $lang[$this->indexEnglishName]))) {
return $lang[$this->indexIso639_2b];
break;
}
}

return $result;
return '';
}

/*
Expand All @@ -446,15 +422,32 @@ public function code3ByLanguage($language)
{
$language_key = ucwords(strtolower($language));

$result = '';
foreach ($this->languages as $lang) {
if (in_array($language_key, explode(', ', $lang[$this->indexEnglishName]))) {
return $lang[$this->indexIso639_3];
}
}

return '';
}

/**
* Gat language array from ISO-639-2/b (three-letter code)
*
* @param $code
* @return array|null
*/
public function getLanguageByIsoCode2b($code)
{
$code = strtolower(trim($code));

foreach ($this->languages as $lang) {
if (in_array($language_key, explode(', ', $lang[4]))) {
$result = $lang[3];
break;
if ($lang[$this->indexIso639_2b] === $code) {
return $lang;
}
}

return $result;
return null;
}

}
13 changes: 13 additions & 0 deletions tests/ISO639Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,17 @@ public function testISO6393Language()
$this->assertSame('msa', $this->iso->code3ByLanguage('Malay'));
$this->assertSame('sun', $this->iso->code3ByLanguage('Sundanese'));
}

public function testGetLanguageByIsoCode2B()
{
$result = ['en', 'eng', 'eng', 'eng', 'English', 'English'];
$this->assertSame($result, $this->iso->getLanguageByIsoCode2b('eng'));
$result = ['fr', 'fra', 'fre', 'fra', 'French', 'français, langue française'];
$this->assertSame($result, $this->iso->getLanguageByIsoCode2b('fre'));
$result = ['id', 'ind', 'ind', 'ind', 'Indonesian', 'Bahasa Indonesia'];
$this->assertSame($result, $this->iso->getLanguageByIsoCode2b('ind'));

$this->assertNull($this->iso->getLanguageByIsoCode2b('null'));
}

}