diff --git a/README.md b/README.md index 649d035..3d92e76 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele * [at](#at) * [first](#first) * [last](#last) + * [ensureLeft](#ensureLeft) + * [ensureRight](#ensureRight) * [Tests](#tests) * [License](#license) @@ -646,34 +648,46 @@ S::create('fòô bàř', 'UTF-8')->last(3); S::last('fòô bàř', 3, 'UTF-8'); // 'bàř' ``` +##### ensureLeft + +$stringy->ensureLeft(string $substring) + +S::ensureLeft(string $substring [, string $encoding ]) + +Ensures that $str begins with $substring. + +```php +S::create('foobar')->ensureLeft('http://'); +S::ensureLeft('foobar', 'http://'); // 'http://foobar' +``` + +##### ensureRight + +$stringy->ensureRight(string $substring) + +S::ensureRight(string $substring [, string $encoding ]) + +Ensures that $str ends with $substring. + +```php +S::create('foobar')->ensureRight('.com'); +S::ensureRight('foobar', '.com'); // 'foobar.com' +``` + ## TODO **count** => substr_count **wordCount** => str_word_count -**isMultibyte** - **wordWrap** -**chars** $callback - -**words** $callback - -**paragraphs** - -**lines** - **excerpt** ($str, $substring, $radius) **pluralize** ($count, $singular, $plural = null) **toBoolean** -**ensureLeft** - -**ensureRight** - **isAlpha** **isAlphaNumeric** @@ -684,6 +698,10 @@ S::last('fòô bàř', 3, 'UTF-8'); // 'bàř' **isBlank** +**chompLeft** + +**chompRight** + ## Tests [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) diff --git a/src/Stringy/StaticStringy.php b/src/Stringy/StaticStringy.php index 2d231ba..9843d65 100644 --- a/src/Stringy/StaticStringy.php +++ b/src/Stringy/StaticStringy.php @@ -526,4 +526,28 @@ class StaticStringy { return Stringy::create($str, $encoding)->last($n)->str; } + + /** + * Ensures that $str begins with $substring. + * + * @param string $substring The substring to add if not present + * @param string $encoding The character encoding + * @return string The string prefixed by the $substring + */ + public static function ensureLeft($str, $substring, $encoding = null) + { + return Stringy::create($str, $encoding)->ensureLeft($substring)->str; + } + + /** + * Ensures that $str ends with $substring. + * + * @param string $substring The substring to add if not present + * @param string $encoding The character encoding + * @return string The string suffixed by the $substring + */ + public static function ensureRight($str, $substring, $encoding = null) + { + return Stringy::create($str, $encoding)->ensureRight($substring)->str; + } } diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index efc34f4..e568c64 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -824,4 +824,32 @@ class Stringy return $this; } + + /** + * Ensures that $str begins with $substring. + * + * @param string $substring The substring to add if not present + * @return Stringy Object with its $str prefixed by the $substring + */ + public function ensureLeft($substring) + { + if (!$this->startsWith($substring)) + $this->str = $substring . $this->str; + + return $this; + } + + /** + * Ensures that $str ends with $substring. + * + * @param string $substring The substring to add if not present + * @return Stringy Object with its $str suffixed by the $substring + */ + public function ensureRight($substring) + { + if (!$this->endsWith($substring)) + $this->str .= $substring; + + return $this; + } } diff --git a/tests/Stringy/CommonTest.php b/tests/Stringy/CommonTest.php index d573e03..cfdb5b9 100644 --- a/tests/Stringy/CommonTest.php +++ b/tests/Stringy/CommonTest.php @@ -633,6 +633,42 @@ class CommonTest extends PHPUnit_Framework_TestCase return $testData; } + public function stringsForEnsureLeft() + { + $testData = array( + array('foobar', 'foobar', 'f'), + array('foobar', 'foobar', 'foo'), + array('foo/foobar', 'foobar', 'foo/'), + array('http://foobar', 'foobar', 'http://'), + array('http://foobar', 'http://foobar', 'http://'), + array('fòôbàř', 'fòôbàř', 'f', 'UTF-8'), + array('fòôbàř', 'fòôbàř', 'fòô', 'UTF-8'), + array('fòô/fòôbàř', 'fòôbàř', 'fòô/', 'UTF-8'), + array('http://fòôbàř', 'fòôbàř', 'http://', 'UTF-8'), + array('http://fòôbàř', 'http://fòôbàř', 'http://', 'UTF-8'), + ); + + return $testData; + } + + public function stringsForEnsureRight() + { + $testData = array( + array('foobar', 'foobar', 'r'), + array('foobar', 'foobar', 'bar'), + array('foobar/bar', 'foobar', '/bar'), + array('foobar.com/', 'foobar', '.com/'), + array('foobar.com/', 'foobar.com/', '.com/'), + array('fòôbàř', 'fòôbàř', 'ř', 'UTF-8'), + array('fòôbàř', 'fòôbàř', 'bàř', 'UTF-8'), + array('fòôbàř/bàř', 'fòôbàř', '/bàř', 'UTF-8'), + array('fòôbàř.com/', 'fòôbàř', '.com/', 'UTF-8'), + array('fòôbàř.com/', 'fòôbàř.com/', '.com/', 'UTF-8'), + ); + + return $testData; + } + // A test is required so as not to throw an error // This is a lot cleaner than using PHPUnit's mocks to spy public function test() { diff --git a/tests/Stringy/StaticStringyTest.php b/tests/Stringy/StaticStringyTest.php index 89e5277..1eaa0c5 100644 --- a/tests/Stringy/StaticStringyTest.php +++ b/tests/Stringy/StaticStringyTest.php @@ -354,4 +354,22 @@ class StaticStringyTestCase extends CommonTest $result = S::last($str, $n, $encoding); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForEnsureLeft + */ + public function testEnsureLeft($expected, $str, $substring, $encoding = null) + { + $result = S::ensureLeft($str, $substring, $encoding); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForEnsureRight + */ + public function testEnsureRight($expected, $str, $substring, $encoding = null) + { + $result = S::ensureRight($str, $substring, $encoding); + $this->assertEquals($expected, $result); + } } diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index a233a56..5f52d56 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -336,4 +336,22 @@ class StringyTestCase extends CommonTest $result = S::create($str, $encoding)->last($n); $this->assertEquals($expected, $result); } + + /** + * @dataProvider stringsForEnsureLeft + */ + public function testEnsureLeft($expected, $str, $substring, $encoding = null) + { + $result = S::create($str, $encoding)->ensureLeft($substring); + $this->assertEquals($expected, $result); + } + + /** + * @dataProvider stringsForEnsureRight + */ + public function testEnsureRight($expected, $str, $substring, $encoding = null) + { + $result = S::create($str, $encoding)->ensureRight($substring); + $this->assertEquals($expected, $result); + } }