Skip to content

Commit

Permalink
You can use functions separator (:) as common char inside brackets.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyDune committed Aug 6, 2018
1 parent 3a5457c commit b0979aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/PowerReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ protected function callbackReplace($key)
$functionsHolder = $this->functionsHolder;

if ($functionsHolder) {
$functions = $this->explode($key, $this->markerFunctionsSeparator);
//$functions = $this->explode($key, $this->markerFunctionsSeparator);
$functions = $this->explodeWithFunctions($key, $this->markerFunctionsSeparator);
$key = array_shift($functions);
} else {
$functions = false;
Expand Down Expand Up @@ -199,6 +200,45 @@ protected function explode($string, $separator = ',')
return $result;
}

protected function explodeWithFunctions($string, $separator = ':')
{
if (!strpos($string, $separator)) {
return [$string];
}

$result = [];
$accumulatedString = '';
$value = 0;
$openedBracket = 0;
$openedQuote = '';
$stringArray = str_split($string);
foreach ($stringArray as $char) {
if (!$openedBracket and $char == $separator) {
if ($accumulatedString) {
$result[] = $accumulatedString;
$accumulatedString = '';
}
continue;
}

if ($char == '(') {
$openedBracket++;
}

if ($char == ')') {
$openedBracket--;
}

$accumulatedString .= $char;
}

if ($accumulatedString) {
$result[] = $accumulatedString;
}

return $result;
}

/**
* Set whole data array.
*
Expand Down
7 changes: 7 additions & 0 deletions test/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ public function testPower()
$instance->apple_count = 2;
$this->assertEquals('Vegetables I have: apples 2, 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 testPowerWrongFunctions()
Expand Down

0 comments on commit b0979aa

Please sign in to comment.