Skip to content

Commit

Permalink
Add support for complex geographical names: Нижний Новгород
Browse files Browse the repository at this point in the history
  • Loading branch information
wapmorgan committed May 6, 2017
1 parent 6f4e22a commit 98401fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Russian/GeographicalNamesDeclension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,41 @@ static public function isMutable($name) {

static public function getCases($name) {
$name = S::lower($name);

// check for name of two words
if (strpos($name, ' ') !== false) {
$parts = explode(' ', $name);
$cases = array();
$result = array();
foreach ($parts as $i => $part) {
$result[$i] = static::getCases($part);
if ($i > 0)
$result[$i][self::PREDLOJ] = substr(strstr($result[$i][self::PREDLOJ], ' '), 1);
}

$cases = array();
foreach (array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT, self::PREDLOJ) as $case) {
foreach ($parts as $i => $part) {
$cases[$case][] = $result[$i][$case];
}
$cases[$case] = implode(' ', $cases[$case]);
}
return $cases;
}

if (!in_array($name, self::$abbreviations)) {
if (S::slice($name, -1) == 'а') {
if (S::slice($name, -2) == 'ий') {
// Нижний, Русский
$prefix = S::name(S::slice($name, 0, -2));
return array(
self::IMENIT => $prefix.'ий',
self::RODIT => $prefix.(self::isVelarConsonant(S::slice($name, -3, -2)) ? 'ого' : 'его'),
self::DAT => $prefix.(self::isVelarConsonant(S::slice($name, -3, -2)) ? 'ому' : 'ему'),
self::VINIT => $prefix.'ий',
self::TVORIT => $prefix.'им',
self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'ем',
);
} else if (S::slice($name, -1) == 'а') {
// Москва, Рига
$prefix = S::name(S::slice($name, 0, -1));
return array(
Expand Down
1 change: 1 addition & 0 deletions tests/Russian/GeographicalNamesDeclensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function wordsProvider() {
array('Азия', array('Азия', 'Азии', 'Азии', 'Азию', 'Азией', 'об Азии')),
array('Рига', array('Рига', 'Риги', 'Риге', 'Ригу', 'Ригой', 'о Риге')),
array('Волга', array('Волга', 'Волги', 'Волге', 'Волгу', 'Волгой', 'о Волге')),
array('Нижний Новгород', array('Нижний Новгород', 'Нижнего Новгорода', 'Нижнему Новгороду', 'Нижний Новгород', 'Нижним Новгородом', 'о Нижнем Новгороде')),
);
}

Expand Down

0 comments on commit 98401fc

Please sign in to comment.