Skip to content

Commit

Permalink
Functions may use ' or " as params. Add functions: plural and printf.…
Browse files Browse the repository at this point in the history
… Tested.
  • Loading branch information
AndyDune committed May 15, 2018
1 parent 9d06db1 commit aaa39a4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Functions/PluralStringEnglish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
*
* PHP version >= 5.6
*
* @package andydune/string-replace
* @link https://github.com/AndyDune/StringReplace for the canonical source repository
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrey Ryzhov <info@rznw.ru>
* @copyright 2018 Andrey Ryzhov
*/


namespace AndyDune\StringReplace\Functions;


class PluralStringEnglish
{
public function __invoke($string, $form1 = '', $form2 = '')
{
$n = (int)$string;
if ($n == 1) return $form1;
return $form2;
}

}
28 changes: 28 additions & 0 deletions src/Functions/PrintFormatted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
*
* PHP version >= 5.6
*
* @package andydune/string-replace
* @link https://github.com/AndyDune/StringReplace for the canonical source repository
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @author Andrey Ryzhov <info@rznw.ru>
* @copyright 2018 Andrey Ryzhov
*/


namespace AndyDune\StringReplace\Functions;


class PrintFormatted
{
public function __invoke($string, $format = '')
{
$string = trim($string);
if ($string) {
return sprintf($format, $string);
}
return '';
}

}
6 changes: 5 additions & 1 deletion src/FunctionsHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use AndyDune\StringReplace\Functions\LeaveStringWithLength;
use AndyDune\StringReplace\Functions\PluralStringRussian;
use AndyDune\StringReplace\Functions\SetCommaBefore;
use AndyDune\StringReplace\Functions\PluralStringEnglish;
use AndyDune\StringReplace\Functions\PrintFormatted;
use Exception;

class FunctionsHolder
Expand All @@ -24,7 +26,9 @@ class FunctionsHolder
'escape' => EscapeHtmlSpecialChars::class,
'addcomma' => SetCommaBefore::class,
'maxlen' => LeaveStringWithLength::class,
'pluralrus' => PluralStringRussian::class
'pluralrus' => PluralStringRussian::class,
'plural' => PluralStringEnglish::class,
'printf' => PrintFormatted ::class
];

public function executeFunction($name, $arguments)
Expand Down
7 changes: 6 additions & 1 deletion src/PowerReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ protected function explode($string, $separator = ',')
$array = explode($separator, $string);
array_walk($array, function ($value) use (&$result) {
$value = trim($value);
$value = trim($value, '\'"');
if (substr($value, 0, 1) == '"') {
$value = trim($value, '"');
} else {
$value = trim($value, '\'');
}

if ($value) {
$result[] = $value;
}
Expand Down
21 changes: 21 additions & 0 deletions test/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ public function testPower()
$instance->count = 21;
$this->assertEquals('У меня есть 21 яблоко ', $instance->replace($string));

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


$string = 'I see #count# #count:plural(man, men)#';
$instance = new PowerReplace();
$instance->count = 1;
$this->assertEquals('I see 1 man', $instance->replace($string));
$instance->count = 21;
$this->assertEquals('I see 21 men', $instance->replace($string));


$string = 'I know words: #it:printf(«%s»):addcomma(1)##and_it:printf(«%s»):addcomma(1)# and #and_it_2:printf(«%s»):addcomma(1, 1)#';
$instance = new PowerReplace();
$instance->it = 'eat';
$instance->and_it_2 = 'sleep';
$this->assertEquals('I know words: «eat» and «sleep»', $instance->replace($string));


}

public function testPowerAddCustomFunction()
Expand Down

0 comments on commit aaa39a4

Please sign in to comment.