1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-09 14:56:31 +02:00

Added repeat

This commit is contained in:
Daniel St. Jules
2015-07-23 23:53:33 -07:00
parent bded15d683
commit 2d34e72cde
3 changed files with 49 additions and 2 deletions

View File

@@ -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.

View File

@@ -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.
*

View File

@@ -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()
*/