diff --git a/src/FunctionsHolder.php b/src/FunctionsHolder.php index 0e0a85d..cf808a8 100644 --- a/src/FunctionsHolder.php +++ b/src/FunctionsHolder.php @@ -20,7 +20,8 @@ class FunctionsHolder protected $functions = [ 'escape' => 'htmlSpecialChars', 'addcomma' => 'setCommaBefore', - 'maxlen' => 'leaveStringWithLength' + 'maxlen' => 'leaveStringWithLength', + 'pluralrus' => 'showPluralStringRussian' ]; public function __call($name, $arguments) @@ -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) { diff --git a/src/PowerReplace.php b/src/PowerReplace.php index 7379060..702956a 100644 --- a/src/PowerReplace.php +++ b/src/PowerReplace.php @@ -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]); diff --git a/test/StringReplaceTest.php b/test/StringReplaceTest.php index ca93efb..077fa6f 100644 --- a/test/StringReplaceTest.php +++ b/test/StringReplaceTest.php @@ -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)); + } } \ No newline at end of file