Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Php85/Php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @author Pierre Ambroise <[email protected]>
* @author Alexander Schranz <[email protected]>
*
* @internal
*/
Expand Down Expand Up @@ -47,4 +48,54 @@ public static function array_last(array $array)
{
return $array ? current(array_slice($array, -1)) : null;
}

public static function locale_is_right_to_left(string $locale): bool {
static $rtlScripts = [
'Adlm', 'Arab', 'Armi', 'Hebr', 'Mani', 'Mend', 'Nkoo',
'Phnx', 'Rohg', 'Samr', 'Syrc', 'Thaa',
];

static $languageToLikelyRtlScript = [
'ar' => 'Arab', // Arabic
'fa' => 'Arab', // Persian (Farsi)
'ur' => 'Arab', // Urdu
'ps' => 'Arab', // Pashto
'sd' => 'Arab', // Sindhi
'ug' => 'Arab', // Uyghur
'ckb' => 'Arab', // Sorani Kurdish
'he' => 'Hebr', // Hebrew
'yi' => 'Hebr', // Yiddish
'dv' => 'Thaa', // Dhivehi
'nqo' => 'Nkoo', // N'Ko
];

if (empty($locale)) {
return false;
}

$localeParts = preg_split('/[_-]/', $locale);
$language = strtolower($localeParts[0] ?? '');
$script = null;

foreach ($localeParts as $part) {
if (strlen($part) === 4 && ctype_alpha($part)) {
$script = ucfirst(strtolower($part));
break;
}
}

if ($script === null) {
$script = $languageToLikelyScript[$language] ?? null;
}

if ($script === null) {
if (in_array($language, ['ar', 'he', 'fa', 'ur', 'ps', 'sd', 'ug', 'ckb', 'yi', 'dv'])) {
return true;
}

return false;
}

return in_array($script, $rtlScripts, true);
}
}
4 changes: 4 additions & 0 deletions src/Php85/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ function array_first(array $array) { return p\Php85::array_first($array); }
if (!function_exists('array_last')) {
function array_last(array $array) { return p\Php85::array_last($array); }
}

if (!function_exists('locale_is_right_to_left')) {
function locale_is_right_to_left(string $locale): bool { return p\Php85::locale_is_right_to_left($locale); }
}
19 changes: 19 additions & 0 deletions tests/Php85/Php85Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ public function testArrayFirstArrayLast()
$this->assertTrue(array_first([true, new \stdClass]));
$this->assertEquals(new \stdClass(), array_last([true, new \stdClass]));
}

public function testLocaleIsRightToLeft(): void
{
$this->assertTrue(locale_is_right_to_left('ar'));
$this->assertTrue(locale_is_right_to_left('he'));
$this->assertTrue(locale_is_right_to_left('fa'));
$this->assertTrue(locale_is_right_to_left('ur'));
$this->assertTrue(locale_is_right_to_left('ps'));
$this->assertTrue(locale_is_right_to_left('sd'));
$this->assertTrue(locale_is_right_to_left('ug'));
$this->assertTrue(locale_is_right_to_left('ckb'));
$this->assertTrue(locale_is_right_to_left('yi'));
$this->assertTrue(locale_is_right_to_left('dv'));
$this->assertTrue(locale_is_right_to_left('ku_arab'));
$this->assertTrue(locale_is_right_to_left('ku-arab'));

$this->assertFalse(locale_is_right_to_left('en'));
$this->assertFalse(locale_is_right_to_left('fr'));
}
}

class TestHandler
Expand Down
Loading