mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 00:04:11 +02:00
Added ensureLeft() and ensureRight()
This commit is contained in:
46
README.md
46
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
|
||||
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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() {
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user