mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-08 22:36:32 +02:00
Add append and prepend
This commit is contained in:
18
README.md
18
README.md
@@ -16,6 +16,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy'
|
||||
* [Class methods](#class-methods)
|
||||
* [create](#createmixed-str--encoding-)
|
||||
* [Instance methods](#instance-methods)
|
||||
* [append](#appendstring-string)
|
||||
* [at](#atint-index)
|
||||
* [camelize](#camelize)
|
||||
* [chars](#chars)
|
||||
@@ -57,6 +58,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy'
|
||||
* [padBoth](#padbothint-length--string-padstr----)
|
||||
* [padLeft](#padleftint-length--string-padstr----)
|
||||
* [padRight](#padrightint-length--string-padstr----)
|
||||
* [prepend](#prependstring-string)
|
||||
* [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)
|
||||
* [removeLeft](#removeleftstring-substring)
|
||||
* [removeRight](#removerightstring-substring)
|
||||
@@ -204,6 +206,14 @@ $stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
|
||||
|
||||
Stringy objects are immutable. Methods return new instances.
|
||||
|
||||
##### append(string $string)
|
||||
|
||||
Returns a new string with $string appended.
|
||||
|
||||
```php
|
||||
S::create('fòô')->append('bàř'); // 'fòôbàř'
|
||||
```
|
||||
|
||||
##### at(int $index)
|
||||
|
||||
Returns the character at $index, with indexes starting at 0.
|
||||
@@ -566,6 +576,14 @@ padded. Alias for pad() with a $padType of 'right'.
|
||||
S::create('foo bar')->padRight(10, '_*'); // 'foo bar_*_'
|
||||
```
|
||||
|
||||
##### prepend(string $string)
|
||||
|
||||
Returns a new string starting with $string.
|
||||
|
||||
```php
|
||||
S::create('bàř')->prepend('fòô'); // 'fòôbàř'
|
||||
```
|
||||
|
||||
##### regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
|
||||
|
||||
Replaces all occurrences of $pattern in $str by $replacement. An alias
|
||||
|
@@ -178,6 +178,28 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
throw new \Exception('Stringy object is immutable, cannot unset char');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with $string appended.
|
||||
*
|
||||
* @param string $string The string to append
|
||||
* @return Stringy Object with appended $string
|
||||
*/
|
||||
public function append($string)
|
||||
{
|
||||
return static::create($this->str . $string, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string starting with $string.
|
||||
*
|
||||
* @param string $string The string to append
|
||||
* @return Stringy Object with appended $string
|
||||
*/
|
||||
public function prepend($string)
|
||||
{
|
||||
return static::create($string . $this->str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array consisting of the characters in the string.
|
||||
*
|
||||
|
@@ -201,6 +201,42 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider appendProvider()
|
||||
*/
|
||||
public function testAppend($expected, $str, $string, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->append($string);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function appendProvider()
|
||||
{
|
||||
return array(
|
||||
array('foobar', 'foo', 'bar'),
|
||||
array('fòôbàř', 'fòô', 'bàř', 'UTF-8')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider prependProvider()
|
||||
*/
|
||||
public function testPrepend($expected, $str, $string, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->prepend($string);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function prependProvider()
|
||||
{
|
||||
return array(
|
||||
array('foobar', 'bar', 'foo'),
|
||||
array('fòôbàř', 'bàř', 'fòô', 'UTF-8')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user