Skip to content

Commit e1562b1

Browse files
committed
feat: main upload of v2.0.0 version
Closes #1
1 parent b1e464f commit e1562b1

File tree

3 files changed

+78
-145
lines changed

3 files changed

+78
-145
lines changed

src/LanguageCode.php

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
2-
/**
3-
* List of 217 language codes: ISO 639-1.
4-
*
5-
* @author Josantonius <hello@josantonius.com>
6-
* @copyright 2017 - 2018 (c) Josantonius - PHP-LanguageCode
7-
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8-
* @link https://github.com/Josantonius/PHP-LanguageCode
9-
* @since 1.0.0
10-
*/
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-language-code repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
1112
namespace Josantonius\LanguageCode;
1213

1314
/**
@@ -17,35 +18,25 @@ class LanguageCode
1718
{
1819
/**
1920
* Get all language codes as array.
20-
*
21-
* @return array → language codes and language names
2221
*/
23-
public static function get()
22+
public static function all(): array
2423
{
2524
return LanguageCodeCollection::all();
2625
}
2726

2827
/**
29-
* Get language name from language code.
30-
*
31-
* @param string $languageCode → language code, e.g. 'es'
32-
*
33-
* @return tring|false → country name
28+
* Get language code from language name.
3429
*/
35-
public static function getLanguageFromCode($languageCode)
30+
public static function getCode(string $languageName): ?string
3631
{
37-
return LanguageCodeCollection::get($languageCode) ?: false;
32+
return LanguageCodeCollection::getCode($languageName);
3833
}
3934

4035
/**
41-
* Get language code from language name.
42-
*
43-
* @param string $languageName → language name, e.g. 'Spanish'
44-
*
45-
* @return tring|false → language code
36+
* Get language name from language code.
4637
*/
47-
public static function getCodeFromLanguage($languageName)
38+
public static function getName(string $languageCode): ?string
4839
{
49-
return array_search($languageName, LanguageCodeCollection::all(), true);
40+
return LanguageCodeCollection::getName($languageCode);
5041
}
5142
}

src/LanguageCodeCollection.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
<?php
2-
/**
3-
* List of 217 language codes: ISO 639-1.
4-
*
5-
* @author Josantonius <hello@josantonius.com>
6-
* @copyright 2017 - 2018 (c) Josantonius - PHP-LanguageCode
7-
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8-
* @link https://github.com/Josantonius/PHP-LanguageCode
9-
* @since 1.1.4
10-
*/
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-language-code repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
1112
namespace Josantonius\LanguageCode;
1213

1314
/**
14-
* Language code handler.
15+
* Language code collection.
1516
*
16-
* Compiled from https://wikipedia.org/wiki/List_of_ISO_639-1_codes
17+
* Compiled from https://wikipedia.org/wiki/List_of_ISO_639-1_codes.
1718
*/
1819
class LanguageCodeCollection
1920
{
20-
/**
21-
* List of language codes.
22-
*
23-
* @var array
24-
*/
25-
protected static $data = [
21+
protected static array $data = [
2622
'aa' => 'Afar',
2723
'ab' => 'Abkhazian',
2824
'af' => 'Afrikaans',
@@ -36,7 +32,7 @@ class LanguageCodeCollection
3632
'ar-jo' => 'Arabic (Jordan)',
3733
'ar-kw' => 'Arabic (Kuwait)',
3834
'ar-lb' => 'Arabic (Lebanon)',
39-
'ar-ly' => 'Arabic (libya)',
35+
'ar-ly' => 'Arabic (Libya)',
4036
'ar-ma' => 'Arabic (Morocco)',
4137
'ar-om' => 'Arabic (Oman)',
4238
'ar-qa' => 'Arabic (Qatar)',
@@ -46,7 +42,7 @@ class LanguageCodeCollection
4642
'ar-ye' => 'Arabic (Yemen)',
4743
'as' => 'Assamese',
4844
'ay' => 'Aymara',
49-
'az' => 'Azeri',
45+
'az' => 'Azerí',
5046
'ba' => 'Bashkir',
5147
'be' => 'Belarusian',
5248
'bg' => 'Bulgarian',
@@ -243,28 +239,26 @@ class LanguageCodeCollection
243239
];
244240

245241
/**
246-
* Returns all language codes.
247-
*
248-
* @return array
242+
* Get all language codes as array.
249243
*/
250-
public static function all()
244+
public static function all(): array
251245
{
252-
return static::$data;
246+
return self::$data;
253247
}
254248

255249
/**
256-
* Return language name from language code.
257-
*
258-
* @param string $key
259-
*
260-
* @return string|null → language code or null
250+
* Get language code from language name.
261251
*/
262-
public static function get($key)
252+
public static function getCode(string $languageName): ?string
263253
{
264-
if (isset(static::$data[$key])) {
265-
return static::$data[$key];
266-
}
254+
return array_search($languageName, self::$data) ?: null;
255+
}
267256

268-
return null;
257+
/**
258+
* Get language name from language code.
259+
*/
260+
public static function getName(string $languageCode): ?string
261+
{
262+
return self::$data[$languageCode] ?? null;
269263
}
270264
}

tests/LanguageCodeTest.php

Lines changed: 33 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,64 @@
11
<?php
2-
/**
3-
* List of 217 language codes: ISO 639-1.
4-
*
5-
* @author Josantonius <hello@josantonius.com>
6-
* @copyright 2017 - 2018 (c) Josantonius - PHP-LanguageCode
7-
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8-
* @link https://github.com/Josantonius/PHP-LanguageCode
9-
* @since 1.1.3
10-
*/
11-
namespace Josantonius\LanguageCode;
2+
3+
/*
4+
* This file is part of https://github.com/josantonius/php-language-code repository.
5+
*
6+
* (c) Josantonius <hello@josantonius.dev>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Josantonius\LanguageCode\Tests;
1213

1314
use PHPUnit\Framework\TestCase;
15+
use Josantonius\LanguageCode\LanguageCode;
16+
use Josantonius\LanguageCode\LanguageCodeCollection;
1417

15-
/**
16-
* Tests class for LanguageCode library.
17-
*/
1818
class LanguageCodeTest extends TestCase
1919
{
20-
/**
21-
* LanguageCode instance.
22-
*
23-
* @since 1.1.5
24-
*
25-
* @var object
26-
*/
27-
protected $LanguageCode;
28-
29-
/**
30-
* Set up.
31-
*
32-
* @since 1.1.5
33-
*/
34-
public function setUp()
20+
protected $languageCode;
21+
22+
public function setUp(): void
3523
{
3624
parent::setUp();
3725

38-
$this->LanguageCode = new LanguageCode;
39-
}
40-
41-
/**
42-
* Check if it is an instance of LanguageCode.
43-
*
44-
* @since 1.1.5
45-
*/
46-
public function testIsInstanceOfLanguageCode()
47-
{
48-
$this->assertInstanceOf(
49-
'Josantonius\LanguageCode\LanguageCode',
50-
$this->LanguageCode
51-
);
26+
$this->collection = new LanguageCodeCollection();
27+
$this->languageCode = new LanguageCode();
5228
}
5329

54-
/**
55-
* Get language name from language code.
56-
*/
57-
public function testGetLanguageFromCode()
30+
public function testShouldGetAllLanguageCodes(): void
5831
{
59-
$languageCode = $this->LanguageCode;
32+
$this->assertNotEmpty($this->languageCode->all());
6033

61-
$this->assertContains(
62-
'Spanish',
63-
$languageCode::getLanguageFromCode('es')
64-
);
34+
$this->assertNotEmpty($this->collection->all());
6535
}
6636

67-
/**
68-
* Getting a language name wrong.
69-
*/
70-
public function testGetLanguageFromCodeUndefined()
37+
public function testShouldGetLanguageCodeFromLanguageName(): void
7138
{
72-
$languageCode = $this->LanguageCode;
39+
$this->assertSame('es', $this->languageCode->getCode('Spanish'));
7340

74-
$this->assertFalse(
75-
$languageCode::getLanguageFromCode('abcd')
76-
);
41+
$this->assertSame('es', $this->collection->getCode('Spanish'));
7742
}
7843

79-
/**
80-
* Get language code from language name
81-
*/
82-
public function testGetCodeFromLanguage()
44+
public function testShouldReturnNullWithUnknownLanguageName(): void
8345
{
84-
$languageCode = $this->LanguageCode;
46+
$this->assertNull($this->languageCode->getCode('foo'));
8547

86-
$this->assertContains(
87-
'es',
88-
$languageCode::getCodeFromLanguage('Spanish')
89-
);
48+
$this->assertNull($this->collection->getCode('foo'));
9049
}
9150

92-
/**
93-
* Getting a language code wrong.
94-
*/
95-
public function testGetCodeFromLanguageUndefined()
51+
public function testShouldGetLanguageNameFromLanguageCode(): void
9652
{
97-
$languageCode = $this->LanguageCode;
53+
$this->assertSame('Spanish', $this->languageCode->getName('es'));
9854

99-
$this->assertFalse(
100-
$languageCode::getCodeFromLanguage('abcd')
101-
);
55+
$this->assertSame('Spanish', $this->collection->getName('es'));
10256
}
10357

104-
/**
105-
* Get all language codes as array.
106-
*/
107-
public function testGetAll()
58+
public function testShouldReturnNullWithUnknownLanguageCode(): void
10859
{
109-
$languageCode = $this->LanguageCode;
60+
$this->assertNull($this->languageCode->getName('bar'));
11061

111-
$this->assertInternalType(
112-
'array',
113-
$languageCode::get()
114-
);
62+
$this->assertNull($this->collection->getName('bar'));
11563
}
11664
}

0 commit comments

Comments
 (0)