Skip to content

Commit

Permalink
v1.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
drrefe committed Aug 28, 2023
1 parent b099f73 commit e18757c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 48 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Using Turkish Suffixes

Supports `-e`, `-i`, `-in`, `-de`, `-den` suffixes

`Tr::suffix(string $suffix, string $text, ?array $config): string`
`Tr::suffix(string $suffix, string $text, string $locale, bool $apostrophe, bool $uppercase): string`

Example:

Expand All @@ -35,15 +35,15 @@ echo Tr::suffix('e', 'Ahmet'); // Ahmet'e
echo Tr::suffix('e', 'Ayşe'); // Ayşe'ye
echo Tr::suffix('i', 'Arda'); // Arda'yı
echo Tr::suffix('i', 'Doğu'); // Doğu'yu
echo Tr::suffix('i', 'Kitap', ['apostrophe' => false]); // Kitabı
echo Tr::suffix('i', 'Kitap', apostrophe: false); // Kitabı
echo Tr::suffix('in', 'Ankara'); // Ankara'nın
echo Tr::suffix('in', 'Mehmet'); // Mehmet'in
echo Tr::suffix('in', 'Bölük', ['apostrophe' => false]); // Bölüğün
echo Tr::suffix('in', 'Mehmet', ['locale' => 'en']); // Mehmet's
echo Tr::suffix('in', 'Enes', ['locale' => 'en']); // Enes'
echo Tr::suffix('in', 'Bölük', apostrophe: false); // Bölüğün
echo Tr::suffix('in', 'Mehmet', locale: 'en'); // Mehmet's
echo Tr::suffix('in', 'Enes', locale: 'en'); // Enes'
echo Tr::suffix('de', 'İstanbul'); // İstanbul'da
echo Tr::suffix('de', 'Yozgat'); // Yozgat'ta
echo Tr::suffix('den', 'Kitaplık', ['apostrophe' => false]); // Kitaplıktan
echo Tr::suffix('den', 'KİTAPLIK', apostrophe: false, uppercase: true); // KİTAPLIKTAN

```

Expand Down
38 changes: 6 additions & 32 deletions src/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,11 @@ class Config
public bool $apostrophe = true;
public bool $uppercase = false;

public function __construct(array $config)
{
if (isset($config['locale'])) {
if (!is_null($config['locale'])) {
if (is_string($config['locale'])) {
$locale = Tr::lowerCase(mb_substr($config['locale'], 0, 2, 'UTF-8'));
if(in_array($locale, array('tr', 'en'))) {
$this->locale = $locale;
} else {
$this->locale = null;
}
} else {
throw new Exception("apostrophe must be type of string!");
}
}
}

if (isset($config['apostrophe'])) {
if (is_bool($config['apostrophe'])) {
$this->apostrophe = $config['apostrophe'];
} else {
throw new Exception("apostrophe must be type of boolean!");
}
}

if (isset($config['uppercase'])) {
if (is_bool($config['uppercase'])) {
$this->uppercase = $config['uppercase'];
} else {
throw new Exception("uppercase must be type of boolean!");
}
}
public function __construct( string $locale = 'tr', bool $apostrophe = true, bool $uppercase = false)
{
$locale = Tr::lowerCase(mb_substr($locale, 0, 2, 'UTF-8'));
$this->locale = in_array($locale, array('tr', 'en')) ? $locale : null;
$this->apostrophe = $apostrophe;
$this->uppercase = $uppercase;
}
}
4 changes: 2 additions & 2 deletions src/Core/SuffixHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class SuffixHandler
private string $original;
private Text $text;

public function __construct(string $suffix, array $config)
public function __construct(string $suffix, string $locale, $apostrophe, $uppercase)
{
if (!in_array($suffix, array('e', 'i', 'in', 'de', 'den'))) {
throw new Exception("Suffix is not supported!");
}
$this->suffix = $suffix;
$this->config = new Config($config);
$this->config = new Config($locale, $apostrophe, $uppercase);
}

public function handle(string $text): string
Expand Down
8 changes: 5 additions & 3 deletions src/Tr.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public static function title(string $text): string
*
* @param string $suffix 'e'|'i'|'in'|'de'|'den'
* @param string $text
* @param array $config ['locale' => string, 'apostrophe' => bool, 'uppercase' => bool]
* @param string $locale default:'tr'
* @param bool $apostrophe default: true
* @param bool $uppercase default: false
* @return string
*/
public static function suffix(string $suffix, string $text, array $config = array()): string
public static function suffix(string $suffix, string $text, string $locale = 'tr', bool $apostrophe = true, bool $uppercase = false): string
{
$suffixHandler = new SuffixHandler($suffix, $config);
$suffixHandler = new SuffixHandler($suffix, $locale, $apostrophe, $uppercase);
return $suffixHandler->handle($text);
}
}
10 changes: 5 additions & 5 deletions tests/TrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function testSuffix()
$rows = json_decode(file_get_contents(__DIR__ . "/test-rows.json"), true, JSON_UNESCAPED_UNICODE);

foreach ($rows as $row) {
$this->assertSame($row["expected_e"], Tr::suffix("e", $row["text"], ["locale" => $row["locale"], "apostrophe" => $row["apostrophe"], "uppercase" => $row["uppercase"]]));
$this->assertSame($row["expected_i"], Tr::suffix("i", $row["text"], ["locale" => $row["locale"], "apostrophe" => $row["apostrophe"], "uppercase" => $row["uppercase"]]));
$this->assertSame($row["expected_in"], Tr::suffix("in", $row["text"], ["locale" => $row["locale"], "apostrophe" => $row["apostrophe"], "uppercase" => $row["uppercase"]]));
$this->assertSame($row["expected_de"], Tr::suffix("de", $row["text"], ["locale" => $row["locale"], "apostrophe" => $row["apostrophe"], "uppercase" => $row["uppercase"]]));
$this->assertSame($row["expected_den"], Tr::suffix("den", $row["text"], ["locale" => $row["locale"], "apostrophe" => $row["apostrophe"], "uppercase" => $row["uppercase"]]));
$this->assertSame($row["expected_e"], Tr::suffix("e", $row["text"], locale: $row["locale"], apostrophe: $row["apostrophe"], uppercase: $row["uppercase"]));
$this->assertSame($row["expected_i"], Tr::suffix("i", $row["text"], locale: $row["locale"], apostrophe: $row["apostrophe"], uppercase: $row["uppercase"]));
$this->assertSame($row["expected_in"], Tr::suffix("in", $row["text"], locale: $row["locale"], apostrophe: $row["apostrophe"], uppercase: $row["uppercase"]));
$this->assertSame($row["expected_de"], Tr::suffix("de", $row["text"], locale: $row["locale"], apostrophe: $row["apostrophe"], uppercase: $row["uppercase"]));
$this->assertSame($row["expected_den"], Tr::suffix("den", $row["text"], locale: $row["locale"], apostrophe: $row["apostrophe"], uppercase: $row["uppercase"]));
}
}
}
11 changes: 11 additions & 0 deletions tests/test-rows.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@
"expected_de": "Kitapta",
"expected_den": "Kitaptan"
},
{
"text": "KİTAPLIK",
"locale": "tr",
"apostrophe": false,
"uppercase": true,
"expected_e": "KİTAPLIĞA",
"expected_i": "KİTAPLIĞI",
"expected_in": "KİTAPLIĞIN",
"expected_de": "KİTAPLIKTA",
"expected_den": "KİTAPLIKTAN"
},
{
"text": "Stok",
"locale": "tr",
Expand Down

0 comments on commit e18757c

Please sign in to comment.