Skip to content

Commit

Permalink
Merge pull request #10571 from KorsaR-ZN/NFR-4
Browse files Browse the repository at this point in the history
[2.0.x] [NFR] Added method generate random text
  • Loading branch information
andresgutierrez committed Jul 3, 2015
2 parents 35d47f2 + f75ef63 commit 17a30e2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Added `Phalcon\Mvc\Model\Validator\Ip` from incubator
- Added parameter return `defaultValue` in `Phalcon\Mvc\Model\Validator::getOption()`
- Fixed in `Phalcon\Validation\Validator\Identical` the name of parameter `value` to `accepted` according docs
- Added method `Text:dynamic()` generate random text in accordance with the template see [#10571](https://github.com/phalcon/cphalcon/pull/10571)

# [2.0.3](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.3) (2015-06-10)
- Added support for Behaviors in `Phalcon\Mvc\Collection`
Expand Down
33 changes: 33 additions & 0 deletions phalcon/text.zep
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,37 @@ abstract class Text

return rtrim(a, separator) . separator . ltrim(b, separator);
}

/**
* Generates random text in accordance with the template
*
* <code>
* 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
* </code>
*/
public static function dynamic(string! text, string! leftDelimiter = "{", string! rightDelimiter = "}", string! separator = "|") -> string
{
if substr_count(text, leftDelimiter) !== substr_count(text, rightDelimiter) {
throw new \RuntimeException("Syntax error in string \"" . text . "\"");
}

var ld_s, rd_s, result, pattern;

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

while strpos(result, leftDelimiter) !== false {
let result = preg_replace_callback(pattern, function (matches) {
var words;
let words = explode("|", matches[1]);
return words[array_rand(words)];
}, result);
}

return result;
}
}
6 changes: 6 additions & 0 deletions tests/PhalconTest/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ public static function concat($separator, $a, $b)
{
return call_user_func_array('parent::concat', func_get_args());
}

public static function dynamic($text, $leftDelimiter = '{', $rightDelimiter = '}', $separator = '|')
{
return parent::dynamic($text, $leftDelimiter, $rightDelimiter, $separator);
}

}
65 changes: 65 additions & 0 deletions tests/unit/Phalcon/Text/TextDynamicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* TextDynamicTest.php
* \Phalcon\Text\TextDynamicTest
*
* Tests the Phalcon\Text component
*
* Phalcon Framework
*
* @copyright (c) 2011-2015 Phalcon Team
* @link http://www.phalconphp.com
* @author Andres Gutierrez <andres@phalconphp.com>
* @author Nikolaos Dimopoulos <nikos@phalconphp.com>
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to license@phalconphp.com
* so that we can send you a copy immediately.
*/
namespace Phalcon\Tests\unit\Phalcon\Text;


use \PhalconTest\Text as PhTText;


class TextDynamicTest extends Helper\TextBase
{
/**
* Tests the dynamic function
*
* @author Stanislav Kiryukhin <korsar.zn@gmail.com>
* @since 2015-07-01
*/
public function testTextDynamicString()
{
$this->specify(
"dynamic do not return the correct string",
function () {

$actual = PhTText::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 the dynamic function
*
* @author Stanislav Kiryukhin <korsar.zn@gmail.com>
* @since 2015-07-01
*/
public function testTextDynamicStringCustomDelimeter()
{
$actual = PhTText::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);
}
}

0 comments on commit 17a30e2

Please sign in to comment.