Skip to content

Commit

Permalink
You can use brackets as function parameters with shielding it with qu…
Browse files Browse the repository at this point in the history
…otes.

Better support utf-8.
  • Loading branch information
AndyDune committed Aug 10, 2018
1 parent 7d570b9 commit 1b09709
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
72 changes: 67 additions & 5 deletions src/PowerReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,26 @@ protected function callbackReplace($key)
//$value = $this->dataToReplace[$key];
if ($functions) {
array_walk($functions, function ($function, $key) use (&$value, $functionsHolder) {
$data = $this->findFunctionParams($function);
$function = $data['function'];
$params = $data['params'];

if ($params) {
$params = $this->explode($params);
} else {
$params = [];
}

/*
$count = preg_match('|([_a-z0-9]+)\(([^\)]*)\)|ui', $function, $marches);
if ($count) {
$function = $marches[1];
$params = $this->explode($marches[2]);
} else {
$params = [];
}
*/

array_unshift($params, $value);
$value = $functionsHolder->executeFunction($function, $params);
});
Expand All @@ -114,6 +127,26 @@ protected function callbackReplace($key)
return '';
}


protected function findFunctionParams($functionAsString)
{
$functionAsString = trim($functionAsString);
$result = [
'function' => $functionAsString,
'params' => ''
];
$partsOpenBracket = explode('(', $functionAsString);

if (count($partsOpenBracket) == 1) {
return $result;
}

$result['function'] = trim(array_shift($partsOpenBracket));
$result['params'] = rtrim(implode('(', $partsOpenBracket), ')');

return $result;
}

public function getValueFromDataToReplace($key)
{
if (array_key_exists($key, $this->dataToReplace)) {
Expand All @@ -128,7 +161,8 @@ public function getValueFromDataToReplace($key)
$accumulatedString = '';
$value = 0;
$opened = false;
$keyArray = str_split($key);
//$keyArray = str_split($key);
$keyArray = $this->utf8Split($key);
foreach ($keyArray as $char) {
if (!in_array($char, ['[', ']'])) {
$accumulatedString .= $char;
Expand Down Expand Up @@ -210,9 +244,26 @@ protected function explodeWithFunctions($string, $separator = ':')
$accumulatedString = '';
$value = 0;
$openedBracket = 0;
$openedQuote = '';
$stringArray = str_split($string);
$openedQuote = false;
$openedSingleQuote = false;
//$stringArray = str_split($string);
$stringArray = $this->utf8Split($string);
foreach ($stringArray as $char) {

if ($char == '"') {
if (!$openedSingleQuote) {
$openedQuote = !$openedQuote;
}
}

if ($char == "'") {
if (!$openedQuote) {
$openedSingleQuote = !$openedSingleQuote;
}
}

$closedAllQuote = !($openedSingleQuote or $openedQuote);

if (!$openedBracket and $char == $separator) {
if ($accumulatedString) {
$result[] = $accumulatedString;
Expand All @@ -221,11 +272,11 @@ protected function explodeWithFunctions($string, $separator = ':')
continue;
}

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

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

Expand Down Expand Up @@ -270,4 +321,15 @@ public function set($name, $value)
return $this;
}


function utf8Split($str, $len = 1)
{
$arr = array();
$strLen = mb_strlen($str, 'UTF-8');
for ($i = 0; $i < $strLen; $i++) {
$arr[] = mb_substr($str, $i, $len, 'UTF-8');
}
return $arr;
}

}
40 changes: 38 additions & 2 deletions test/StringReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,32 @@ public function testBrackets()
$instance->growth = 180;
$this->assertEquals('Params: :80:', $instance->replace($string));

/*
$string = 'Params: #weight:prefix(\':\'):postfix(":")#';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: :80:', $instance->replace($string));


$string = 'Params: #weight:prefix("("):postfix(")")#';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: (80)', $instance->replace($string));
*/


$string = "Params: #weight:prefix('('):postfix(')')#";
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: (80)', $instance->replace($string));

$string = "Params: #weight:prefix(\"(\"):postfix(')')#";
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: (80)', $instance->replace($string));

}


Expand Down Expand Up @@ -253,6 +272,23 @@ public function testPowerWrongFunctions()
$instance = new PowerReplace();
$instance->it = 'eat';
$this->assertEquals('I know to eat', $instance->replace($string));


// It has not mixed qoutes
$string = 'Params: #weight:prefix("(\'):postfix(")")#';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: (\'):postfix(")80', $instance->replace($string));


// It has not qoutes to protect
$string = 'Params: #weight:prefix(():postfix(")")#';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$this->assertEquals('Params: ():postfix(")"80', $instance->replace($string));

}

public function testValueFromArray()
Expand Down

0 comments on commit 1b09709

Please sign in to comment.