mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-31 16:51:57 +02:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4749c205db | ||
|
fe3368bd8b | ||
|
7763df3c3b | ||
|
397d2ca2f8 | ||
|
2c295fd15d | ||
|
9a5601efef | ||
|
e42f165471 | ||
|
1a2e771ff8 | ||
|
f9eab1b657 | ||
|
f9729788c4 | ||
|
f1009d0267 | ||
|
9d1073f4f5 | ||
|
c2a554253c |
@@ -1,3 +1,12 @@
|
||||
### 1.10.0 (2015-07-22)
|
||||
|
||||
* Added trimLeft, trimRight
|
||||
* Added support for unicode whitespace to trim
|
||||
* Added delimit
|
||||
* Added indexOf and indexOfLast
|
||||
* Added htmlEncode and htmlDecode
|
||||
* Added "Ç" in toAscii()
|
||||
|
||||
### 1.9.0 (2015-02-09)
|
||||
|
||||
* Added hasUpperCase and hasLowerCase
|
||||
|
125
README.md
125
README.md
@@ -21,6 +21,7 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||
* [countSubstr](#countsubstr)
|
||||
* [create](#create)
|
||||
* [dasherize](#dasherize)
|
||||
* [delimit](#delimit)
|
||||
* [endsWith](#endswith)
|
||||
* [ensureLeft](#ensureleft)
|
||||
* [ensureRight](#ensureright)
|
||||
@@ -28,7 +29,11 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||
* [getEncoding](#getencoding)
|
||||
* [hasLowerCase](#haslowercase)
|
||||
* [hasUpperCase](#hasuppercase)
|
||||
* [htmlDecode](#htmldecode)
|
||||
* [htmlEncode](#htmlencode)
|
||||
* [humanize](#humanize)
|
||||
* [indexOf](#indexof)
|
||||
* [indexOfLast](#indexoflast)
|
||||
* [insert](#insert)
|
||||
* [isAlpha](#isalpha)
|
||||
* [isAlphanumeric](#isalphanumeric)
|
||||
@@ -69,6 +74,8 @@ PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||
* [toTitleCase](#totitlecase)
|
||||
* [toUpperCase](#touppercase)
|
||||
* [trim](#trim)
|
||||
* [trimLeft](#trimLeft)
|
||||
* [trimRight](#trimRight)
|
||||
* [truncate](#truncate)
|
||||
* [underscored](#underscored)
|
||||
* [upperCamelize](#uppercamelize)
|
||||
@@ -85,7 +92,7 @@ in your composer.json file:
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"danielstjules/stringy": "~1.9"
|
||||
"danielstjules/stringy": "~1.10"
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -343,6 +350,22 @@ S::create('TestDCase')->dasherize();
|
||||
S::dasherize('TestDCase'); // 'test-d-case'
|
||||
```
|
||||
|
||||
#### delimit
|
||||
|
||||
$stringy->delimit($delimiter);
|
||||
|
||||
S::delimit(string $str [, string $delimiter, string $encoding ])
|
||||
|
||||
Returns a lowercase and trimmed string separated by the given delimiter.
|
||||
Delimiters are inserted before uppercase characters (with the exception
|
||||
of the first character of the string), and in place of spaces, dashes,
|
||||
and underscores. Alpha delimiters are not converted to lowercase.
|
||||
|
||||
```php
|
||||
S::create('TestDCase')->delimit('>>');
|
||||
S::delimit('TestCase', '>>'); // 'test>>case'
|
||||
```
|
||||
|
||||
#### endsWith
|
||||
|
||||
$stringy->endsWith(string $substring [, boolean $caseSensitive = true ])
|
||||
@@ -433,6 +456,32 @@ S::create('fòô bàř', 'UTF-8')->hasUpperCase();
|
||||
S::hasUpperCase('fòô bàř', 'UTF-8'); // false
|
||||
```
|
||||
|
||||
#### htmlDecode
|
||||
|
||||
$stringy->htmlDecode()
|
||||
|
||||
S::htmlDecode(string $str [, int $flags, string $encoding ])
|
||||
|
||||
Convert all HTML entities to their applicable characters.
|
||||
|
||||
```php
|
||||
S::create('&')->htmlDecode();
|
||||
S::htmlDecode('&'); // '&'
|
||||
```
|
||||
|
||||
#### htmlEncode
|
||||
|
||||
$stringy->htmlEncode()
|
||||
|
||||
S::htmlEncode(string $str [, int $flags, string $encoding ])
|
||||
|
||||
Convert all applicable characters to HTML entities.
|
||||
|
||||
```php
|
||||
S::create('&')->htmlEncode();
|
||||
S::htmlEncode('&'); // '&'
|
||||
```
|
||||
|
||||
#### humanize
|
||||
|
||||
$stringy->humanize()
|
||||
@@ -447,6 +496,36 @@ S::create('author_id')->humanize();
|
||||
S::humanize('author_id'); // 'Author'
|
||||
```
|
||||
|
||||
#### indexOf
|
||||
|
||||
$stringy->indexOf(string $needle [, $offset = 0 ]);
|
||||
|
||||
S::indexOf(string $haystack , string $needle [, $offset = 0 [, $encoding = null ]])
|
||||
|
||||
Returns the index of the first occurrence of $needle in the string,
|
||||
and false if not found. Accepts an optional offset from which to begin
|
||||
the search.
|
||||
|
||||
```php
|
||||
S::create('string', 'UTF-8')->indexOf('ing');
|
||||
S::indexOf('string', 'ing'); // 3
|
||||
```
|
||||
|
||||
#### indexOfLast
|
||||
|
||||
$stringy->indexOfLast(string $needle [, $offset = 0 ]);
|
||||
|
||||
S::indexOfLast(string $haystack , string $needle [, $offset = 0 [, $encoding = null ]])
|
||||
|
||||
Returns the index of the last occurrence of $needle in the string,
|
||||
and false if not found. Accepts an optional offset from which to begin
|
||||
the search.
|
||||
|
||||
```php
|
||||
S::create('string', 'UTF-8')->indexOfLast('ing');
|
||||
S::indexOfLast('string string', 'ing'); // 10
|
||||
```
|
||||
|
||||
#### insert
|
||||
|
||||
$stringy->insert(int $index, string $substring)
|
||||
@@ -992,15 +1071,47 @@ S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ'
|
||||
|
||||
#### trim
|
||||
|
||||
$stringy->trim()
|
||||
$stringy->trim([, string $chars])
|
||||
|
||||
S::trim(string $str)
|
||||
S::trim(string $str [, string $chars [, string $encoding ]])
|
||||
|
||||
Returns the trimmed string. An alias for PHP's trim() function.
|
||||
Returns a string with whitespace removed from the start and end of the
|
||||
string. Supports the removal of unicode whitespace. Accepts an optional
|
||||
string of characters to strip instead of the defaults.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->trim();
|
||||
S::trim(' fòô bàř '); // 'fòô bàř'
|
||||
S::create(' fòô bàř ', 'UTF-8')->trim();
|
||||
S::trim(' fòô bàř '); // 'fòô bàř'
|
||||
```
|
||||
|
||||
#### trimLeft
|
||||
|
||||
$stringy->trimLeft([, string $chars])
|
||||
|
||||
S::trimLeft(string $str [, string $chars [, string $encoding ]])
|
||||
|
||||
Returns a string with whitespace removed from the start of the string.
|
||||
Supports the removal of unicode whitespace. Accepts an optional
|
||||
string of characters to strip instead of the defaults.
|
||||
|
||||
```php
|
||||
S::create(' fòô bàř ', 'UTF-8')->trimLeft();
|
||||
S::trimLeft(' fòô bàř '); // 'fòô bàř '
|
||||
```
|
||||
|
||||
#### trimRight
|
||||
|
||||
$stringy->trimRight([, string $chars])
|
||||
|
||||
S::trimRight(string $str [, string $chars [, string $encoding ]])
|
||||
|
||||
Returns a string with whitespace removed from the end of the string.
|
||||
Supports the removal of unicode whitespace. Accepts an optional
|
||||
string of characters to strip instead of the defaults.
|
||||
|
||||
```php
|
||||
S::create(' fòô bàř ', 'UTF-8')->trimRight();
|
||||
S::trimRight(' fòô bàř '); // ' fòô bàř'
|
||||
```
|
||||
|
||||
#### truncate
|
||||
@@ -1067,6 +1178,8 @@ The following is a list of libraries that extend Stringy:
|
||||
|
||||
* [SliceableStringy](https://github.com/danielstjules/SliceableStringy):
|
||||
Python-like string slices in PHP
|
||||
* [SubStringy](https://github.com/TCB13/SubStringy):
|
||||
Advanced substring methods
|
||||
|
||||
## Tests
|
||||
|
||||
|
@@ -97,6 +97,22 @@ class StaticStringy
|
||||
return (string) Stringy::create($str, $encoding)->underscored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string separated by the given delimiter.
|
||||
* Delimiters are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces, dashes,
|
||||
* and underscores. Alpha delimiters are not converted to lowercase.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $delimiter Sequence used to separate parts of the string
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with delimiter
|
||||
*/
|
||||
public static function delimit($str, $delimiter, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->delimit($delimiter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of the string.
|
||||
*
|
||||
@@ -421,6 +437,40 @@ class StaticStringy
|
||||
->containsAll($needles, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first occurrence of $needle in the string,
|
||||
* and false if not found. Accepts an optional offset from which to begin
|
||||
* the search.
|
||||
*
|
||||
* @param string $haystack String to search
|
||||
* @param string $needle Substring to look for
|
||||
* @param int $offset Offset from which to search
|
||||
* @return int|bool The occurrence's index if found, otherwise false
|
||||
*/
|
||||
public static function indexOf($haystack, $needle, $offset = 0,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->indexOf($needle, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the last occurrence of $needle in the string,
|
||||
* and false if not found. Accepts an optional offset from which to begin
|
||||
* the search.
|
||||
*
|
||||
* @param string $haystack String to search
|
||||
* @param string $needle Substring to look for
|
||||
* @param int $offset Offset from which to search
|
||||
* @return int|bool The last occurrence's index if found, otherwise false
|
||||
*/
|
||||
public static function indexOfLast($haystack, $needle, $offset = 0,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->indexOfLast($needle, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
@@ -511,14 +561,48 @@ class StaticStringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trimmed string. An alias for PHP's trim() function.
|
||||
* Returns a string with whitespace removed from the start and end of the
|
||||
* string. Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @param string $str String to trim
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trim($str)
|
||||
public static function trim($str, $chars = null, $encoding = null)
|
||||
{
|
||||
return trim($str);
|
||||
return (string) Stringy::create($str, $encoding)->trim($chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with whitespace removed from the start of the string.
|
||||
* Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trimLeft($str, $chars = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->trimLeft($chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with whitespace removed from the end of the string.
|
||||
* Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trimRight($str, $chars = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->trimRight($chars);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -866,4 +950,30 @@ class StaticStringy
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->regexReplace($pattern, $replacement, $options, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert all applicable characters to HTML entities.
|
||||
*
|
||||
* @param string $str The string to encode.
|
||||
* @param int|null $flags See http://php.net/manual/en/function.htmlentities.php
|
||||
* @param string $encoding The character encoding
|
||||
* @return Stringy Object with the resulting $str after being html encoded.
|
||||
*/
|
||||
public static function htmlEncode($str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->htmlEncode($flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert all HTML entities to their applicable characters.
|
||||
*
|
||||
* @param string $str The string to decode.
|
||||
* @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php
|
||||
* @param string $encoding The character encoding
|
||||
* @return Stringy Object with the resulting $str after being html decoded.
|
||||
*/
|
||||
public static function htmlDecode($str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->htmlDecode($flags);
|
||||
}
|
||||
}
|
||||
|
114
src/Stringy.php
114
src/Stringy.php
@@ -277,7 +277,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
*/
|
||||
public function dasherize()
|
||||
{
|
||||
return $this->applyDelimiter('-');
|
||||
return $this->delimit('-');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -290,24 +290,27 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
*/
|
||||
public function underscored()
|
||||
{
|
||||
return $this->applyDelimiter('_');
|
||||
return $this->delimit('_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string separated by the given delimiter.
|
||||
* Delimiters are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces, dashes,
|
||||
* and underscores. Alpha delimiters are not converted to lowercase.
|
||||
*
|
||||
* @param string $delimiter Sequence used to separate parts of the string
|
||||
* @return Stringy Object with a delimited $str
|
||||
*/
|
||||
protected function applyDelimiter($delimiter)
|
||||
public function delimit($delimiter)
|
||||
{
|
||||
// Save current regex encoding so we can reset it after
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$str = mb_ereg_replace('\B([A-Z])', $delimiter .'\1', $this->trim());
|
||||
$str = mb_ereg_replace('[-_\s]+', $delimiter, $str);
|
||||
$str = mb_ereg_replace('\B([A-Z])', '-\1', $this->trim());
|
||||
$str = mb_strtolower($str, $this->encoding);
|
||||
$str = mb_ereg_replace('[-_\s]+', $delimiter, $str);
|
||||
|
||||
mb_regex_encoding($regexEncoding);
|
||||
|
||||
@@ -520,7 +523,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ',
|
||||
'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
|
||||
'B' => array('Б', 'Β'),
|
||||
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
|
||||
'C' => array('Ç','Ć', 'Č', 'Ĉ', 'Ċ'),
|
||||
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'),
|
||||
'E' => array('É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ',
|
||||
'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ',
|
||||
@@ -815,7 +818,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
|
||||
$stringy->str = preg_replace($pattern, '', $stringy);
|
||||
|
||||
return $stringy->toLowerCase()->applyDelimiter($replacement)
|
||||
return $stringy->toLowerCase()->delimit($replacement)
|
||||
->removeLeft($replacement)->removeRight($replacement);
|
||||
}
|
||||
|
||||
@@ -887,6 +890,36 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the first occurrence of $needle in the string,
|
||||
* and false if not found. Accepts an optional offset from which to begin
|
||||
* the search.
|
||||
*
|
||||
* @param string $needle Substring to look for
|
||||
* @param int $offset Offset from which to search
|
||||
* @return int|bool The occurrence's index if found, otherwise false
|
||||
*/
|
||||
public function indexOf($needle, $offset = 0)
|
||||
{
|
||||
return mb_strpos($this->str, (string) $needle,
|
||||
(int) $offset, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the last occurrence of $needle in the string,
|
||||
* and false if not found. Accepts an optional offset from which to begin
|
||||
* the search.
|
||||
*
|
||||
* @param string $needle Substring to look for
|
||||
* @param int $offset Offset from which to search
|
||||
* @return int|bool The last occurrence's index if found, otherwise false
|
||||
*/
|
||||
public function indexOfLast($needle, $offset = 0)
|
||||
{
|
||||
return mb_strrpos($this->str, (string) $needle,
|
||||
(int) $offset, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds $str with the given substring.
|
||||
*
|
||||
@@ -1024,13 +1057,48 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trimmed string. An alias for PHP's trim() function.
|
||||
* Returns a string with whitespace removed from the start and end of the
|
||||
* string. Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @return Stringy Object with a trimmed $str
|
||||
*/
|
||||
public function trim()
|
||||
public function trim($chars = null)
|
||||
{
|
||||
return static::create(trim($this->str), $this->encoding);
|
||||
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
|
||||
|
||||
return $this->regexReplace("^[$chars]+|[$chars]+\$", '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with whitespace removed from the start of the string.
|
||||
* Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @return Stringy Object with a trimmed $str
|
||||
*/
|
||||
public function trimLeft($chars = null)
|
||||
{
|
||||
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
|
||||
|
||||
return $this->regexReplace("^[$chars]+", '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with whitespace removed from the end of the string.
|
||||
* Supports the removal of unicode whitespace. Accepts an optional
|
||||
* string of characters to strip instead of the defaults.
|
||||
*
|
||||
* @param string $chars Optional string of characters to strip
|
||||
* @return Stringy Object with a trimmed $str
|
||||
*/
|
||||
public function trimRight($chars = null)
|
||||
{
|
||||
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
|
||||
|
||||
return $this->regexReplace("[$chars]+\$", '');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1461,4 +1529,30 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
|
||||
return static::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert all applicable characters to HTML entities.
|
||||
*
|
||||
* @param int|null $flags See http://php.net/manual/en/function.htmlentities.php
|
||||
* @return Stringy Object with the resulting $str after being html encoded.
|
||||
*/
|
||||
public function htmlEncode($flags = ENT_COMPAT)
|
||||
{
|
||||
$str = htmlentities($this->str, $flags, $this->encoding);
|
||||
|
||||
return static::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert all HTML entities to their applicable characters.
|
||||
*
|
||||
* @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php
|
||||
* @return Stringy Object with the resulting $str after being html decoded.
|
||||
*/
|
||||
public function htmlDecode($flags = ENT_COMPAT)
|
||||
{
|
||||
$str = html_entity_decode($this->str, $flags, $this->encoding);
|
||||
|
||||
return static::create($str, $this->encoding);
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertInstanceOf('Stringy\Stringy', $actual);
|
||||
}
|
||||
|
||||
public function indexOfProvider()
|
||||
{
|
||||
return array(
|
||||
array(2, 'This is the string', 'is'),
|
||||
array(2, 'This is the string', 'is', 0, 'UTF-8'),
|
||||
array(false, 'This is the string', 'not-found', 0, 'UTF-8'),
|
||||
array(32, 'This is the string... and there is another thing', 'is', 10, 'UTF-8'),
|
||||
);
|
||||
}
|
||||
|
||||
public function indexOfLastProvider()
|
||||
{
|
||||
return array(
|
||||
array(5, 'This is the string', 'is'),
|
||||
array(5, 'This is the string', 'is', 0, 'UTF-8'),
|
||||
array(false, 'This is the string', 'not-found', 0, 'UTF-8'),
|
||||
array(32, 'This is the string... and there is another thing', 'is', 0, 'UTF-8'),
|
||||
);
|
||||
}
|
||||
|
||||
public function charsProvider()
|
||||
{
|
||||
return array(
|
||||
@@ -125,6 +145,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function delimitProvider()
|
||||
{
|
||||
return array(
|
||||
array('test*case', 'testCase', '*'),
|
||||
array('test&case', 'Test-Case', '&'),
|
||||
array('test#case', 'test case', '#'),
|
||||
array('test**case', 'test -case', '**'),
|
||||
array('~!~test~!~case', '-test - case', '~!~'),
|
||||
array('test*case', 'test_case', '*'),
|
||||
array('test%c%test', ' test c test', '%'),
|
||||
array('test+u+case', 'TestUCase', '+'),
|
||||
array('test=c=c=test', 'TestCCTest', '='),
|
||||
array('string#>with1number', 'string_with1number', '#>'),
|
||||
array('1test2case', '1test2case', '*'),
|
||||
array('test ύα σase', 'test Σase', ' ύα ', 'UTF-8',),
|
||||
array('στανιλαcase', 'Στανιλ case', 'α', 'UTF-8',),
|
||||
array('σashΘcase', 'Σash Case', 'Θ', 'UTF-8')
|
||||
);
|
||||
}
|
||||
|
||||
public function swapCaseProvider()
|
||||
{
|
||||
return array(
|
||||
@@ -600,7 +640,49 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('fòô bàř', ' fòô bàř '),
|
||||
array('fòô bàř', ' fòô bàř'),
|
||||
array('fòô bàř', 'fòô bàř '),
|
||||
array('fòô bàř', "\n\t fòô bàř \n\t")
|
||||
array(' foo bar ', "\n\t foo bar \n\t", "\n\t"),
|
||||
array('fòô bàř', "\n\t fòô bàř \n\t", null, 'UTF-8'),
|
||||
array('fòô', ' fòô ', null, 'UTF-8'), // narrow no-break space (U+202F)
|
||||
array('fòô', ' fòô ', null, 'UTF-8'), // medium mathematical space (U+205F)
|
||||
array('fòô', ' fòô', null, 'UTF-8') // spaces U+2000 to U+200A
|
||||
);
|
||||
}
|
||||
|
||||
public function trimLeftProvider()
|
||||
{
|
||||
return array(
|
||||
array('foo bar ', ' foo bar '),
|
||||
array('foo bar', ' foo bar'),
|
||||
array('foo bar ', 'foo bar '),
|
||||
array("foo bar \n\t", "\n\t foo bar \n\t"),
|
||||
array('fòô bàř ', ' fòô bàř '),
|
||||
array('fòô bàř', ' fòô bàř'),
|
||||
array('fòô bàř ', 'fòô bàř '),
|
||||
array('foo bar', '--foo bar', '-'),
|
||||
array('fòô bàř', 'òòfòô bàř', 'ò', 'UTF-8'),
|
||||
array("fòô bàř \n\t", "\n\t fòô bàř \n\t", null, 'UTF-8'),
|
||||
array('fòô ', ' fòô ', null, 'UTF-8'), // narrow no-break space (U+202F)
|
||||
array('fòô ', ' fòô ', null, 'UTF-8'), // medium mathematical space (U+205F)
|
||||
array('fòô', ' fòô', null, 'UTF-8') // spaces U+2000 to U+200A
|
||||
);
|
||||
}
|
||||
|
||||
public function trimRightProvider()
|
||||
{
|
||||
return array(
|
||||
array(' foo bar', ' foo bar '),
|
||||
array('foo bar', 'foo bar '),
|
||||
array(' foo bar', ' foo bar'),
|
||||
array("\n\t foo bar", "\n\t foo bar \n\t"),
|
||||
array(' fòô bàř', ' fòô bàř '),
|
||||
array('fòô bàř', 'fòô bàř '),
|
||||
array(' fòô bàř', ' fòô bàř'),
|
||||
array('foo bar', 'foo bar--', '-'),
|
||||
array('fòô bàř', 'fòô bàřòò', 'ò', 'UTF-8'),
|
||||
array("\n\t fòô bàř", "\n\t fòô bàř \n\t", null, 'UTF-8'),
|
||||
array(' fòô', ' fòô ', null, 'UTF-8'), // narrow no-break space (U+202F)
|
||||
array(' fòô', ' fòô ', null, 'UTF-8'), // medium mathematical space (U+205F)
|
||||
array('fòô', 'fòô ', null, 'UTF-8') // spaces U+2000 to U+200A
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1023,4 +1105,26 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
|
||||
);
|
||||
}
|
||||
|
||||
public function htmlEncodeProvider()
|
||||
{
|
||||
return array(
|
||||
array('&', '&'),
|
||||
array('"', '"'),
|
||||
array(''', "'", ENT_QUOTES),
|
||||
array('<', '<'),
|
||||
array('>', '>'),
|
||||
);
|
||||
}
|
||||
|
||||
public function htmlDecodeProvider()
|
||||
{
|
||||
return array(
|
||||
array('&', '&'),
|
||||
array('"', '"'),
|
||||
array("'", ''', ENT_QUOTES),
|
||||
array('<', '<'),
|
||||
array('>', '>'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,24 @@ use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider indexOfProvider()
|
||||
*/
|
||||
public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
|
||||
{
|
||||
$result = S::indexOf($str, $subStr, $offset, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider indexOfLastProvider()
|
||||
*/
|
||||
public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
|
||||
{
|
||||
$result = S::indexOfLast($str, $subStr, $offset, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
@@ -384,9 +402,31 @@ class StaticStringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
public function testTrim($expected, $str, $chars = null, $encoding = null)
|
||||
{
|
||||
$result = S::trim($str);
|
||||
$result = S::trim($str, $chars, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimLeftProvider()
|
||||
*/
|
||||
public function testTrimLeft($expected, $str, $chars = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::trimLeft($str, $chars, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimRightProvider()
|
||||
*/
|
||||
public function testTrimRight($expected, $str, $chars = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::trimRight($str, $chars, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
@@ -647,4 +687,24 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider htmlEncodeProvider()
|
||||
*/
|
||||
public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
$result = S::htmlEncode($str, $flags, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider htmlDecodeProvider()
|
||||
*/
|
||||
public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
$result = S::htmlDecode($str, $flags, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
|
@@ -153,6 +153,24 @@ class StringyTestCase extends CommonTest
|
||||
unset($stringy[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider indexOfProvider()
|
||||
*/
|
||||
public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->indexOf($subStr, $offset);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider indexOfLastProvider()
|
||||
*/
|
||||
public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->indexOfLast($subStr, $offset);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
@@ -236,6 +254,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider delimitProvider()
|
||||
*/
|
||||
public function testDelimit($expected, $str, $delimiter, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->delimit($delimiter);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider swapCaseProvider()
|
||||
*/
|
||||
@@ -596,10 +626,36 @@ class StringyTestCase extends CommonTest
|
||||
/**
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
public function testTrim($expected, $str, $chars = null, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str);
|
||||
$result = $stringy->trim();
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->trim($chars);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimLeftProvider()
|
||||
*/
|
||||
public function testTrimLeft($expected, $str, $chars = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->trimLeft($chars);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider trimRightProvider()
|
||||
*/
|
||||
public function testTrimRight($expected, $str, $chars = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->trimRight($chars);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
@@ -911,4 +967,28 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider htmlEncodeProvider()
|
||||
*/
|
||||
public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->htmlEncode($flags);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider htmlDecodeProvider()
|
||||
*/
|
||||
public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->htmlDecode($flags);
|
||||
$this->assertStringy($result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user