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

Add append and prepend

This commit is contained in:
Daniel St. Jules
2015-07-22 23:30:02 -07:00
parent f77d9903fa
commit aef6f0c200
3 changed files with 76 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy'
* [Class methods](#class-methods) * [Class methods](#class-methods)
* [create](#createmixed-str--encoding-) * [create](#createmixed-str--encoding-)
* [Instance methods](#instance-methods) * [Instance methods](#instance-methods)
* [append](#appendstring-string)
* [at](#atint-index) * [at](#atint-index)
* [camelize](#camelize) * [camelize](#camelize)
* [chars](#chars) * [chars](#chars)
@@ -57,6 +58,7 @@ S::create('string')->toTitleCase()->ensureRight('y') == 'Stringy'
* [padBoth](#padbothint-length--string-padstr----) * [padBoth](#padbothint-length--string-padstr----)
* [padLeft](#padleftint-length--string-padstr----) * [padLeft](#padleftint-length--string-padstr----)
* [padRight](#padrightint-length--string-padstr----) * [padRight](#padrightint-length--string-padstr----)
* [prepend](#prependstring-string)
* [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr) * [regexReplace](#regexreplacestring-pattern-string-replacement--string-options--msr)
* [removeLeft](#removeleftstring-substring) * [removeLeft](#removeleftstring-substring)
* [removeRight](#removerightstring-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. 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) ##### at(int $index)
Returns the character at $index, with indexes starting at 0. 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_*_' 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']) ##### regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
Replaces all occurrences of $pattern in $str by $replacement. An alias Replaces all occurrences of $pattern in $str by $replacement. An alias

View File

@@ -178,6 +178,28 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
throw new \Exception('Stringy object is immutable, cannot unset char'); 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. * Returns an array consisting of the characters in the string.
* *

View File

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