Skip to content

Commit

Permalink
Add function pluralrus for name numbers for Russian.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyDune committed May 3, 2018
1 parent 1f1790c commit f194e2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/FunctionsHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class FunctionsHolder
protected $functions = [
'escape' => 'htmlSpecialChars',
'addcomma' => 'setCommaBefore',
'maxlen' => 'leaveStringWithLength'
'maxlen' => 'leaveStringWithLength',
'pluralrus' => 'showPluralStringRussian'
];

public function __call($name, $arguments)
Expand All @@ -44,6 +45,17 @@ public function setCommaBefore($string)
return '';
}

public function showPluralStringRussian($string, $form1 = '', $form2 = '', $form3 = '')
{
$n = (int)$string;
$n = abs($n) % 100;
$n1 = $n % 10;
if ($n > 10 && $n < 20) return $form3;
if ($n1 > 1 && $n1 < 5) return $form2;
if ($n1 == 1) return $form1;
return $form3;
}

public function leaveStringWithLength($string, $length = 100)
{
if (mb_strlen ($string, 'UTF-8') > $length) {
Expand Down
2 changes: 1 addition & 1 deletion src/PowerReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function callbackReplace($key)
$value = $this->dataToReplace[$key];
if ($functions) {
array_walk($functions, function ($function, $key) use (&$value, $functionsHolder) {
$count = preg_match('|([_a-z0-9]+)\(([,_a-z0-9]*)\)|ui', $function, $marches);
$count = preg_match('|([_a-z0-9]+)\(([^\)]*)\)|ui', $function, $marches);
if ($count) {
$function = $marches[1];
$params = $this->explode($marches[2]);
Expand Down
18 changes: 18 additions & 0 deletions test/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,23 @@ public function testPower()
$instance->and_it = 'sleep sleep sleep sleep';
$this->assertEquals('I know eat', $instance->replace($string));


$string = 'У меня есть #count# #count:pluralrus(яблоко, яблока, яблок)#';
$instance = new PowerReplace();
$instance->count = 1;
$this->assertEquals('У меня есть 1 яблоко', $instance->replace($string));
$instance->count = 21;
$this->assertEquals('У меня есть 21 яблоко', $instance->replace($string));

$instance->count = 2;
$this->assertEquals('У меня есть 2 яблока', $instance->replace($string));
$instance->count = 23;
$this->assertEquals('У меня есть 23 яблока', $instance->replace($string));

$instance->count = 5;
$this->assertEquals('У меня есть 5 яблок', $instance->replace($string));
$instance->count = 11;
$this->assertEquals('У меня есть 11 яблок', $instance->replace($string));

}
}

0 comments on commit f194e2a

Please sign in to comment.