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

Поддержка имён в верхнем регистре #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions Petrovich.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Petrovich {
const GENDER_MALE = 1; // Мужской
const GENDER_FEMALE = 2; // Женский

private $gender = Petrovich::GENDER_ANDROGYNOUS; //Пол male/мужской female/женский
private $gender = Petrovich::GENDER_ANDROGYNOUS; //Пол male/мужской female/женский

/**
* Конструтор класса Петрович
Expand Down Expand Up @@ -48,8 +48,8 @@ public function detectGender($middlename)
{
if(empty($middlename))
throw new Exception('Middlename cannot be empty.');
switch ( mb_substr( mb_strtolower($middlename) , -4))
switch ( mb_substr( mb_strtolower($middlename) , -4))
{
case 'оглы': return Petrovich::GENDER_MALE; break;
case 'кызы': return Petrovich::GENDER_FEMALE; break;
Expand Down Expand Up @@ -157,8 +157,8 @@ private function findInRules($name,$case,$type) {
if ( ! $this->checkGender($rule->gender) )
continue;
foreach($rule->test as $last_char) {
$last_name_char = mb_substr($name,mb_strlen($name)-mb_strlen($last_char),mb_strlen($last_char));
if($last_char == $last_name_char) {
$last_name_char = mb_strtolower(mb_substr($name,mb_strlen($name)-mb_strlen($last_char),mb_strlen($last_char)));
if(mb_strtolower($last_char) == $last_name_char) {
if($rule->mods[$case] == '.')
return $name;
return $this->applyRule($rule->mods,$name,$case);
Expand Down Expand Up @@ -203,7 +203,13 @@ private function checkException($name,$case,$type) {
* @return string
*/
private function applyRule($mods,$name,$case) {
$nameWithoutFirstLetter = mb_substr($name, 1);
$isUpperCase = ($nameWithoutFirstLetter != mb_strtolower($nameWithoutFirstLetter));
$result = mb_substr($name,0,mb_strlen($name) - mb_substr_count($mods[$case],'-'));
if($isUpperCase)
{
$mods[$case] = mb_strtoupper($mods[$case]);
}
$result .= str_replace('-','',$mods[$case]);
return $result;
}
Expand Down