Skip to content

Commit

Permalink
Add function to add any prefix (it must not contain :) for not empty …
Browse files Browse the repository at this point in the history
…values.
  • Loading branch information
AndyDune committed Aug 3, 2018
1 parent ef84fd1 commit ede24dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Functions/Prefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 Prefix
{
public function __invoke($string, $prefix = '')
{
if ($string) {
return $prefix . $string;
}
return $string;
}

}
2 changes: 2 additions & 0 deletions src/FunctionsHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ class FunctionsHolder
{
protected $functions = [
'escape' => EscapeHtmlSpecialChars::class,
'prefix' => Prefix::class,
'addcomma' => SetCommaBefore::class,
'maxlen' => LeaveStringWithLength::class,
'pluralrus' => PluralStringRussian::class,
Expand Down
17 changes: 17 additions & 0 deletions test/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ede24dd

Please sign in to comment.