1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 00:04:11 +02:00

Stringy methods now return a new Stringy object, rather than modifying the original

This commit is contained in:
Daniel St. Jules
2013-07-28 18:43:08 -04:00
parent f29e400e85
commit a829f2d244
3 changed files with 285 additions and 178 deletions

View File

@@ -113,7 +113,7 @@ echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
## Methods
In the list below, any static method other than S::create refers to a
method in Stringy\StaticStringy. For all others, they're found in Stringy\Stringy.
method in Stringy\StaticStringy. For all others, they're found in Stringy\Stringy. Furthermore, all methods that return an object of type Stringy return a new object, and do not modify the original.
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*

View File

@@ -21,7 +21,7 @@ class Stringy
{
$encoding = $encoding ?: mb_internal_encoding();
$stringyObj = new Stringy();
$stringyObj = new self();
$stringyObj->str = $str;
$stringyObj->encoding = $encoding;
@@ -49,9 +49,9 @@ class Stringy
$rest = mb_substr($this->str, 1, $this->length() - 1,
$this->encoding);
$this->str = mb_strtoupper($first, $this->encoding) . $rest;
$str = mb_strtoupper($first, $this->encoding) . $rest;
return $this;
return self::create($str, $this->encoding);
}
/**
@@ -65,9 +65,9 @@ class Stringy
$rest = mb_substr($this->str, 1, $this->length() - 1,
$this->encoding);
$this->str = mb_strtolower($first, $this->encoding) . $rest;
$str = mb_strtolower($first, $this->encoding) . $rest;
return $this;
return self::create($str, $this->encoding);
}
/**
@@ -80,16 +80,17 @@ class Stringy
public function camelize()
{
$encoding = $this->encoding;
$stringy = self::create($this->str, $this->encoding);
$camelCase = preg_replace_callback(
'/[-_\s]+(.)?/u',
function ($match) use (&$encoding) {
return $match[1] ? mb_strtoupper($match[1], $encoding) : "";
},
$this->trim()->lowerCaseFirst()
$stringy->trim()->lowerCaseFirst()->str
);
$this->str = preg_replace_callback(
$stringy->str = preg_replace_callback(
'/[\d]+(.)?/u',
function ($match) use (&$encoding) {
return mb_strtoupper($match[0], $encoding);
@@ -97,7 +98,7 @@ class Stringy
$camelCase
);
return $this;
return $stringy;
}
/**
@@ -109,9 +110,7 @@ class Stringy
*/
public function upperCamelize()
{
$this->camelize()->upperCaseFirst();
return $this;
return $this->camelize()->upperCaseFirst();
}
/**
@@ -127,13 +126,14 @@ class Stringy
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($this->encoding);
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', $this->trim());
$stringy = self::create($this->str, $this->encoding)->trim();
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', $stringy->str);
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
mb_regex_encoding($regexEncoding);
$this->str = mb_strtolower($dasherized, $this->encoding);
$stringy->str = mb_strtolower($dasherized, $stringy->encoding);
return $this;
return $stringy;
}
/**
@@ -150,13 +150,14 @@ class Stringy
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($this->encoding);
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', $this->trim());
$stringy = self::create($this->str, $this->encoding)->trim();
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', $stringy->str);
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
mb_regex_encoding($regexEncoding);
$this->str = mb_strtolower($underscored, $this->encoding);
$stringy->str = mb_strtolower($underscored, $stringy->encoding);
return $this;
return $stringy;
}
/**
@@ -166,9 +167,10 @@ class Stringy
*/
public function swapCase()
{
$encoding = $this->encoding;
$stringy = self::create($this->str, $this->encoding);
$encoding = $stringy->encoding;
$this->str = preg_replace_callback(
$stringy->str = preg_replace_callback(
'/[\S]/u',
function ($match) use (&$encoding) {
if ($match[0] == mb_strtoupper($match[0], $encoding))
@@ -176,10 +178,10 @@ class Stringy
else
return mb_strtoupper($match[0], $encoding);
},
$this->str
$stringy->str
);
return $this;
return $stringy;
}
/**
@@ -193,21 +195,21 @@ class Stringy
*/
public function titleize($ignore = null)
{
$encoding = $this->encoding;
$that = $this;
$stringy = self::create($this->str, $this->encoding)->trim();
$encoding = $stringy->encoding;
$this->str = preg_replace_callback(
$stringy->str = preg_replace_callback(
'/([\S]+)/u',
function ($match) use (&$encoding, &$ignore, &$that) {
function ($match) use (&$encoding, &$ignore, &$stringy) {
if ($ignore && in_array($match[0], $ignore))
return $match[0];
$that->str = $match[0];
return $that->upperCaseFirst();
$stringy->str = $match[0];
return $stringy->upperCaseFirst();
},
$this->trim()->str
$stringy->str
);
return $this;
return $stringy;
}
/**
@@ -218,10 +220,12 @@ class Stringy
*/
public function humanize()
{
$humanized = str_replace('_id', '', $this->str);
$this->str = str_replace('_', ' ', $humanized);
$stringy = self::create($this->str, $this->encoding);
return $this->trim()->upperCaseFirst();
$humanized = str_replace('_id', '', $stringy->str);
$stringy->str = str_replace('_', ' ', $humanized);
return $stringy->trim()->upperCaseFirst();
}
/**
@@ -232,12 +236,14 @@ class Stringy
*/
public function tidy()
{
$this->str = preg_replace('/\x{2026}/u', '...', $this->str);
$this->str = preg_replace('/[\x{201C}\x{201D}]/u', '"', $this->str);
$this->str = preg_replace('/[\x{2018}\x{2019}]/u', "'", $this->str);
$this->str = preg_replace('/[\x{2013}\x{2014}]/u', '-', $this->str);
$stringy = self::create($this->str, $this->encoding);
return $this;
$stringy->str = preg_replace('/\x{2026}/u', '...', $stringy->str);
$stringy->str = preg_replace('/[\x{201C}\x{201D}]/u', '"', $stringy->str);
$stringy->str = preg_replace('/[\x{2018}\x{2019}]/u', "'", $stringy->str);
$stringy->str = preg_replace('/[\x{2013}\x{2014}]/u', '-', $stringy->str);
return $stringy;
}
/**
@@ -248,9 +254,10 @@ class Stringy
*/
public function collapseWhitespace()
{
$this->str = preg_replace('/\s+/u', ' ', $this->trim());
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/\s+/u', ' ', $stringy->trim());
return $this;
return $stringy;
}
/**
@@ -260,6 +267,7 @@ class Stringy
*/
public function standardize()
{
$stringy = self::create($this->str, $this->encoding);
$charsArray = array(
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
@@ -313,10 +321,10 @@ class Stringy
);
foreach ($charsArray as $key => $value) {
$this->str = str_replace($value, $key, $this->str);
$stringy->str = str_replace($value, $key, $stringy->str);
}
return $this;
return $stringy;
}
/**
@@ -340,11 +348,12 @@ class Stringy
"to be one of 'left', 'right' or 'both'");
}
$strLength = $this->length();
$padStrLength = mb_strlen($padStr, $this->encoding);
$stringy = self::create($this->str, $this->encoding);
$strLength = $stringy->length();
$padStrLength = mb_strlen($padStr, $stringy->encoding);
if ($length <= $strLength || $padStrLength <= 0)
return $this;
return $stringy;
// Number of times to repeat the padStr if left or right
$times = ceil(($length - $strLength) / $padStrLength);
@@ -354,11 +363,11 @@ class Stringy
// Repeat the pad, cut it, and prepend
$leftPad = str_repeat($padStr, $times);
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $this->encoding);
$this->str = $leftPad . $this->str;
$stringy->str = $leftPad . $stringy->str;
} elseif ($padType == 'right') {
// Append the repeated pad and get a substring of the given length
$this->str = $this->str . str_repeat($padStr, $times);
$this->str = mb_substr($this->str, 0, $length, $this->encoding);
$stringy->str = $stringy->str . str_repeat($padStr, $times);
$stringy->str = mb_substr($stringy->str, 0, $length, $this->encoding);
} else {
// Number of times to repeat the padStr on both sides
$paddingSize = ($length - $strLength) / 2;
@@ -371,10 +380,10 @@ class Stringy
$leftPad = str_repeat($padStr, $times);
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $this->encoding);
$this->str = $leftPad . $this->str . $rightPad;
$stringy->str = $leftPad . $stringy->str . $rightPad;
}
return $this;
return $stringy;
}
/**
@@ -472,10 +481,12 @@ class Stringy
*/
public function toSpaces($tabLength = 4)
{
$spaces = str_repeat(' ', $tabLength);
$this->str = str_replace("\t", $spaces, $this->str);
$stringy = self::create($this->str, $this->encoding);
return $this;
$spaces = str_repeat(' ', $tabLength);
$stringy->str = str_replace("\t", $spaces, $stringy->str);
return $stringy;
}
/**
@@ -488,10 +499,12 @@ class Stringy
*/
public function toTabs($tabLength = 4)
{
$spaces = str_repeat(' ', $tabLength);
$this->str = str_replace($spaces, "\t", $this->str);
$stringy = self::create($this->str, $this->encoding);
return $this;
$spaces = str_repeat(' ', $tabLength);
$stringy->str = str_replace($spaces, "\t", $stringy->str);
return $stringy;
}
/**
@@ -504,11 +517,13 @@ class Stringy
*/
public function slugify()
{
$this->str = preg_replace('/[^a-zA-Z\d -]/u', '', $this->standardize());
$this->collapseWhitespace();
$this->str = str_replace(' ', '-', strtolower($this->str));
$stringy = self::create($this->str, $this->encoding);
return $this;
$stringy->str = preg_replace('/[^a-zA-Z\d -]/u', '', $stringy->standardize());
$stringy->str = $stringy->collapseWhitespace()->str;
$stringy->str = str_replace(' ', '-', strtolower($stringy->str));
return $stringy;
}
/**
@@ -533,9 +548,10 @@ class Stringy
*/
public function surround($substring)
{
$this->str = implode('', array($substring, $this->str, $substring));
$stringy = self::create($this->str, $this->encoding);
$stringy->str = implode('', array($substring, $stringy->str, $substring));
return $this;
return $stringy;
}
/**
@@ -547,15 +563,16 @@ class Stringy
*/
public function insert($substring, $index)
{
if ($index > $this->length())
return $this;
$stringy = self::create($this->str, $this->encoding);
if ($index > $stringy->length())
return $stringy;
$start = mb_substr($this->str, 0, $index, $this->encoding);
$end = mb_substr($this->str, $index, $this->length(), $this->encoding);
$start = mb_substr($stringy->str, 0, $index, $stringy->encoding);
$end = mb_substr($stringy->str, $index, $stringy->length(), $stringy->encoding);
$this->str = $start . $substring . $end;
$stringy->str = $start . $substring . $end;
return $this;
return $stringy;
}
/**
@@ -569,17 +586,18 @@ class Stringy
*/
public function truncate($length, $substring = '')
{
if ($length >= $this->length())
return $this;
$stringy = self::create($this->str, $this->encoding);
if ($length >= $stringy->length())
return $stringy;
// Need to further trim the string so we can append the substring
$substringLength = mb_strlen($substring, $this->encoding);
$substringLength = mb_strlen($substring, $stringy->encoding);
$length = $length - $substringLength;
$truncated = mb_substr($this->str, 0, $length, $this->encoding);
$this->str = $truncated . $substring;
$truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding);
$stringy->str = $truncated . $substring;
return $this;
return $stringy;
}
/**
@@ -594,25 +612,26 @@ class Stringy
*/
public function safeTruncate($length, $substring = '')
{
if ($length >= $this->length())
return $this;
$stringy = self::create($this->str, $this->encoding);
if ($length >= $stringy->length())
return $stringy;
// Need to further trim the string so we can append the substring
$substringLength = mb_strlen($substring, $this->encoding);
$substringLength = mb_strlen($substring, $stringy->encoding);
$length = $length - $substringLength;
$truncated = mb_substr($this->str, 0, $length, $this->encoding);
$truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding);
// If the last word was truncated
if (mb_strpos($this->str, ' ', $length - 1, $this->encoding) != $length) {
if (mb_strpos($stringy->str, ' ', $length - 1, $stringy->encoding) != $length) {
// Find pos of the last occurence of a space, and get everything up until
$lastPos = mb_strrpos($truncated, ' ', 0, $this->encoding);
$truncated = mb_substr($truncated, 0, $lastPos, $this->encoding);
$lastPos = mb_strrpos($truncated, ' ', 0, $stringy->encoding);
$truncated = mb_substr($truncated, 0, $lastPos, $stringy->encoding);
}
$this->str = $truncated . $substring;
$stringy->str = $truncated . $substring;
return $this;
return $stringy;
}
/**
@@ -630,9 +649,7 @@ class Stringy
$reversed .= mb_substr($this->str, $i, 1, $this->encoding);
}
$this->str = $reversed;
return $this;
return self::create($reversed, $this->encoding);
}
/**
@@ -651,9 +668,7 @@ class Stringy
$shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding);
}
$this->str = $shuffledStr;
return $this;
return self::create($shuffledStr, $this->encoding);
}
/**
@@ -663,9 +678,7 @@ class Stringy
*/
public function trim()
{
$this->str = trim($this->str);
return $this;
return self::create(trim($this->str), $this->encoding);
}
/**
@@ -689,9 +702,7 @@ class Stringy
}
}
$this->str = $longestCommonPrefix;
return $this;
return self::create($longestCommonPrefix, $this->encoding);
}
/**
@@ -715,9 +726,7 @@ class Stringy
}
}
$this->str = $longestCommonSuffix;
return $this;
return self::create($longestCommonSuffix, $this->encoding);
}
/**
@@ -731,13 +740,14 @@ class Stringy
{
// Uses dynamic programming to solve
// http://en.wikipedia.org/wiki/Longest_common_substring_problem
$strLength = $this->length();
$otherLength = mb_strlen($otherStr, $this->encoding);
$stringy = self::create($this->str, $this->encoding);
$strLength = $stringy->length();
$otherLength = mb_strlen($otherStr, $stringy->encoding);
// Return if either string is empty
if ($strLength == 0 || $otherLength == 0) {
$this->str = '';
return $this;
$stringy->str = '';
return $stringy;
}
$len = 0;
@@ -746,8 +756,8 @@ class Stringy
for ($i = 1; $i <= $strLength; $i++){
for ($j = 1; $j <= $otherLength; $j++){
$strChar = mb_substr($this->str, $i - 1, 1, $this->encoding);
$otherChar = mb_substr($otherStr, $j - 1, 1, $this->encoding);
$strChar = mb_substr($stringy->str, $i - 1, 1, $stringy->encoding);
$otherChar = mb_substr($otherStr, $j - 1, 1, $stringy->encoding);
if ($strChar == $otherChar) {
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
@@ -761,9 +771,9 @@ class Stringy
}
}
$this->str = mb_substr($this->str, $end - $len, $len, $this->encoding);
$stringy->str = mb_substr($stringy->str, $end - $len, $len, $stringy->encoding);
return $this;
return $stringy;
}
/**
@@ -787,14 +797,17 @@ class Stringy
*/
public function substr($start, $length = null)
{
$stringy = self::create($this->str, $this->encoding);
if ($length === null) {
$this->str = mb_substr($this->str, $start, $this->length() - $start,
$this->encoding);
$stringy->str = mb_substr($stringy->str, $start,
$stringy->length() - $start, $this->encoding);
} else {
$this->str = mb_substr($this->str, $start, $length, $this->encoding);
$stringy->str = mb_substr($stringy->str, $start, $length,
$stringy->encoding);
}
return $this;
return $stringy;
}
/**
@@ -805,9 +818,7 @@ class Stringy
*/
public function at($index)
{
$this->substr($index, 1);
return $this;
return $this->substr($index, 1);
}
/**
@@ -818,13 +829,15 @@ class Stringy
*/
public function first($n)
{
$stringy = self::create($this->str, $this->encoding);
if ($n < 0) {
$this->str = '';
$stringy->str = '';
} else {
$this->substr(0, $n);
return $stringy->substr(0, $n);
}
return $this;
return $stringy;
}
/**
@@ -835,13 +848,15 @@ class Stringy
*/
public function last($n)
{
$stringy = self::create($this->str, $this->encoding);
if ($n <= 0) {
$this->str = '';
$stringy->str = '';
} else {
$this->substr(-$n);
return $stringy->substr(-$n);
}
return $this;
return $stringy;
}
/**
@@ -852,10 +867,12 @@ class Stringy
*/
public function ensureLeft($substring)
{
if (!$this->startsWith($substring))
$this->str = $substring . $this->str;
$stringy = self::create($this->str, $this->encoding);
return $this;
if (!$stringy->startsWith($substring))
$stringy->str = $substring . $stringy->str;
return $stringy;
}
/**
@@ -866,10 +883,12 @@ class Stringy
*/
public function ensureRight($substring)
{
if (!$this->endsWith($substring))
$this->str .= $substring;
$stringy = self::create($this->str, $this->encoding);
return $this;
if (!$stringy->endsWith($substring))
$stringy->str .= $substring;
return $stringy;
}
/**
@@ -880,12 +899,14 @@ class Stringy
*/
public function removeLeft($substring)
{
if ($this->startsWith($substring)) {
$substringLength = mb_strlen($substring, $this->encoding);
$this->substr($substringLength);
$stringy = self::create($this->str, $this->encoding);
if ($stringy->startsWith($substring)) {
$substringLength = mb_strlen($substring, $stringy->encoding);
return $stringy->substr($substringLength);
}
return $this;
return $stringy;
}
/**
@@ -896,12 +917,14 @@ class Stringy
*/
public function removeRight($substring)
{
if ($this->endsWith($substring)) {
$substringLength = mb_strlen($substring, $this->encoding);
$this->substr(0, $this->length() - $substringLength);
$stringy = self::create($this->str, $this->encoding);
if ($stringy->endsWith($substring)) {
$substringLength = mb_strlen($substring, $stringy->encoding);
return $stringy->substr(0, $stringy->length() - $substringLength);
}
return $this;
return $stringy;
}
/**

View File

@@ -21,8 +21,10 @@ class StringyTestCase extends CommonTest
*/
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->lowerCaseFirst();
$stringy = S::create($str, $encoding);
$result = $stringy->lowerCaseFirst();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -30,8 +32,10 @@ class StringyTestCase extends CommonTest
*/
public function testCamelize($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->camelize();
$stringy = S::create($str, $encoding);
$result = $stringy->camelize();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -39,8 +43,10 @@ class StringyTestCase extends CommonTest
*/
public function testUpperCamelize($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->upperCamelize();
$stringy = S::create($str, $encoding);
$result = $stringy->upperCamelize();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -48,8 +54,10 @@ class StringyTestCase extends CommonTest
*/
public function testDasherize($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->dasherize();
$stringy = S::create($str, $encoding);
$result = $stringy->dasherize();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -57,8 +65,10 @@ class StringyTestCase extends CommonTest
*/
public function testUnderscored($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->underscored();
$stringy = S::create($str, $encoding);
$result = $stringy->underscored();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -66,8 +76,10 @@ class StringyTestCase extends CommonTest
*/
public function testSwapCase($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->swapCase();
$stringy = S::create($str, $encoding);
$result = $stringy->swapCase();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -76,8 +88,10 @@ class StringyTestCase extends CommonTest
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
{
$result = S::create($str, $encoding)->titleize($ignore);
$stringy = S::create($str, $encoding);
$result = $stringy->titleize($ignore);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -85,8 +99,10 @@ class StringyTestCase extends CommonTest
*/
public function testHumanize($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->humanize();
$stringy = S::create($str, $encoding);
$result = $stringy->humanize();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -94,8 +110,10 @@ class StringyTestCase extends CommonTest
*/
public function testTidy($expected, $str)
{
$result = S::create($str)->tidy();
$stringy = S::create($str);
$result = $stringy->tidy();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -103,8 +121,10 @@ class StringyTestCase extends CommonTest
*/
public function testCollapseWhitespace($expected, $str)
{
$result = S::create($str)->collapseWhitespace();
$stringy = S::create($str);
$result = $stringy->collapseWhitespace();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -112,8 +132,10 @@ class StringyTestCase extends CommonTest
*/
public function testStandardize($expected, $str)
{
$result = S::create($str)->standardize();
$stringy = S::create($str);
$result = $stringy->standardize();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -122,8 +144,10 @@ class StringyTestCase extends CommonTest
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
{
$result = S::create($str, $encoding)->pad($length, $padStr, $padType);
$stringy = S::create($str, $encoding);
$result = $stringy->pad($length, $padStr, $padType);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -132,8 +156,10 @@ class StringyTestCase extends CommonTest
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::create($str, $encoding)->padLeft($length, $padStr);
$stringy = S::create($str, $encoding);
$result = $stringy->padLeft($length, $padStr);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -142,8 +168,10 @@ class StringyTestCase extends CommonTest
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::create($str, $encoding)->padRight($length, $padStr);
$stringy = S::create($str, $encoding);
$result = $stringy->padRight($length, $padStr);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -152,8 +180,10 @@ class StringyTestCase extends CommonTest
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::create($str, $encoding)->padBoth($length, $padStr);
$stringy = S::create($str, $encoding);
$result = $stringy->padBoth($length, $padStr);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -162,8 +192,10 @@ class StringyTestCase extends CommonTest
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::create($str, $encoding)->startsWith($substring, $caseSensitive);
$stringy = S::create($str, $encoding);
$result = $stringy->startsWith($substring, $caseSensitive);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -172,8 +204,10 @@ class StringyTestCase extends CommonTest
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::create($str, $encoding)->endsWith($substring, $caseSensitive);
$stringy = S::create($str, $encoding);
$result = $stringy->endsWith($substring, $caseSensitive);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -181,8 +215,10 @@ class StringyTestCase extends CommonTest
*/
public function testToSpaces($expected, $str, $tabLength = 4)
{
$result = S::create($str)->toSpaces($tabLength);
$stringy = S::create($str);
$result = $stringy->toSpaces($tabLength);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -190,8 +226,10 @@ class StringyTestCase extends CommonTest
*/
public function testToTabs($expected, $str, $tabLength = 4)
{
$result = S::create($str)->toTabs($tabLength);
$stringy = S::create($str);
$result = $stringy->toTabs($tabLength);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -199,8 +237,10 @@ class StringyTestCase extends CommonTest
*/
public function testSlugify($expected, $str)
{
$result = S::create($str)->slugify();
$stringy = S::create($str);
$result = $stringy->slugify();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -208,8 +248,10 @@ class StringyTestCase extends CommonTest
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
{
$result = S::create($haystack, $encoding)->contains($needle);
$stringy = S::create($haystack, $encoding);
$result = $stringy->contains($needle);
$this->assertEquals($expected, $result);
$this->assertEquals($haystack, $stringy);
}
/**
@@ -217,8 +259,10 @@ class StringyTestCase extends CommonTest
*/
public function testSurround($expected, $str, $substring)
{
$result = S::create($str)->surround($substring);
$stringy = S::create($str);
$result = $stringy->surround($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -227,8 +271,10 @@ class StringyTestCase extends CommonTest
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
{
$result = S::create($str, $encoding)->insert($substring, $index);
$stringy = S::create($str, $encoding);
$result = $stringy->insert($substring, $index);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -237,8 +283,10 @@ class StringyTestCase extends CommonTest
public function testTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$result = S::create($str, $encoding)->truncate($length, $substring);
$stringy = S::create($str, $encoding);
$result = $stringy->truncate($length, $substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -246,8 +294,10 @@ class StringyTestCase extends CommonTest
*/
public function testReverse($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->reverse();
$stringy = S::create($str, $encoding);
$result = $stringy->reverse();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -256,8 +306,10 @@ class StringyTestCase extends CommonTest
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$result = S::create($str, $encoding)->shuffle();
$stringy = S::create($str, $encoding);
$result = $stringy->shuffle();
$this->assertEquals(count_chars($str), count_chars($result));
$this->assertEquals($str, $stringy);
}
/**
@@ -265,8 +317,10 @@ class StringyTestCase extends CommonTest
*/
public function testTrim($expected, $str)
{
$result = S::create($str)->trim();
$stringy = S::create($str);
$result = $stringy->trim();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -275,9 +329,10 @@ class StringyTestCase extends CommonTest
public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null)
{
$result = S::create($str, $encoding)
->longestCommonPrefix($otherStr);
$stringy = S::create($str, $encoding);
$result = $stringy->longestCommonPrefix($otherStr);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -286,9 +341,10 @@ class StringyTestCase extends CommonTest
public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null)
{
$result = S::create($str, $encoding)
->longestCommonSubstring($otherStr);
$stringy = S::create($str, $encoding);
$result = $stringy->longestCommonSubstring($otherStr);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -296,8 +352,10 @@ class StringyTestCase extends CommonTest
*/
public function testLength($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->length();
$stringy = S::create($str, $encoding);
$result = $stringy->length();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -306,8 +364,10 @@ class StringyTestCase extends CommonTest
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
{
$result = S::create($str, $encoding)->substr($start, $length);
$stringy = S::create($str, $encoding);
$result = $stringy->substr($start, $length);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -315,8 +375,10 @@ class StringyTestCase extends CommonTest
*/
public function testAt($expected, $str, $index, $encoding = null)
{
$result = S::create($str, $encoding)->at($index);
$stringy = S::create($str, $encoding);
$result = $stringy->at($index);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -324,8 +386,10 @@ class StringyTestCase extends CommonTest
*/
public function testFirst($expected, $str, $n, $encoding = null)
{
$result = S::create($str, $encoding)->first($n);
$stringy = S::create($str, $encoding);
$result = $stringy->first($n);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -333,8 +397,10 @@ class StringyTestCase extends CommonTest
*/
public function testLast($expected, $str, $n, $encoding = null)
{
$result = S::create($str, $encoding)->last($n);
$stringy = S::create($str, $encoding);
$result = $stringy->last($n);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -342,8 +408,10 @@ class StringyTestCase extends CommonTest
*/
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->ensureLeft($substring);
$stringy = S::create($str, $encoding);
$result = $stringy->ensureLeft($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -351,8 +419,10 @@ class StringyTestCase extends CommonTest
*/
public function testEnsureRight($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->ensureRight($substring);
$stringy = S::create($str, $encoding);
$result = $stringy->ensureRight($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -360,8 +430,10 @@ class StringyTestCase extends CommonTest
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->removeLeft($substring);
$stringy = S::create($str, $encoding);
$result = $stringy->removeLeft($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -369,8 +441,10 @@ class StringyTestCase extends CommonTest
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->removeRight($substring);
$stringy = S::create($str, $encoding);
$result = $stringy->removeRight($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -378,8 +452,10 @@ class StringyTestCase extends CommonTest
*/
public function testIsAlpha($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->isAlpha();
$stringy = S::create($str, $encoding);
$result = $stringy->isAlpha();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -387,8 +463,10 @@ class StringyTestCase extends CommonTest
*/
public function testIsAlphanumeric($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->isAlphanumeric();
$stringy = S::create($str, $encoding);
$result = $stringy->isAlphanumeric();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -396,8 +474,10 @@ class StringyTestCase extends CommonTest
*/
public function testIsBlank($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->isBlank();
$stringy = S::create($str, $encoding);
$result = $stringy->isBlank();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -405,8 +485,10 @@ class StringyTestCase extends CommonTest
*/
public function testIsLowerCase($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->isLowerCase();
$stringy = S::create($str, $encoding);
$result = $stringy->isLowerCase();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
@@ -414,7 +496,9 @@ class StringyTestCase extends CommonTest
*/
public function testIsUpperCase($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->isUpperCase();
$stringy = S::create($str, $encoding);
$result = $stringy->isUpperCase();
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
}