Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Phalcon\Text:dynamic() to allow custom separator #11927

Merged
merged 1 commit into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
- Added `Phalcon\Http\Request::getPort`. To get information about the port on which the request is made.
- Added `Phalcon\Http\Request::setStrictHostCheck` and `Phalcon\Http\Request::isStrictHostCheck` to manage strict validation of host name.
- Fixed matching host name by `Phalcon\Mvc\Route::handle` when using port on current host name [#2573](https://github.com/phalcon/cphalcon/issues/2573)
- Fixed `Phalcon\Text:dynamic()` to allow custom separator [#11215](https://github.com/phalcon/cphalcon/issues/11215)

# [2.0.13](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.13) (2016-05-19)
- Restored `Phalcon\Text::camelize` behavior [#11767](https://github.com/phalcon/cphalcon/issues/11767)
Expand Down
36 changes: 23 additions & 13 deletions phalcon/text.zep
Original file line number Diff line number Diff line change
Expand Up @@ -254,30 +254,40 @@ abstract class Text
* echo Phalcon\Text::dynamic("{Hi|Hello}, my name is a {Bob|Mark|Jon}!"); // Hi my name is a Bob
* echo Phalcon\Text::dynamic("{Hi|Hello}, my name is a {Bob|Mark|Jon}!"); // Hi my name is a Jon
* echo Phalcon\Text::dynamic("{Hi|Hello}, my name is a {Bob|Mark|Jon}!"); // Hello my name is a Bob
* echo Phalcon\Text::dynamic("[Hi/Hello], my name is a [Zyxep/Mark]!", '[', ']', '/'); // Hello my name is a Zyxep
* </code>
*/
public static function dynamic(string! text, string! leftDelimiter = "{", string! rightDelimiter = "}", string! separator = "|") -> string
{
var ldS, rdS, result, pattern;
var ldS, rdS, pattern, matches, match, words, word, sub;

if substr_count(text, leftDelimiter) !== substr_count(text, rightDelimiter) {
throw new \RuntimeException("Syntax error in string \"" . text . "\"");
}

let ldS = preg_quote(leftDelimiter);
let rdS = preg_quote(rightDelimiter);
let pattern = "/" . ldS . "([^" . ldS . rdS . "]+)" . rdS . "/";
let result = text;

while memstr(result, leftDelimiter) {
let result = preg_replace_callback(pattern, function (matches) {
var words;
let words = explode("|", matches[1]);
return words[array_rand(words)];
}, result);
let ldS = preg_quote(leftDelimiter),
rdS = preg_quote(rightDelimiter),
pattern = "/" . ldS . "([^" . ldS . rdS . "]+)" . rdS . "/",
matches = [];

if !preg_match_all(pattern, text, matches, 2) {
return text;
}

if typeof matches == "array" {
for match in matches {
if !isset match[0] || !isset match[1] {
continue;
}

let words = explode(separator, match[1]),
word = words[array_rand(words)],
sub = preg_quote(match[0], separator),
text = preg_replace("/" . sub . "/", word, text, 1);
}
}

return result;
return text;
}

/**
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/Text/TextDynamicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,51 @@ function () {
* @author Stanislav Kiryukhin <korsar.zn@gmail.com>
* @since 2015-07-01
*/
public function testTextDynamicStringCustomDelimeter()
public function testTextDynamicStringCustomDelimiter()
{
$actual = Text::dynamic('(Hi|Hello), my name is a Bob!', '(', ')');
expect($actual)->notContains('(');
expect($actual)->notContains(')');

expect(preg_match('/^(Hi|Hello), my name is a Bob!$/', $actual))->equals(1);
}

/**
* Tests custom separator
*
* @issue 11215
* @author Serghei Iakovlev <serghei@phalconphp.com>
* @since 2016-06-27
*/
public function testTextDynamicStringCustomSeparator()
{
$this->specify(
'Text::dynamic with custom separator does not return expected result',
function () {
$actual = Text::dynamic('{Hi=Hello}, my name is a Bob!', '{', '}', '=');

expect($actual)->notContains('{');
expect($actual)->notContains('}');
expect($actual)->notContains('=');

expect(preg_match('/^(Hi|Hello), my name is a Bob!$/', $actual))->equals(1);

$actual = Text::dynamic("{Hi'Hello}, my name is a {Rob'Zyxep'Andres}!", '{', '}', "'");

expect($actual)->notContains('{');
expect($actual)->notContains('}');
expect($actual)->notContains("'");

expect(preg_match('/^(Hi|Hello), my name is a (Rob|Zyxep|Andres)!$/', $actual))->equals(1);

$actual = Text::dynamic('{Hi/Hello}, my name is a {Stanislav/Nikos}!', '{', '}', '/');

expect($actual)->notContains('{');
expect($actual)->notContains('}');
expect($actual)->notContains('/');

expect(preg_match('/^(Hi|Hello), my name is a (Stanislav|Nikos)!$/', $actual))->equals(1);
}
);
}
}