diff --git a/README.md b/README.md index a638c04..5a35867 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy' * [containsAny](#containsanyarray-needles--boolean-casesensitive--true-) * [countSubstr](#countsubstrstring-substring--boolean-casesensitive--true-) * [dasherize](#dasherize) - * [delimit](#delimitdelimiter) + * [delimit](#delimitint-delimiter) * [endsWith](#endswithstring-substring--boolean-casesensitive--true-) * [ensureLeft](#ensureleftstring-substring) * [ensureRight](#ensurerightstring-substring) @@ -62,6 +62,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy' * [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr) * [removeLeft](#removeleftstring-substring) * [removeRight](#removerightstring-substring) + * [repeat](#repeatmultiplier) * [replace](#replacestring-search-string-replacement) * [reverse](#reverse) * [safeTruncate](#safetruncateint-length--string-substring---) @@ -307,7 +308,7 @@ character of the string), and in place of spaces as well as underscores. S::create('TestDCase')->dasherize(); // 'test-d-case' ``` -##### delimit($delimiter) +##### delimit(int $delimiter) Returns a lowercase and trimmed string separated by the given delimiter. Delimiters are inserted before uppercase characters (with the exception @@ -617,6 +618,14 @@ Returns a new string with the suffix $substring removed, if present. S::create('fòô bàř')->removeRight(' bàř'); // 'fòô' ``` +##### repeat(int $multiplier) + +Returns a repeated string given a multiplier. An alias for str_repeat. + +```php +S::create('à')->repeat(3); // 'ààà' +``` + ##### replace(string $search, string $replacement) Replaces all occurrences of $search in $str by $replacement. diff --git a/src/Stringy.php b/src/Stringy.php index b36d650..8dcb623 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1094,6 +1094,19 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess } /** + * Returns a repeated string given a multiplier. An alias for str_repeat. + * + * @param int $multiplier The number of times to repeat the string + * @return Stringy Object with a repeated str + */ + public function repeat($multiplier) + { + $repeated = str_repeat($this->str, $multiplier); + + return static::create($repeated, $this->encoding); + } + + /* * A multibyte str_shuffle() function. It returns a string with its * characters in random order. * diff --git a/tests/StringyTest.php b/tests/StringyTest.php index d51d73a..f40e371 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -1298,6 +1298,31 @@ class StringyTestCase extends PHPUnit_Framework_TestCase ); } + /** + * @dataProvider repeatProvider() + */ + public function testRepeat($expected, $str, $multiplier, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->repeat($multiplier); + $this->assertStringy($result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + public function repeatProvider() + { + return array( + array('', 'foo', 0), + array('foo', 'foo', 1), + array('foofoo', 'foo', 2), + array('foofoofoo', 'foo', 3), + array('fòô', 'fòô', 1, 'UTF-8'), + array('fòôfòô', 'fòô', 2, 'UTF-8'), + array('fòôfòôfòô', 'fòô', 3, 'UTF-8') + ); + } + /** * @dataProvider shuffleProvider() */