1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-04 02:22:36 +02:00

20 Commits
1.8.0 ... 1.8.1

Author SHA1 Message Date
Daniel St. Jules
6a7b0391b2 1.8.1 2015-01-08 07:21:43 -08:00
Daniel St. Jules
d6fa00bb1c Conditionally load Stringy\create 2015-01-07 09:09:07 -08:00
Daniel St. Jules
d1452263bd Merge pull request #59 from usernam3/master
Typecast comparison in startsWith/endsWith
2015-01-06 21:42:16 -08:00
Stanislav Dobrovolskiy
9da55d5d4f type cast in startsWith && endsWith, test cases for this behavior 2015-01-07 06:06:53 +02:00
Daniel St. Jules
b515598227 Merge pull request #67 from vlakoff/regex
Remove some useless (and erroneous) regex capturing groups
2015-01-06 16:42:30 -08:00
Daniel St. Jules
28b65615d7 Merge pull request #66 from vlakoff/nbsp
Support for various Unicode spaces in toAscii()
2015-01-06 16:38:55 -08:00
vlakoff
7b88b4f3da Support for various Unicode spaces in toAscii()
* no-break space (U+00A0)
* thin space (U+2009)
* narrow no-break space (U+202F)
* ideographic space (U+3000)
2015-01-07 01:28:49 +01:00
vlakoff
70e5b9c38b Remove some useless (and erroneous) regex capturing groups 2015-01-07 01:24:40 +01:00
vlakoff
e0d50f0b8f Unit tests for no-break space in collapseWhitespace() and isBlank() 2015-01-07 01:10:30 +01:00
Daniel St. Jules
c5365f256f Merge pull request #64 from vlakoff/tests
Add missing tests in StringyTest
2015-01-06 13:42:20 -08:00
Daniel St. Jules
401af084c7 Merge pull request #63 from vlakoff/docblock
Proper Stringy\create() description
2015-01-05 16:37:27 -08:00
vlakoff
773477cdd8 Add missing tests in StringyTest
For consistency with StaticStringyTest.
2015-01-06 00:39:31 +01:00
vlakoff
26444e4429 Proper Stringy\create() description 2015-01-06 00:10:39 +01:00
Daniel St. Jules
f3d32c79b0 Merge pull request #62 from vlakoff/static
Replace self::create() with static::create()
2015-01-05 14:06:06 -08:00
vlakoff
98c1013623 Replace self::create() with static::create()
In case someone wants to extend create().
2015-01-05 23:02:06 +01:00
Daniel St. Jules
91d8da4741 Cleanup toAscii array alignment 2015-01-05 13:55:08 -08:00
Daniel St. Jules
49cdd5b1e6 Merge pull request #61 from vlakoff/toAscii
Add « ä Ä Ö Ü » in toAscii()
2015-01-05 13:38:30 -08:00
vlakoff
2fe42b46a1 Add « ä Ä Ö Ü » in toAscii()
To complete diareses on vowels.
2015-01-05 22:20:22 +01:00
Daniel St. Jules
9baa545f23 Merge pull request #60 from vlakoff/optimize
Optimize chars() by not repeatedly call length()
2015-01-05 12:08:58 -05:00
vlakoff
640fc5b26b Optimize chars() by not repeatedly call length() 2015-01-05 13:49:48 +01:00
5 changed files with 137 additions and 80 deletions

View File

@@ -1,3 +1,12 @@
### 1.8.1 (2015-01-08)
* Optimized chars()
* Added "ä Ä Ö Ü"" in toAscii()
* Added support for Unicode spaces in toAscii()
* Replaced instances of self::create() with static::create()
* Added missing test cases for safeTruncate() and longestCommonSuffix()
* Updated Stringy\create() to avoid collision when it already exists
### 1.8.0 (2015-01-03) ### 1.8.0 (2015-01-03)
* Listed ext-mbstring in composer.json * Listed ext-mbstring in composer.json

View File

@@ -2,16 +2,18 @@
namespace Stringy; namespace Stringy;
/** if (!function_exists('Stringy\create')) {
* Invokes Stringy::create and returns the generated Stringy object on success. /**
* * Creates a Stringy object and returns it on success.
* @param mixed $str Value to modify, after being cast to string *
* @param string $encoding The character encoding * @param mixed $str Value to modify, after being cast to string
* @return Stringy A Stringy object * @param string $encoding The character encoding
* @throws \InvalidArgumentException if an array or object without a * @return Stringy A Stringy object
* __toString method is passed as the first argument * @throws \InvalidArgumentException if an array or object without a
*/ * __toString method is passed as the first argument
function create($str, $encoding = null) */
{ function create($str, $encoding = null)
return new Stringy($str, $encoding); {
return new Stringy($str, $encoding);
}
} }

View File

@@ -186,7 +186,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
public function chars() public function chars()
{ {
$chars = array(); $chars = array();
for ($i = 0; $i < $this->length(); $i++) { for ($i = 0, $l = $this->length(); $i < $l; $i++) {
$chars[] = $this->at($i)->str; $chars[] = $this->at($i)->str;
} }
@@ -206,7 +206,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$str = mb_strtoupper($first, $this->encoding) . $rest; $str = mb_strtoupper($first, $this->encoding) . $rest;
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -222,7 +222,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$str = mb_strtolower($first, $this->encoding) . $rest; $str = mb_strtolower($first, $this->encoding) . $rest;
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -235,7 +235,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
public function camelize() public function camelize()
{ {
$encoding = $this->encoding; $encoding = $this->encoding;
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
$camelCase = preg_replace_callback( $camelCase = preg_replace_callback(
'/[-_\s]+(.)?/u', '/[-_\s]+(.)?/u',
@@ -311,7 +311,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
mb_regex_encoding($regexEncoding); mb_regex_encoding($regexEncoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -321,7 +321,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function swapCase() public function swapCase()
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
$encoding = $stringy->encoding; $encoding = $stringy->encoding;
$stringy->str = preg_replace_callback( $stringy->str = preg_replace_callback(
@@ -378,7 +378,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = str_replace(array('_id', '_'), array('', ' '), $this->str); $str = str_replace(array('_id', '_'), array('', ' '), $this->str);
return self::create($str, $this->encoding)->trim()->upperCaseFirst(); return static::create($str, $this->encoding)->trim()->upperCaseFirst();
} }
/** /**
@@ -402,7 +402,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
'-', '-',
), $this->str); ), $this->str);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -427,33 +427,37 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = $this->str; $str = $this->str;
$charsArray = array( $charsArray = array(
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ', 'a' => array('à', 'á', 'â', 'ä', 'ã', 'ā', 'ą', 'ă', 'å', 'α',
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ',
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ',
'ᾶ', 'ᾷ', 'а'), 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а'),
'b' => array('б', 'β'), 'b' => array('б', 'β'),
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'), 'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'), 'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
'e' => array('è', 'é', 'ê', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'д'),
'ἐ', 'ἑ', '', '', '', '', '', 'έ', 'е', 'ё', 'э', 'є'), 'e' => array('è', 'é', 'ê', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε',
'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е',
'ё', 'э', 'є'),
'f' => array('ф'), 'f' => array('ф'),
'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ'), 'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ'),
'h' => array('ĥ', 'ħ'), 'h' => array('ĥ', 'ħ'),
'i' => array('ì', 'í', 'î', 'ï', 'ī', 'ĩ', 'ĭ', 'į', 'ı', 'ι', 'ί', 'i' => array('ì', 'í', 'î', 'ï', 'ī', 'ĩ', 'ĭ', 'į', 'ı', 'ι',
'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ',
'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и'), 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і',
'ї', 'и'),
'j' => array('ĵ'), 'j' => array('ĵ'),
'k' => array('ķ', 'ĸ', 'к'), 'k' => array('ķ', 'ĸ', 'к'),
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л'), 'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л'),
'm' => array('м'), 'm' => array('м'),
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н'), 'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н'),
'o' => array('ò', 'ó', 'ô', 'õ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ό', 'ὀ', 'o' => array('ò', 'ó', 'ô', 'õ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ό',
'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'ö', 'о'), 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'ö', 'о'),
'p' => array('п'), 'p' => array('п'),
'r' => array('ŕ', 'ř', 'ŗ', 'р'), 'r' => array('ŕ', 'ř', 'ŗ', 'р'),
's' => array('ś', 'š', 'ş', 'с'), 's' => array('ś', 'š', 'ş', 'с'),
't' => array('ť', 'ţ', 'т'), 't' => array('ť', 'ţ', 'т'),
'u' => array('ü', 'ù', 'ú', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ũ', 'ų', 'µ', 'у'), 'u' => array('ü', 'ù', 'ú', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ũ', 'ų',
'µ', 'у'),
'v' => array('в'), 'v' => array('в'),
'w' => array('ŵ'), 'w' => array('ŵ'),
'y' => array('ÿ', 'ý', 'ŷ', 'й', 'ы'), 'y' => array('ÿ', 'ý', 'ŷ', 'й', 'ы'),
@@ -467,30 +471,33 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
'ya' => array('я'), 'ya' => array('я'),
'yu' => array('ю'), 'yu' => array('ю'),
'zh' => array('ж'), 'zh' => array('ж'),
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'A' => array('Á', 'Â', 'Ä', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά',
'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ',
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'), 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά',
'ᾼ', 'А'),
'B' => array('Б'), 'B' => array('Б'),
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'), 'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'), 'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'),
'E' => array('É', 'Ê', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'E' => array('É', 'Ê', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ',
'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', 'Э', 'Є'), 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё',
'Э', 'Є'),
'F' => array('Ф'), 'F' => array('Ф'),
'G' => array('Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ'), 'G' => array('Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ'),
'I' => array('Í', 'Î', 'Ï', 'Ī', 'Ĩ', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ', 'I' => array('Í', 'Î', 'Ï', 'Ī', 'Ĩ', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί',
'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ',
'И', 'І', 'Ї'), 'Ὶ', 'Ί', 'И', 'І', 'Ї'),
'K' => array('К'), 'K' => array('К'),
'L' => array('Ĺ', 'Ł', 'Л'), 'L' => array('Ĺ', 'Ł', 'Л'),
'M' => array('М'), 'M' => array('М'),
'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н'), 'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н'),
'O' => array('Ó', 'Ô', 'Õ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'O' => array('Ó', 'Ô', 'Ö', 'Õ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό',
'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О'), 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О'),
'P' => array('П'), 'P' => array('П'),
'R' => array('Ř', 'Ŕ', 'Р'), 'R' => array('Ř', 'Ŕ', 'Р'),
'S' => array('Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С'), 'S' => array('Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С'),
'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț', 'Т'), 'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț', 'Т'),
'U' => array('Ù', 'Ú', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ũ', 'Ų', 'У'), 'U' => array('Ù', 'Ú', 'Û', 'Ü', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ũ', 'Ų',
'У'),
'V' => array('В'), 'V' => array('В'),
'Y' => array('Ý', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й'), 'Y' => array('Ý', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й'),
'Z' => array('Ź', 'Ž', 'Ż', 'З'), 'Z' => array('Ź', 'Ž', 'Ż', 'З'),
@@ -501,7 +508,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
'TS' => array('Ц'), 'TS' => array('Ц'),
'YA' => array('Я'), 'YA' => array('Я'),
'YU' => array('Ю'), 'YU' => array('Ю'),
'ZH' => array('Ж') 'ZH' => array('Ж'),
' ' => array("\xC2\xA0", "\xE2\x80\x89", "\xE2\x80\xAF",
"\xE3\x80\x80"),
); );
foreach ($charsArray as $key => $value) { foreach ($charsArray as $key => $value) {
@@ -510,7 +519,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$str = preg_replace('/[^\x20-\x7E]/u', '', $str); $str = preg_replace('/[^\x20-\x7E]/u', '', $str);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -597,7 +606,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
private function applyPadding($left = 0, $right = 0, $padStr = ' ') private function applyPadding($left = 0, $right = 0, $padStr = ' ')
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
$length = mb_strlen($padStr, $stringy->encoding); $length = mb_strlen($padStr, $stringy->encoding);
$strLength = $stringy->length(); $strLength = $stringy->length();
@@ -637,7 +646,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$startOfStr = mb_strtolower($startOfStr, $this->encoding); $startOfStr = mb_strtolower($startOfStr, $this->encoding);
} }
return $substring === $startOfStr; return (string) $substring === $startOfStr;
} }
/** /**
@@ -662,7 +671,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$endOfStr = mb_strtolower($endOfStr, $this->encoding); $endOfStr = mb_strtolower($endOfStr, $this->encoding);
} }
return $substring === $endOfStr; return (string) $substring === $endOfStr;
} }
/** /**
@@ -677,7 +686,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$spaces = str_repeat(' ', $tabLength); $spaces = str_repeat(' ', $tabLength);
$str = str_replace("\t", $spaces, $this->str); $str = str_replace("\t", $spaces, $this->str);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -693,7 +702,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$spaces = str_repeat(' ', $tabLength); $spaces = str_repeat(' ', $tabLength);
$str = str_replace($spaces, "\t", $this->str); $str = str_replace($spaces, "\t", $this->str);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -705,7 +714,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding); $str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -718,7 +727,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = mb_strtolower($this->str, $this->encoding); $str = mb_strtolower($this->str, $this->encoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -731,7 +740,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = mb_strtoupper($this->str, $this->encoding); $str = mb_strtoupper($this->str, $this->encoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -746,7 +755,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function slugify($replacement = '-') public function slugify($replacement = '-')
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
$quotedReplacement = preg_quote($replacement); $quotedReplacement = preg_quote($replacement);
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u"; $pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
@@ -834,7 +843,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{ {
$str = implode('', array($substring, $this->str, $substring)); $str = implode('', array($substring, $this->str, $substring));
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -846,7 +855,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function insert($substring, $index) public function insert($substring, $index)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($index > $stringy->length()) { if ($index > $stringy->length()) {
return $stringy; return $stringy;
} }
@@ -871,7 +880,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function truncate($length, $substring = '') public function truncate($length, $substring = '')
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($length >= $stringy->length()) { if ($length >= $stringy->length()) {
return $stringy; return $stringy;
} }
@@ -898,7 +907,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function safeTruncate($length, $substring = '') public function safeTruncate($length, $substring = '')
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($length >= $stringy->length()) { if ($length >= $stringy->length()) {
return $stringy; return $stringy;
} }
@@ -937,7 +946,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$reversed .= mb_substr($this->str, $i, 1, $this->encoding); $reversed .= mb_substr($this->str, $i, 1, $this->encoding);
} }
return self::create($reversed, $this->encoding); return static::create($reversed, $this->encoding);
} }
/** /**
@@ -956,7 +965,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding); $shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding);
} }
return self::create($shuffledStr, $this->encoding); return static::create($shuffledStr, $this->encoding);
} }
/** /**
@@ -966,7 +975,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function trim() public function trim()
{ {
return self::create(trim($this->str), $this->encoding); return static::create(trim($this->str), $this->encoding);
} }
/** /**
@@ -991,7 +1000,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
} }
} }
return self::create($longestCommonPrefix, $encoding); return static::create($longestCommonPrefix, $encoding);
} }
/** /**
@@ -1016,7 +1025,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
} }
} }
return self::create($longestCommonSuffix, $encoding); return static::create($longestCommonSuffix, $encoding);
} }
/** /**
@@ -1031,7 +1040,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
// Uses dynamic programming to solve // Uses dynamic programming to solve
// http://en.wikipedia.org/wiki/Longest_common_substring_problem // http://en.wikipedia.org/wiki/Longest_common_substring_problem
$encoding = $this->encoding; $encoding = $this->encoding;
$stringy = self::create($this->str, $encoding); $stringy = static::create($this->str, $encoding);
$strLength = $stringy->length(); $strLength = $stringy->length();
$otherLength = mb_strlen($otherStr, $encoding); $otherLength = mb_strlen($otherStr, $encoding);
@@ -1091,7 +1100,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$length = $length === null ? $this->length() : $length; $length = $length === null ? $this->length() : $length;
$str = mb_substr($this->str, $start, $length, $this->encoding); $str = mb_substr($this->str, $start, $length, $this->encoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
/** /**
@@ -1113,7 +1122,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function first($n) public function first($n)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($n < 0) { if ($n < 0) {
$stringy->str = ''; $stringy->str = '';
@@ -1132,7 +1141,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function last($n) public function last($n)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($n <= 0) { if ($n <= 0) {
$stringy->str = ''; $stringy->str = '';
@@ -1152,7 +1161,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function ensureLeft($substring) public function ensureLeft($substring)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if (!$stringy->startsWith($substring)) { if (!$stringy->startsWith($substring)) {
$stringy->str = $substring . $stringy->str; $stringy->str = $substring . $stringy->str;
@@ -1170,7 +1179,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function ensureRight($substring) public function ensureRight($substring)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if (!$stringy->endsWith($substring)) { if (!$stringy->endsWith($substring)) {
$stringy->str .= $substring; $stringy->str .= $substring;
@@ -1187,7 +1196,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function removeLeft($substring) public function removeLeft($substring)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($stringy->startsWith($substring)) { if ($stringy->startsWith($substring)) {
$substringLength = mb_strlen($substring, $stringy->encoding); $substringLength = mb_strlen($substring, $stringy->encoding);
@@ -1205,7 +1214,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function removeRight($substring) public function removeRight($substring)
{ {
$stringy = self::create($this->str, $this->encoding); $stringy = static::create($this->str, $this->encoding);
if ($stringy->endsWith($substring)) { if ($stringy->endsWith($substring)) {
$substringLength = mb_strlen($substring, $stringy->encoding); $substringLength = mb_strlen($substring, $stringy->encoding);
@@ -1240,7 +1249,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isAlpha() public function isAlpha()
{ {
return $this->matchesPattern('^([[:alpha:]])*$'); return $this->matchesPattern('^[[:alpha:]]*$');
} }
/** /**
@@ -1251,7 +1260,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isAlphanumeric() public function isAlphanumeric()
{ {
return $this->matchesPattern('^([[:alnum:]])*$'); return $this->matchesPattern('^[[:alnum:]]*$');
} }
/** /**
@@ -1262,7 +1271,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isHexadecimal() public function isHexadecimal()
{ {
return $this->matchesPattern('^([[:xdigit:]])*$'); return $this->matchesPattern('^[[:xdigit:]]*$');
} }
/** /**
@@ -1273,7 +1282,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isBlank() public function isBlank()
{ {
return $this->matchesPattern('^([[:space:]])*$'); return $this->matchesPattern('^[[:space:]]*$');
} }
/** /**
@@ -1296,7 +1305,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isLowerCase() public function isLowerCase()
{ {
return $this->matchesPattern('^([[:lower:]])*$'); return $this->matchesPattern('^[[:lower:]]*$');
} }
/** /**
@@ -1307,7 +1316,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
*/ */
public function isUpperCase() public function isUpperCase()
{ {
return $this->matchesPattern('^([[:upper:]])*$'); return $this->matchesPattern('^[[:upper:]]*$');
} }
/** /**
@@ -1372,6 +1381,6 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
$str = mb_ereg_replace($pattern, $replacement, $this->str, $options); $str = mb_ereg_replace($pattern, $replacement, $this->str, $options);
mb_regex_encoding($regexEncoding); mb_regex_encoding($regexEncoding);
return self::create($str, $this->encoding); return static::create($str, $this->encoding);
} }
} }

View File

@@ -1,5 +1,7 @@
<?php <?php
use Stringy\Stringy;
abstract class CommonTest extends PHPUnit_Framework_TestCase abstract class CommonTest extends PHPUnit_Framework_TestCase
{ {
/** /**
@@ -172,6 +174,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('test string', 'test string'), array('test string', 'test string'),
array('Ο συγγραφέας', ' Ο συγγραφέας '), array('Ο συγγραφέας', ' Ο συγγραφέας '),
array('123', ' 123 '), array('123', ' 123 '),
array('', ' ', 'UTF-8'), // no-break space
array('1 2 3', '  1  2  3  ', 'UTF-8'), // ideographic spaces array('1 2 3', '  1  2  3  ', 'UTF-8'), // ideographic spaces
array('', ' ', 'UTF-8'), // thin space and space array('', ' ', 'UTF-8'), // thin space and space
array('', ' '), array('', ' '),
@@ -188,7 +191,10 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('perevirka', 'перевірка'), array('perevirka', 'перевірка'),
array('lysaya gora', 'лысая гора'), array('lysaya gora', 'лысая гора'),
array('shchuka', 'щука'), array('shchuka', 'щука'),
array('', '漢字') array('', '漢字'),
array(' ', ' '), // no-break space
array(' 1 2 3 ', '  1  2  3  '), // ideographic spaces
array(' ', ' '), // thin space and space
); );
} }
@@ -752,6 +758,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('bar', 'foo bar', 'foo '), array('bar', 'foo bar', 'foo '),
array('foo bar', 'foo bar', 'oo'), array('foo bar', 'foo bar', 'oo'),
array('foo bar', 'foo bar', 'oo bar'), array('foo bar', 'foo bar', 'oo bar'),
array('oo bar', 'foo bar', Stringy::create('foo bar')->first(1), 'UTF-8'),
array('oo bar', 'foo bar', Stringy::create('foo bar')->at(0), 'UTF-8'),
array('fòô bàř', 'fòô bàř', '', 'UTF-8'), array('fòô bàř', 'fòô bàř', '', 'UTF-8'),
array('òô bàř', 'fòô bàř', 'f', 'UTF-8'), array('òô bàř', 'fòô bàř', 'f', 'UTF-8'),
array('bàř', 'fòô bàř', 'fòô ', 'UTF-8'), array('bàř', 'fòô bàř', 'fòô ', 'UTF-8'),
@@ -768,6 +776,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array('foo', 'foo bar', ' bar'), array('foo', 'foo bar', ' bar'),
array('foo bar', 'foo bar', 'ba'), array('foo bar', 'foo bar', 'ba'),
array('foo bar', 'foo bar', 'foo ba'), array('foo bar', 'foo bar', 'foo ba'),
array('foo ba', 'foo bar', Stringy::create('foo bar')->last(1), 'UTF-8'),
array('foo ba', 'foo bar', Stringy::create('foo bar')->at(6), 'UTF-8'),
array('fòô bàř', 'fòô bàř', '', 'UTF-8'), array('fòô bàř', 'fòô bàř', '', 'UTF-8'),
array('fòô bà', 'fòô bàř', 'ř', 'UTF-8'), array('fòô bà', 'fòô bàř', 'ř', 'UTF-8'),
array('fòô', 'fòô bàř', ' bàř', 'UTF-8'), array('fòô', 'fòô bàř', ' bàř', 'UTF-8'),
@@ -822,6 +832,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
array(false, "\n\t ' \v\f"), array(false, "\n\t ' \v\f"),
array(false, "\n\t 2 \v\f"), array(false, "\n\t 2 \v\f"),
array(true, '', 'UTF-8'), array(true, '', 'UTF-8'),
array(true, ' ', 'UTF-8'), // no-break space
array(true, ' ', 'UTF-8'), // thin space array(true, ' ', 'UTF-8'), // thin space
array(true, '  ', 'UTF-8'), // ideographic spaces array(true, '  ', 'UTF-8'), // ideographic spaces
array(false, ' z', 'UTF-8'), array(false, ' z', 'UTF-8'),

View File

@@ -545,6 +545,19 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy); $this->assertEquals($str, $stringy);
} }
/**
* @dataProvider safeTruncateProvider()
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->safeTruncate($length, $substring);
$this->assertStringy($result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/** /**
* @dataProvider reverseProvider() * @dataProvider reverseProvider()
*/ */
@@ -605,6 +618,19 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy); $this->assertEquals($str, $stringy);
} }
/**
* @dataProvider longestCommonSuffixProvider()
*/
public function testLongestCommonSuffix($expected, $str, $otherStr,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->longestCommonSuffix($otherStr);
$this->assertStringy($result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/** /**
* @dataProvider longestCommonSubstringProvider() * @dataProvider longestCommonSubstringProvider()
*/ */