From ede24dd8c78ffe95dbafe2e52b18e6a1f31db105 Mon Sep 17 00:00:00 2001 From: Andrey Ryzhov Date: Fri, 3 Aug 2018 17:10:08 +0300 Subject: [PATCH] Add function to add any prefix (it must not contain :) for not empty values. --- src/Functions/Prefix.php | 27 +++++++++++++++++++++++++++ src/FunctionsHolder.php | 2 ++ test/StringReplaceTest.php | 17 +++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/Functions/Prefix.php diff --git a/src/Functions/Prefix.php b/src/Functions/Prefix.php new file mode 100644 index 0000000..82dd87f --- /dev/null +++ b/src/Functions/Prefix.php @@ -0,0 +1,27 @@ += 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 + * @copyright 2018 Andrey Ryzhov + */ + + +namespace AndyDune\StringReplace\Functions; + + +class Prefix +{ + public function __invoke($string, $prefix = '') + { + if ($string) { + return $prefix . $string; + } + return $string; + } + +} \ No newline at end of file diff --git a/src/FunctionsHolder.php b/src/FunctionsHolder.php index 90257bd..6191950 100644 --- a/src/FunctionsHolder.php +++ b/src/FunctionsHolder.php @@ -15,6 +15,7 @@ use AndyDune\StringReplace\Functions\FunctionAbstract; use AndyDune\StringReplace\Functions\LeaveStringWithLength; use AndyDune\StringReplace\Functions\PluralStringRussian; +use AndyDune\StringReplace\Functions\Prefix; use AndyDune\StringReplace\Functions\SetCommaBefore; use AndyDune\StringReplace\Functions\PluralStringEnglish; use AndyDune\StringReplace\Functions\PrintFormatted; @@ -24,6 +25,7 @@ class FunctionsHolder { protected $functions = [ 'escape' => EscapeHtmlSpecialChars::class, + 'prefix' => Prefix::class, 'addcomma' => SetCommaBefore::class, 'maxlen' => LeaveStringWithLength::class, 'pluralrus' => PluralStringRussian::class, diff --git a/test/StringReplaceTest.php b/test/StringReplaceTest.php index 4d2b023..ff1ae59 100644 --- a/test/StringReplaceTest.php +++ b/test/StringReplaceTest.php @@ -165,6 +165,23 @@ public function testPower() $this->assertEquals('I know words: «eat» and «sleep»', $instance->replace($string)); + $string = 'Vegetables I have: #apple_count:prefix("apples "):addcomma(1)##orange_count:prefix("oranges "):addcomma(1)#'; + $instance = new PowerReplace(); + $instance->apple_count = 1; + $this->assertEquals('Vegetables I have: apples 1', $instance->replace($string)); + + $string = 'Vegetables I have: #apple_count:prefix("apples "):addcomma(1)##orange_count:prefix("oranges "):addcomma(1)#'; + $instance = new PowerReplace(); + $instance->orange_count = 1; + $this->assertEquals('Vegetables I have: oranges 1', $instance->replace($string)); + + $string = 'Vegetables I have: #apple_count:prefix("apples "):addcomma(1)##orange_count:prefix("oranges "):addcomma(1)#'; + $instance = new PowerReplace(); + $instance->orange_count = 1; + $instance->apple_count = 2; + $this->assertEquals('Vegetables I have: apples 2, oranges 1', $instance->replace($string)); + + } public function testPowerAddCustomFunction()