mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 15:54:04 +02:00
Make Stringy\Stringy OO, and add Stringy\StaticStringy
This commit is contained in:
72
README.md
72
README.md
@@ -1,11 +1,13 @@
|
||||
# Stringy
|
||||
|
||||
A PHP library with a variety of string manipulation functions with multibyte support. Inspired by underscore.string.js.
|
||||
A PHP library with a variety of string manipulation functions with multibyte support. It contains both an OO and Static wrapper. Inspired by underscore.string.js.
|
||||
|
||||
Note: The methods listed below are subject to change until we reach a 1.0.0 release.
|
||||
|
||||
* [Requiring/Loading](#requiringloading)
|
||||
* [OO & Procedural](#oo-procedural)
|
||||
* [Methods](#methods)
|
||||
* [create](#create)
|
||||
* [upperCaseFirst](#uppercasefirst)
|
||||
* [lowerCaseFirst](#lowercasefirst)
|
||||
* [camelize](#camelize)
|
||||
@@ -54,18 +56,41 @@ Otherwise, you can simply require the file directly:
|
||||
|
||||
```php
|
||||
require_once 'path/to/Stringy/src/Stringy/Stringy.php';
|
||||
// or
|
||||
require_once 'path/to/Stringy/src/Stringy/StaticStringy.php';
|
||||
```
|
||||
|
||||
And in either case, I'd suggest using an alias.
|
||||
|
||||
```php
|
||||
use Stringy\Stringy as S;
|
||||
// or
|
||||
use Stringy\StaticStringy as S;
|
||||
```
|
||||
|
||||
## OO & Procedural
|
||||
|
||||
Documentation coming soon.
|
||||
|
||||
## 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.
|
||||
|
||||
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*
|
||||
|
||||
##### create
|
||||
|
||||
S->create(string $str, [, $encoding ])
|
||||
|
||||
Creates a Stringy object and assigns both str and encoding properties
|
||||
the supplied values. If $encoding is not specified, it defaults to
|
||||
mb_internal_encoding(). It then returns the instantiated object.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
|
||||
```
|
||||
|
||||
##### upperCaseFirst
|
||||
|
||||
S::upperCaseFirst(string $str [, string $encoding ])
|
||||
@@ -74,6 +99,7 @@ Converts the first character of the supplied string to upper case, with
|
||||
support for multibyte strings.
|
||||
|
||||
```php
|
||||
S::create('σ test', 'UTF-8')->upperCaseFirst();
|
||||
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
||||
```
|
||||
|
||||
@@ -85,6 +111,7 @@ Converts the first character of the supplied string to lower case, with
|
||||
support for multibyte strings.
|
||||
|
||||
```php
|
||||
S::create('Σ test', 'UTF-8')->lowerCaseFirst();
|
||||
S::lowerCaseFirst('Σ test', 'UTF-8'); // 'σ test'
|
||||
```
|
||||
|
||||
@@ -97,6 +124,7 @@ Trims surrounding spaces, capitalizes letters following digits, spaces,
|
||||
dashes and underscores, and removes spaces, dashes, underscores.
|
||||
|
||||
```php
|
||||
S::create('Camel-Case')->camelize();
|
||||
S::camelize('Camel-Case'); // 'camelCase'
|
||||
```
|
||||
|
||||
@@ -109,6 +137,7 @@ support. Trims surrounding spaces, capitalizes letters following digits,
|
||||
spaces, dashes and underscores, and removes spaces, dashes, underscores.
|
||||
|
||||
```php
|
||||
S::create('Upper Camel-Case')->upperCamelize();
|
||||
S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase'
|
||||
```
|
||||
|
||||
@@ -122,6 +151,7 @@ multibyte support. Dashes are inserted before uppercase characters
|
||||
of spaces as well as underscores.
|
||||
|
||||
```php
|
||||
S::create('TestDCase')->dasherize();
|
||||
S::dasherize('TestDCase'); // 'test-d-case'
|
||||
```
|
||||
|
||||
@@ -135,6 +165,7 @@ multibyte support. Underscores are inserted before uppercase characters
|
||||
of spaces as well as dashes.
|
||||
|
||||
```php
|
||||
S::create('TestUCase')->underscored();
|
||||
S::underscored('TestUCase'); // 'test_u_case'
|
||||
```
|
||||
|
||||
@@ -145,6 +176,7 @@ S::swapCase(string $str [, string $encoding ])
|
||||
Returns a case swapped version of a string.
|
||||
|
||||
```php
|
||||
S::create('Ντανιλ', 'UTF-8')->swapCase();
|
||||
S::swapCase('Ντανιλ', 'UTF-8'); // 'νΤΑΝΙΛ'
|
||||
```
|
||||
|
||||
@@ -159,6 +191,7 @@ capitalized.
|
||||
|
||||
```php
|
||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||
S::create('i like to watch DVDs at home', 'UTF-8')->titleize($ignore);
|
||||
S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
|
||||
// 'I Like to Watch DVDs at Home'
|
||||
```
|
||||
@@ -171,6 +204,7 @@ Capitalizes the first word of a string, replaces underscores with spaces,
|
||||
and strips '_id'.
|
||||
|
||||
```php
|
||||
S::create('author_id')->humanize();
|
||||
S::humanize('author_id'); // 'Author'
|
||||
```
|
||||
|
||||
@@ -182,6 +216,7 @@ Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||
(and commonly used in Word documents) with their ASCII equivalents.
|
||||
|
||||
```php
|
||||
S::create('“I see…”')->tidy();
|
||||
S::tidy('“I see…”'); // '"I see..."'
|
||||
```
|
||||
|
||||
@@ -193,6 +228,7 @@ Trims the string and replaces consecutive whitespace characters with a
|
||||
single space. This inclues tabs and newline characters.
|
||||
|
||||
```php
|
||||
S::create(' Ο συγγραφέας ')->collapseWhitespace();
|
||||
S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγραφέας'
|
||||
```
|
||||
|
||||
@@ -203,6 +239,7 @@ S::standardize(string $str)
|
||||
Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř')->standardize();
|
||||
S::standardize('fòô bàř'); // 'foo bar'
|
||||
```
|
||||
|
||||
@@ -213,9 +250,11 @@ S::pad(string $str , int $length [, string $padStr = ' ' [, string $padType = 'r
|
||||
Pads a string to a given length with another string. If length is less
|
||||
than or equal to the length of $str, then no padding takes places. The
|
||||
default string used for padding is a space, and the default type (one of
|
||||
'left', 'right', 'both') is 'right'.
|
||||
'left', 'right', 'both') is 'right'. Throws an exception if $padType
|
||||
isn't one of those 3 values.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
|
||||
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
|
||||
```
|
||||
|
||||
@@ -227,6 +266,7 @@ Returns a new string of a given length such that the beginning of the
|
||||
string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
|
||||
|
||||
```php
|
||||
S::create($str, $encoding)->padLeft($length, $padStr);
|
||||
S::padLeft('foo bar', 9, ' '); // ' foo bar'
|
||||
```
|
||||
|
||||
@@ -238,6 +278,7 @@ Returns a new string of a given length such that the end of the string is
|
||||
padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
|
||||
|
||||
```php
|
||||
S::create('foo bar')->padRight(10, '_*');
|
||||
S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
|
||||
```
|
||||
|
||||
@@ -249,6 +290,7 @@ Returns a new string of a given length such that both sides of the string
|
||||
string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
|
||||
```php
|
||||
S::create('foo bar')->padBoth(9, ' ');
|
||||
S::padBoth('foo bar', 9, ' '); // ' foo bar '
|
||||
```
|
||||
|
||||
@@ -261,6 +303,7 @@ By default, the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('FÒÔ bàřs', 'UTF-8')->startsWith('fòô bàř', false);
|
||||
S::startsWith('FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'); // true
|
||||
```
|
||||
|
||||
@@ -273,6 +316,7 @@ By default, the comparison is case-sensitive, but can be made insensitive
|
||||
by setting $caseSensitive to false.
|
||||
|
||||
```php
|
||||
S::create('FÒÔ bàřs', 'UTF-8')->endsWith('àřs', true);
|
||||
S::endsWith('FÒÔ bàřs', 'àřs', true, 'UTF-8'); // true
|
||||
```
|
||||
|
||||
@@ -284,7 +328,8 @@ Converts each tab in a string to some number of spaces, as defined by
|
||||
$tabLength. By default, each tab is converted to 4 consecutive spaces.
|
||||
|
||||
```php
|
||||
S::toSpaces(' String speech = "Hi"') \\ ' String speech = "Hi"'
|
||||
S::create(' String speech = "Hi"')->toSpaces();
|
||||
S::toSpaces(' String speech = "Hi"') // ' String speech = "Hi"'
|
||||
```
|
||||
|
||||
##### toTabs
|
||||
@@ -296,7 +341,8 @@ by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
||||
converted to a tab.
|
||||
|
||||
```php
|
||||
S::toTabs(" fòô bàř") \\ " fòô bàř"
|
||||
S::create(' fòô bàř')->toTabs();
|
||||
S::toTabs(' fòô bàř') // ' fòô bàř'
|
||||
```
|
||||
|
||||
##### slugify
|
||||
@@ -309,6 +355,7 @@ non-alphanumeric and non-ASCII characters, and replacing whitespace with
|
||||
dashes. The string is also converted to lowercase.
|
||||
|
||||
```php
|
||||
S::create('Using strings like fòô bàř')->slugify();
|
||||
S::slugify('Using strings like fòô bàř') // 'using-strings-like-foo-bar'
|
||||
```
|
||||
|
||||
@@ -319,6 +366,7 @@ S::contains(string $haystack, string $needle [, string $encoding ])
|
||||
Returns true if $haystack contains $needle, false otherwise.
|
||||
|
||||
```php
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
|
||||
S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8') // true
|
||||
```
|
||||
|
||||
@@ -329,6 +377,7 @@ S::surround(string $str, string $substring)
|
||||
Surrounds a string with the given substring.
|
||||
|
||||
```php
|
||||
S::create(' ͜ ')->surround('ʘ');
|
||||
S::surround(' ͜ ', 'ʘ'); // 'ʘ ͜ ʘ'
|
||||
```
|
||||
|
||||
@@ -339,6 +388,7 @@ S::insert(string $str, int $index, string $substring [, string $encoding ])
|
||||
Inserts $substring into $str at the $index provided.
|
||||
|
||||
```php
|
||||
S::create('fòô bà', 'UTF-8')->insert('ř', 6);
|
||||
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
||||
```
|
||||
|
||||
@@ -352,6 +402,7 @@ is further truncated so that the substring may be appended without
|
||||
exceeding the desired length.
|
||||
|
||||
```php
|
||||
S::create('What are your plans today?')->safeTruncate(22, '...');
|
||||
S::safeTruncate('What are your plans today?', 22, '...'); // 'What are your plans...'
|
||||
```
|
||||
|
||||
@@ -362,6 +413,7 @@ S::reverse(string $str, [, string $encoding ])
|
||||
Reverses a string. A multibyte version of strrev.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->reverse();
|
||||
S::reverse('fòô bàř', 'UTF-8'); // 'řàb ôòf'
|
||||
```
|
||||
|
||||
@@ -373,9 +425,21 @@ A multibyte str_shuffle function. It randomizes the order of characters
|
||||
in a string.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->shuffle();
|
||||
S::shuffle('fòô bàř', 'UTF-8') // 'àôřb òf'
|
||||
```
|
||||
|
||||
##### trim
|
||||
|
||||
S::trim(string $str)
|
||||
|
||||
Trims $str. An alias for PHP's trim() function.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř')->trim();
|
||||
S::trim(' fòô bàř ') // 'fòô bàř'
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
**longestCommonPrefix**
|
||||
|
399
src/Stringy/StaticStringy.php
Normal file
399
src/Stringy/StaticStringy.php
Normal file
@@ -0,0 +1,399 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->lowerCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->camelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an UpperCamelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to UpperCamelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCamelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by dashes. Dashes are
|
||||
* inserted before uppercase characters (with the exception of the first
|
||||
* character of the string), and in place of spaces as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->dasherize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by underscores.
|
||||
* Underscores are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces as well as
|
||||
* dashes.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->underscored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of a string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->swapCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of each word in a string, after trimming.
|
||||
* Ignores the case of other letters, allowing for the use of acronyms.
|
||||
* Also accepts an array, $ignore, allowing you to list words not to be
|
||||
* capitalized.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->titleize($ignore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first word of a string, replaces underscores with spaces,
|
||||
* and strips '_id'.
|
||||
*
|
||||
* @param string $str String to humanize
|
||||
* @param string $encoding The character encoding
|
||||
* @return string A humanized string
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->humanize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||
* (and commonly used in Word documents) with their ASCII equivalents.
|
||||
*
|
||||
* @param string $str String to remove special chars
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->tidy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This inclues tabs and newline characters.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
*/
|
||||
public static function collapseWhitespace($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->collapseWhitespace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||
*
|
||||
* @param string $str A string with non-ASCII characters
|
||||
* @return string The string after the replacements
|
||||
*/
|
||||
public static function standardize($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->standardize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads a string to a given length with another string. If length is less
|
||||
* than or equal to the length of $str, then no padding takes places. The
|
||||
* default string used for padding is a space, and the default type (one of
|
||||
* 'left', 'right', 'both') is 'right'. Throws an exception if $padType
|
||||
* isn't one of those 3 values.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)
|
||||
->pad($length, $padStr, $padType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the beginning of the
|
||||
* string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padLeft($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the end of the string is
|
||||
* padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padRight($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that both sides of the string
|
||||
* string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padBoth($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str begins with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the start of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->startsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str ends with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the end of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->endsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each tab in a string to some number of spaces, as defined by
|
||||
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
|
||||
*
|
||||
* @param string $str String to convert tabs to spaces
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toSpaces($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each occurence of some consecutive number of spaces, as defined
|
||||
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
||||
* converted to a tab.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toTabs($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the supplied text into an URL slug. This includes replacing
|
||||
* non-ASCII characters with their closest ASCII equivalents, removing
|
||||
* non-alphanumeric and non-ASCII characters, and replacing whitespace with
|
||||
* dashes. The string is also converted to lowercase.
|
||||
*
|
||||
* @param string $str Text to transform into an URL slug
|
||||
* @return string The corresponding URL slug
|
||||
*/
|
||||
public static function slugify($str)
|
||||
{
|
||||
return Stringy::create($str)->slugify();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $haystack contains $needle, false otherwise.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains $needle
|
||||
*/
|
||||
public static function contains($haystack, $needle, $encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)->contains($needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
{
|
||||
return Stringy::create($str)->surround($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into $str at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->insert($substring, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* chop words. If $substring is provided, and truncating occurs, the string
|
||||
* is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->safeTruncate($length, $substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses a string. A multibyte version of strrev.
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* A multibyte str_shuffle function. It randomizes the order of characters
|
||||
* in a string.
|
||||
*
|
||||
* @param string $str String to shuffle
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The shuffled string
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims $str. An alias for PHP's trim() function.
|
||||
*
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public function trim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
}
|
@@ -4,142 +4,171 @@ namespace Stringy;
|
||||
|
||||
class Stringy
|
||||
{
|
||||
public $str;
|
||||
|
||||
public $encoding;
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
* Creates a Stringy object and assigns both str and encoding properties
|
||||
* the supplied values. If $encoding is not specified, it defaults to
|
||||
* mb_internal_encoding(). It then returns the instantiated object.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
* @return Stringy A Stringy object
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
public static function create($str, $encoding = null)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$first = mb_substr($str, 0, 1, $encoding);
|
||||
$rest = mb_substr($str, 1, mb_strlen($str, $encoding) - 1, $encoding);
|
||||
$stringyObj = new Stringy();
|
||||
$stringyObj->str = $str;
|
||||
$stringyObj->encoding = $encoding;
|
||||
|
||||
return mb_strtoupper($first, $encoding) . $rest;
|
||||
return $stringyObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
* Returns the value in $str.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
* @return string The current value of the $str property
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
public function __toString()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$first = mb_substr($str, 0, 1, $encoding);
|
||||
$rest = mb_substr($str, 1, mb_strlen($str, $encoding) - 1, $encoding);
|
||||
|
||||
return mb_strtolower($first, $encoding) . $rest;
|
||||
return $this->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
* Converts the first character of the string to upper case.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
* @return Stringy Object with the first character of $str being upper case
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
public function upperCaseFirst()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$first = mb_substr($this->str, 0, 1, $this->encoding);
|
||||
$rest = mb_substr($this->str, 1, mb_strlen($this->str, $this->encoding) - 1,
|
||||
$this->encoding);
|
||||
|
||||
$this->str = mb_strtoupper($first, $this->encoding) . $rest;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the string to lower case.
|
||||
*
|
||||
* @return Stringy Object with the first character of $str being lower case
|
||||
*/
|
||||
public function lowerCaseFirst()
|
||||
{
|
||||
$first = mb_substr($this->str, 0, 1, $this->encoding);
|
||||
$rest = mb_substr($this->str, 1, mb_strlen($this->str, $this->encoding) - 1,
|
||||
$this->encoding);
|
||||
|
||||
$this->str = mb_strtolower($first, $this->encoding) . $rest;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the string to camelCase. Trims surrounding spaces, capitalizes
|
||||
* letters following digits, spaces, dashes and underscores, and removes
|
||||
* spaces, dashes, underscores.
|
||||
*
|
||||
* @return Stringy Object with $str in camelCase
|
||||
*/
|
||||
public function camelize()
|
||||
{
|
||||
$encoding = $this->encoding;
|
||||
|
||||
$camelCase = preg_replace_callback(
|
||||
'/[-_\s]+(.)?/u',
|
||||
function ($matches) use (&$encoding) {
|
||||
return $matches[1] ? mb_strtoupper($matches[1], $encoding) : "";
|
||||
function ($match) use (&$encoding) {
|
||||
return $match[1] ? mb_strtoupper($match[1], $encoding) : "";
|
||||
},
|
||||
self::lowerCaseFirst(trim($str), $encoding)
|
||||
$this->trim()->lowerCaseFirst()
|
||||
);
|
||||
|
||||
$camelCase = preg_replace_callback(
|
||||
$this->str = preg_replace_callback(
|
||||
'/[\d]+(.)?/u',
|
||||
function ($matches) use (&$encoding) {
|
||||
return mb_strtoupper($matches[0], $encoding);
|
||||
function ($match) use (&$encoding) {
|
||||
return mb_strtoupper($match[0], $encoding);
|
||||
},
|
||||
$camelCase
|
||||
);
|
||||
|
||||
return $camelCase;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an UpperCamelCase version of a supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
* Converts the string to UpperCamelCase. Trims surrounding spaces, capitalizes
|
||||
* letters following digits, spaces, dashes and underscores, and removes
|
||||
* spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to UpperCamelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in UpperCamelCase
|
||||
* @return Stringy Object with $str in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
public function upperCamelize()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$camelCase = self::camelize($str, $encoding);
|
||||
$this->camelize()->upperCaseFirst();
|
||||
|
||||
return self::upperCaseFirst($camelCase, $encoding);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by dashes. Dashes are
|
||||
* inserted before uppercase characters (with the exception of the first
|
||||
* Sets the string to lowercase, trims it, and seperates is by dashes. Dashes
|
||||
* are inserted before uppercase characters (with the exception of the first
|
||||
* character of the string), and in place of spaces as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
* @return Stringy Object with a dasherized $str
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
public function dasherize()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
mb_regex_encoding($encoding);
|
||||
// Save current regex encoding so we can reset it after
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', trim($str));
|
||||
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', $this->trim());
|
||||
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
|
||||
|
||||
return mb_strtolower($dasherized, $encoding);
|
||||
mb_regex_encoding($regexEncoding);
|
||||
$this->str = mb_strtolower($dasherized, $this->encoding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a lowercase and trimmed string seperated by underscores.
|
||||
* Sets $str to a lowercase and trimmed string seperated by underscores.
|
||||
* Underscores are inserted before uppercase characters (with the exception
|
||||
* of the first character of the string), and in place of spaces as well as
|
||||
* dashes.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
* @return Stringy Object with an underscored $str
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
public function underscored()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
mb_regex_encoding($encoding);
|
||||
// Save current regex encoding so we can reset it after
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', trim($str));
|
||||
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', $this->trim());
|
||||
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
|
||||
|
||||
return mb_strtolower($underscored, $encoding);
|
||||
mb_regex_encoding($regexEncoding);
|
||||
$this->str = mb_strtolower($underscored, $this->encoding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of a string.
|
||||
* Sets $str to a case swapped version of the string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
* @return Stringy Object whose $str has each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
public function swapCase()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$encoding = $this->encoding;
|
||||
|
||||
$swapped = preg_replace_callback(
|
||||
$this->str = preg_replace_callback(
|
||||
'/[\S]/u',
|
||||
function ($match) use (&$encoding) {
|
||||
if ($match[0] == mb_strtoupper($match[0], $encoding))
|
||||
@@ -147,93 +176,89 @@ class Stringy
|
||||
else
|
||||
return mb_strtoupper($match[0], $encoding);
|
||||
},
|
||||
$str
|
||||
$this
|
||||
);
|
||||
|
||||
return $swapped;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of each word in a string, after trimming.
|
||||
* Capitalizes the first letter of each word in $str, after trimming.
|
||||
* Ignores the case of other letters, allowing for the use of acronyms.
|
||||
* Also accepts an array, $ignore, allowing you to list words not to be
|
||||
* capitalized.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
* @return Stringy Object with a titleized $str
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
public function titleize($ignore = null)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$encoding = $this->encoding;
|
||||
$that = $this;
|
||||
|
||||
$titleized = preg_replace_callback(
|
||||
$this->str = preg_replace_callback(
|
||||
'/([\S]+)/u',
|
||||
function ($match) use (&$encoding, &$ignore) {
|
||||
function ($match) use (&$encoding, &$ignore, &$that) {
|
||||
if ($ignore && in_array($match[0], $ignore))
|
||||
return $match[0];
|
||||
return Stringy::upperCaseFirst($match[0], $encoding);
|
||||
$that->str = $match[0];
|
||||
return $that->upperCaseFirst();
|
||||
},
|
||||
trim($str)
|
||||
$this->trim()
|
||||
);
|
||||
|
||||
return $titleized;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalizes the first word of a string, replaces underscores with spaces,
|
||||
* Capitalizes the first word of $str, replaces underscores with spaces,
|
||||
* and strips '_id'.
|
||||
*
|
||||
* @param string $str String to humanize
|
||||
* @param string $encoding The character encoding
|
||||
* @return string A humanized string
|
||||
* @return Stringy Object with a humanized $str
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
public function humanize()
|
||||
{
|
||||
$humanized = str_replace('_id', '', $str);
|
||||
$humanized = str_replace('_', ' ', $humanized);
|
||||
$humanized = str_replace('_id', '', $this->str);
|
||||
$this->str = str_replace('_', ' ', $humanized);
|
||||
|
||||
return self::upperCaseFirst(trim($humanized), $encoding);
|
||||
return $this->trim()->upperCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
||||
* (and commonly used in Word documents) with their ASCII equivalents.
|
||||
*
|
||||
* @param string $str String to remove special chars
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with those characters removed
|
||||
* @return Stringy Object whose $str has those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
public function tidy()
|
||||
{
|
||||
$tidied = preg_replace('/\x{2026}/u', '...', $str);
|
||||
$tidied = preg_replace('/[\x{201C}\x{201D}]/u', '"', $tidied);
|
||||
$tidied = preg_replace('/[\x{2018}\x{2019}]/u', "'", $tidied);
|
||||
$tidied = preg_replace('/[\x{2013}\x{2014}]/u', '-', $tidied);
|
||||
$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);
|
||||
|
||||
return $tidied;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the string and replaces consecutive whitespace characters with a
|
||||
* single space. This inclues tabs and newline characters.
|
||||
* Trims $str and replaces consecutive whitespace characters with a single
|
||||
* space. This inclues tabs and newline characters.
|
||||
*
|
||||
* @param string $str The string to cleanup whitespace
|
||||
* @return string The trimmed string with condensed whitespace
|
||||
* @return Stringy Object with a trimmed $str with condensed whitespace
|
||||
*/
|
||||
public static function collapseWhitespace($str)
|
||||
public function collapseWhitespace()
|
||||
{
|
||||
return preg_replace('/\s+/u', ' ', trim($str));
|
||||
$this->str = preg_replace('/\s+/u', ' ', $this->trim());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts some non-ASCII characters to their closest ASCII counterparts.
|
||||
*
|
||||
* @param string $str A string with non-ASCII characters
|
||||
* @return string The string after the replacements
|
||||
* @return Stringy Object whose $str had those characters replaced
|
||||
*/
|
||||
public static function standardize($str)
|
||||
public function standardize()
|
||||
{
|
||||
$charsArray = array(
|
||||
'a' => array('à', 'á', 'â', 'ã', 'ă', 'ä', 'å', 'ą'),
|
||||
@@ -269,42 +294,38 @@ class Stringy
|
||||
);
|
||||
|
||||
foreach ($charsArray as $key => $value) {
|
||||
$str = str_replace($value, $key, $str);
|
||||
$this->str = str_replace($value, $key, $this->str);
|
||||
}
|
||||
|
||||
return $str;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pads a string to a given length with another string. If length is less
|
||||
* than or equal to the length of $str, then no padding takes places. The
|
||||
* default string used for padding is a space, and the default type (one of
|
||||
* 'left', 'right', 'both') is 'right'.
|
||||
* Pads $str to a given length with another string. If length is less than
|
||||
* or equal to the length of $str, then no padding takes places. The default
|
||||
* string used for padding is a space, and the default type (one of 'left',
|
||||
* 'right', 'both') is 'right'. Throws an exception if $padType isn't one
|
||||
* of those 3 values.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @return Stringy Object with a padded $str
|
||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
public function pad($length, $padStr = ' ', $padType = 'right')
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
if (!in_array($padType, array('left', 'right', 'both'))) {
|
||||
throw new InvalidArgumentException('Pad expects the fourth ' .
|
||||
"argument to be one of 'left', 'right' or 'both'");
|
||||
throw new InvalidArgumentException('Pad expects $padType ' .
|
||||
"to be one of 'left', 'right' or 'both'");
|
||||
}
|
||||
|
||||
$strLength = mb_strlen($str, $encoding);
|
||||
$padStrLength = mb_strlen($padStr, $encoding);
|
||||
$strLength = mb_strlen($this->str, $this->encoding);
|
||||
$padStrLength = mb_strlen($padStr, $this->encoding);
|
||||
|
||||
if ($length <= $strLength || $padStrLength <= 0)
|
||||
return $str;
|
||||
return $this;
|
||||
|
||||
// Number of times to repeat the padStr if left or right
|
||||
$times = ceil(($length - $strLength) / $padStrLength);
|
||||
@@ -313,12 +334,12 @@ class Stringy
|
||||
if ($padType == 'left') {
|
||||
// Repeat the pad, cut it, and prepend
|
||||
$leftPad = str_repeat($padStr, $times);
|
||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $encoding);
|
||||
$paddedStr = $leftPad . $str;
|
||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $this->encoding);
|
||||
$this->str = $leftPad . $this->str;
|
||||
} elseif ($padType == 'right') {
|
||||
// Append the repeated pad and get a substring of the given length
|
||||
$paddedStr = $str . str_repeat($padStr, $times);
|
||||
$paddedStr = mb_substr($paddedStr, 0, $length, $encoding);
|
||||
$this->str = $this->str . str_repeat($padStr, $times);
|
||||
$this->str = mb_substr($this->str, 0, $length, $this->encoding);
|
||||
} else {
|
||||
// Number of times to repeat the padStr on both sides
|
||||
$paddingSize = ($length - $strLength) / 2;
|
||||
@@ -326,132 +347,116 @@ class Stringy
|
||||
|
||||
// Favour right padding over left, as with str_pad()
|
||||
$rightPad = str_repeat($padStr, $times);
|
||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $encoding);
|
||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $this->encoding);
|
||||
|
||||
$leftPad = str_repeat($padStr, $times);
|
||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $encoding);
|
||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $this->encoding);
|
||||
|
||||
$paddedStr = $leftPad . $str . $rightPad;
|
||||
$this->str = $leftPad . $this->str . $rightPad;
|
||||
}
|
||||
|
||||
return $paddedStr;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the beginning of the
|
||||
* string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
|
||||
* Pads $str to a given length from the begining of the string.
|
||||
* Alias for pad($length, $padStr, 'left')
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @return Stringy Object with a left padded $str
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
public function padLeft($length, $padStr = ' ')
|
||||
{
|
||||
return self::pad($str, $length, $padStr, 'left', $encoding);
|
||||
return $this->pad($length, $padStr, 'left');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the end of the string is
|
||||
* padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
|
||||
* Pads $str to a given length from the end of the string.
|
||||
* Alias for pad($length, $padStr, 'left')
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @return string Object with a right padded $str
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
public function padRight($length, $padStr = ' ')
|
||||
{
|
||||
return self::pad($str, $length, $padStr, 'right', $encoding);
|
||||
return $this->pad($length, $padStr, 'right');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that both sides of the string
|
||||
* string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
* Pads $str to a given length such that both sides of the string string are
|
||||
* padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @return Stringy The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
public function padBoth($length, $padStr = ' ')
|
||||
{
|
||||
return self::pad($str, $length, $padStr, 'both', $encoding);
|
||||
return $this->pad($length, $padStr, 'both');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str begins with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
* Returns true if $str begins with $substring, false otherwise. By default,
|
||||
* the comparison is case-sensitive, but can be made insensitive by setting
|
||||
* $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the start of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
public function startsWith($substring, $caseSensitive = true)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$substringLength = mb_strlen($substring, $encoding);
|
||||
$startOfStr = mb_substr($str, 0, $substringLength, $encoding);
|
||||
$substringLength = mb_strlen($substring, $this->encoding);
|
||||
$startOfStr = mb_substr($this->str, 0, $substringLength, $this->encoding);
|
||||
|
||||
if (!$caseSensitive) {
|
||||
$substring = mb_strtolower($substring, $encoding);
|
||||
$startOfStr = mb_strtolower($startOfStr, $encoding);
|
||||
$substring = mb_strtolower($substring, $this->encoding);
|
||||
$startOfStr = mb_strtolower($startOfStr, $this->encoding);
|
||||
}
|
||||
|
||||
return $substring === $startOfStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string $str ends with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* by setting $caseSensitive to false.
|
||||
* Returns true if $str ends with $substring, false otherwise. By default,
|
||||
* the comparison is case-sensitive, but can be made insensitive by setting
|
||||
* $caseSensitive to false.
|
||||
*
|
||||
* @param string $str String to check the end of
|
||||
* @param string $substring The substring to look for
|
||||
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
public function endsWith($substring, $caseSensitive = true)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
$substringLength = mb_strlen($substring, $this->encoding);
|
||||
$strLength = mb_strlen($this->str, $this->encoding);
|
||||
|
||||
$substringLength = mb_strlen($substring, $encoding);
|
||||
$strLength = mb_strlen($str, $encoding);
|
||||
|
||||
$endOfStr = mb_substr($str, $strLength - $substringLength,
|
||||
$substringLength, $encoding);
|
||||
$endOfStr = mb_substr($this->str, $strLength - $substringLength,
|
||||
$substringLength, $this->encoding);
|
||||
|
||||
if (!$caseSensitive) {
|
||||
$substring = mb_strtolower($substring, $encoding);
|
||||
$endOfStr = mb_strtolower($endOfStr, $encoding);
|
||||
$substring = mb_strtolower($substring, $this->encoding);
|
||||
$endOfStr = mb_strtolower($endOfStr, $this->encoding);
|
||||
}
|
||||
|
||||
return $substring === $endOfStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each tab in a string to some number of spaces, as defined by
|
||||
* Converts each tab in $str to some number of spaces, as defined by
|
||||
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
|
||||
*
|
||||
* @param string $str String to convert tabs to spaces
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
* @return Stringy Object whose $str has had tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
public function toSpaces($tabLength = 4)
|
||||
{
|
||||
$spaces = str_repeat(' ', $tabLength);
|
||||
$this->str = str_replace("\t", $spaces, $this->str);
|
||||
|
||||
return str_replace("\t", $spaces, $str);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -459,164 +464,165 @@ class Stringy
|
||||
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
|
||||
* converted to a tab.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
* @return Stringy Object whose $str has had spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
public function toTabs($tabLength = 4)
|
||||
{
|
||||
$spaces = str_repeat(' ', $tabLength);
|
||||
$this->str = str_replace($spaces, "\t", $this->str);
|
||||
|
||||
return str_replace($spaces, "\t", $str);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the supplied text into an URL slug. This includes replacing
|
||||
* non-ASCII characters with their closest ASCII equivalents, removing
|
||||
* non-alphanumeric and non-ASCII characters, and replacing whitespace with
|
||||
* dashes. The string is also converted to lowercase.
|
||||
* Converts $str into an URL slug. This includes replacing non-ASCII
|
||||
* characters with their closest ASCII equivalents, removing non-alphanumeric
|
||||
* and non-ASCII characters, and replacing whitespace with dashes. The string
|
||||
* is also converted to lowercase.
|
||||
*
|
||||
* @param string $str Text to transform into an URL slug
|
||||
* @return string The corresponding URL slug
|
||||
* @return Stringy Object whose $str has been converted to an URL slug
|
||||
*/
|
||||
public static function slugify($str)
|
||||
public function slugify()
|
||||
{
|
||||
$str = preg_replace('/[^a-zA-Z\d -]/u', '', self::standardize($str));
|
||||
$str = self::collapseWhitespace($str);
|
||||
$this->str = preg_replace('/[^a-zA-Z\d -]/u', '', $this->standardize());
|
||||
$this->collapseWhitespace();
|
||||
$this->str = str_replace(' ', '-', strtolower($this->str));
|
||||
|
||||
return str_replace(' ', '-', strtolower($str));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if $haystack contains $needle, false otherwise.
|
||||
* Returns true if $str contains $needle, false otherwise.
|
||||
*
|
||||
* @param string $haystack String being checked
|
||||
* @param string $needle Substring to look for
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $haystack contains $needle
|
||||
* @return bool Whether or not $str contains $needle
|
||||
*/
|
||||
public static function contains($haystack, $needle, $encoding = null)
|
||||
public function contains($needle)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
if (mb_strpos($haystack, $needle, 0, $encoding) !== false)
|
||||
if (mb_strpos($this->str, $needle, 0, $this->encoding) !== false)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
* Surrounds $str with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
* @return Stringy Object whose $str had the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
public function surround($substring)
|
||||
{
|
||||
return implode('', array($substring, $str, $substring));
|
||||
$this->str = implode('', array($substring, $this->str, $substring));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into $str at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
* @return Stringy Object with the resulting $str after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
public function insert($substring, $index)
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
if ($index > mb_strlen($this->str, $this->encoding))
|
||||
return $this;
|
||||
|
||||
if ($index > mb_strlen($str, $encoding))
|
||||
return $str;
|
||||
$start = mb_substr($this->str, 0, $index, $this->encoding);
|
||||
$end = mb_substr($this->str, $index, mb_strlen($this->str, $this->encoding),
|
||||
$this->encoding);
|
||||
|
||||
$start = mb_substr($str, 0, $index, $encoding);
|
||||
$end = mb_substr($str, $index, mb_strlen($str, $encoding), $encoding);
|
||||
$this->str = $start . $substring . $end;
|
||||
|
||||
return $start . $substring . $end;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* chop words. If $substring is provided, and truncating occurs, the string
|
||||
* Truncates $str to a given length, while ensuring that it does not chop
|
||||
* words. If $substring is provided, and truncating occurs, the string
|
||||
* is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
* @return Stringy Object with the resulting $str after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
public function safeTruncate($length, $substring = '')
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
if ($length >= mb_strlen($str, $encoding))
|
||||
return $str;
|
||||
if ($length >= mb_strlen($this->str, $this->encoding))
|
||||
return $this;
|
||||
|
||||
// Need to further trim the string so we can append the substring
|
||||
$substringLength = mb_strlen($substring, $encoding);
|
||||
$substringLength = mb_strlen($substring, $this->encoding);
|
||||
$length = $length - $substringLength;
|
||||
|
||||
$truncated = mb_substr($str, 0, $length, $encoding);
|
||||
$truncated = mb_substr($this->str, 0, $length, $this->encoding);
|
||||
|
||||
// If the last word was truncated
|
||||
if (mb_strpos($str, ' ', $length - 1, $encoding) != $length) {
|
||||
if (mb_strpos($this->str, ' ', $length - 1, $this->encoding) != $length) {
|
||||
// Find pos of the last occurence of a space, and get everything up until
|
||||
$lastPos = mb_strrpos($truncated, ' ', 0, $encoding);
|
||||
$truncated = mb_substr($truncated, 0, $lastPos, $encoding);
|
||||
$lastPos = mb_strrpos($truncated, ' ', 0, $this->encoding);
|
||||
$truncated = mb_substr($truncated, 0, $lastPos, $this->encoding);
|
||||
}
|
||||
|
||||
return $truncated . $substring;
|
||||
$this->str = $truncated . $substring;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses a string. A multibyte version of strrev.
|
||||
* Reverses $str. A multibyte version of strrev.
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
* @return Stringy Object with a reversed $str
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
public function reverse()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$strLength = mb_strlen($str, $encoding);
|
||||
$strLength = mb_strlen($this->str, $this->encoding);
|
||||
$reversed = '';
|
||||
|
||||
// Loop from last index of string to first
|
||||
for ($i = $strLength - 1; $i >= 0; $i--) {
|
||||
$reversed .= mb_substr($str, $i, 1, $encoding);
|
||||
$reversed .= mb_substr($this->str, $i, 1, $this->encoding);
|
||||
}
|
||||
|
||||
return $reversed;
|
||||
$this->str = $reversed;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* A multibyte str_shuffle function. It randomizes the order of characters
|
||||
* in a string.
|
||||
* in $str.
|
||||
*
|
||||
* @param string $str String to shuffle
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The shuffled string
|
||||
* @return Stringy Object with a shuffled $str
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
public function shuffle()
|
||||
{
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$indexes = range(0, mb_strlen($str, $encoding) - 1);
|
||||
$indexes = range(0, mb_strlen($this->str, $this->encoding) - 1);
|
||||
shuffle($indexes);
|
||||
|
||||
$shuffledStr = '';
|
||||
foreach ($indexes as $i) {
|
||||
$shuffledStr .= mb_substr($str, $i, 1, $encoding);
|
||||
$shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding);
|
||||
}
|
||||
|
||||
return $shuffledStr;
|
||||
$this->str = $shuffledStr;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims $str. An alias for PHP's trim() function.
|
||||
*
|
||||
* @return Stringy Object with a trimmed $str
|
||||
*/
|
||||
public function trim()
|
||||
{
|
||||
$this->str = trim($this->str);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
455
tests/Stringy/CommonTest.php
Normal file
455
tests/Stringy/CommonTest.php
Normal file
@@ -0,0 +1,455 @@
|
||||
<?php
|
||||
|
||||
class CommonTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function stringsForUpperCaseFirst()
|
||||
{
|
||||
$testData = array(
|
||||
array('Test', 'Test'),
|
||||
array('Test', 'test'),
|
||||
array('1a', '1a'),
|
||||
array('Σ test', 'σ test', 'UTF-8'),
|
||||
array(' σ test', ' σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForLowerCaseFirst()
|
||||
{
|
||||
$testData = array(
|
||||
array('test', 'Test'),
|
||||
array('test', 'test'),
|
||||
array('1a', '1a'),
|
||||
array('σ test', 'Σ test', 'UTF-8'),
|
||||
array(' Σ test', ' Σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForCamelize()
|
||||
{
|
||||
$testData = array(
|
||||
array('camelCase', 'CamelCase'),
|
||||
array('camelCase', 'Camel-Case'),
|
||||
array('camelCase', 'camel case'),
|
||||
array('camelCase', 'camel -case'),
|
||||
array('camelCase', 'camel - case'),
|
||||
array('camelCase', 'camel_case'),
|
||||
array('camelCTest', 'camel c test'),
|
||||
array('stringWith1Number', 'string_with1number'),
|
||||
array('stringWith22Numbers', 'string-with-2-2 numbers'),
|
||||
array('1Camel2Case', '1camel2case'),
|
||||
array('camelΣase', 'camel σase', 'UTF-8'),
|
||||
array('στανιλCase', 'Στανιλ case', 'UTF-8'),
|
||||
array('σamelCase', 'σamel Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForUpperCamelize()
|
||||
{
|
||||
$testData = array(
|
||||
array('CamelCase', 'camelCase'),
|
||||
array('CamelCase', 'Camel-Case'),
|
||||
array('CamelCase', 'camel case'),
|
||||
array('CamelCase', 'camel -case'),
|
||||
array('CamelCase', 'camel - case'),
|
||||
array('CamelCase', 'camel_case'),
|
||||
array('CamelCTest', 'camel c test'),
|
||||
array('StringWith1Number', 'string_with1number'),
|
||||
array('StringWith22Numbers', 'string-with-2-2 numbers'),
|
||||
array('1Camel2Case', '1camel2case'),
|
||||
array('CamelΣase', 'camel σase', 'UTF-8'),
|
||||
array('ΣτανιλCase', 'στανιλ case', 'UTF-8'),
|
||||
array('ΣamelCase', 'Σamel Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForDasherize()
|
||||
{
|
||||
$testData = 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-d-case', 'TestDCase'),
|
||||
array('test-c-c-test', 'TestCCTest'),
|
||||
array('string-with1number', 'string_with1number'),
|
||||
array('string-with-2-2-numbers', 'String-with_2_2 numbers'),
|
||||
array('1test2case', '1test2case'),
|
||||
array('dash-σase', 'dash Σase', 'UTF-8'),
|
||||
array('στανιλ-case', 'Στανιλ case', 'UTF-8'),
|
||||
array('σash-case', 'Σash Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForUnderscored()
|
||||
{
|
||||
$testData = 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('string_with_2_2_numbers', 'String-with_2_2 numbers'),
|
||||
array('1test2case', '1test2case'),
|
||||
array('test_σase', 'test Σase', 'UTF-8'),
|
||||
array('στανιλ_case', 'Στανιλ case', 'UTF-8'),
|
||||
array('σash_case', 'Σash Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSwapCase()
|
||||
{
|
||||
$testData = array(
|
||||
array('TESTcASE', 'testCase'),
|
||||
array('tEST-cASE', 'Test-Case'),
|
||||
array(' - σASH cASE', ' - Σash Case', 'UTF-8'),
|
||||
array('νΤΑΝΙΛ', 'Ντανιλ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForTitleize()
|
||||
{
|
||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||
|
||||
$testData = array(
|
||||
array('Testing The Method', 'testing the method'),
|
||||
array('Testing the Method', 'testing the method', $ignore, 'UTF-8'),
|
||||
array('I Like to Watch DVDs at Home', 'i like to watch DVDs at home',
|
||||
$ignore, 'UTF-8'),
|
||||
array('Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForHumanize()
|
||||
{
|
||||
$testData = array(
|
||||
array('Author', 'author_id'),
|
||||
array('Test user', ' _test_user_'),
|
||||
array('Συγγραφέας', ' συγγραφέας_id ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForTidy()
|
||||
{
|
||||
$testData = array(
|
||||
array('"I see..."', '“I see…”'),
|
||||
array("'This too'", "‘This too’"),
|
||||
array('test-dash', 'test—dash'),
|
||||
array('Ο συγγραφέας είπε...', 'Ο συγγραφέας είπε…')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForCollapseWhitespace()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', ' foo bar '),
|
||||
array('test string', 'test string'),
|
||||
array('Ο συγγραφέας', ' Ο συγγραφέας '),
|
||||
array('123', ' 123 '),
|
||||
array('', ' '),
|
||||
array('', ''),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForStandardize()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', 'fòô bàř'),
|
||||
array(' TEST ', ' ŤÉŚŢ '),
|
||||
array('φ = z = 3', 'φ = ź = 3')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForPad()
|
||||
{
|
||||
$testData = array(
|
||||
// $length <= $str
|
||||
array('foo bar', 'foo bar', -1),
|
||||
array('foo bar', 'foo bar', 7),
|
||||
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
|
||||
|
||||
// right
|
||||
array('foo bar ', 'foo bar', 9),
|
||||
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
|
||||
array('foo bar_*_', 'foo bar', 10, '_*', 'right'),
|
||||
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'right', 'UTF-8'),
|
||||
|
||||
// left
|
||||
array(' foo bar', 'foo bar', 9, ' ', 'left'),
|
||||
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
|
||||
array('_*_foo bar', 'foo bar', 10, '_*', 'left'),
|
||||
array(' fòô bàř', 'fòô bàř', 9, ' ', 'left', 'UTF-8'),
|
||||
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'left', 'UTF-8'),
|
||||
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
|
||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'left', 'UTF-8'),
|
||||
|
||||
// both
|
||||
array('foo bar ', 'foo bar', 8, ' ', 'both'),
|
||||
array(' foo bar ', 'foo bar', 9, ' ', 'both'),
|
||||
array('fòô bàř ', 'fòô bàř', 8, ' ', 'both', 'UTF-8'),
|
||||
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'both', 'UTF-8'),
|
||||
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForPadLeft()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar', 'foo bar', 9),
|
||||
array('_*_foo bar', 'foo bar', 10, '_*'),
|
||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForPadRight()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar ', 'foo bar', 9),
|
||||
array('foo bar_*_', 'foo bar', 10, '_*'),
|
||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForPadBoth()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar ', 'foo bar', 8),
|
||||
array(' foo bar ', 'foo bar', 9, ' '),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForStartsWith()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'foo bars', 'foo bar'),
|
||||
array(true, 'FOO bars', 'foo bar', false),
|
||||
array(true, 'FOO bars', 'foo BAR', false),
|
||||
array(true, 'FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'),
|
||||
array(true, 'fòô bàřs', 'fòô BÀŘ', false, 'UTF-8'),
|
||||
array(false, 'foo bar', 'bar'),
|
||||
array(false, 'foo bar', 'foo bars'),
|
||||
array(false, 'FOO bar', 'foo bars'),
|
||||
array(false, 'FOO bars', 'foo BAR'),
|
||||
array(false, 'FÒÔ bàřs', 'fòô bàř', true, 'UTF-8'),
|
||||
array(false, 'fòô bàřs', 'fòô BÀŘ', true, 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForEndsWith()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'foo bars', 'o bars'),
|
||||
array(true, 'FOO bars', 'o bars', false),
|
||||
array(true, 'FOO bars', 'o BARs', false),
|
||||
array(true, 'FÒÔ bàřs', 'ô bàřs', false, 'UTF-8'),
|
||||
array(true, 'fòô bàřs', 'ô BÀŘs', false, 'UTF-8'),
|
||||
array(false, 'foo bar', 'foo'),
|
||||
array(false, 'foo bar', 'foo bars'),
|
||||
array(false, 'FOO bar', 'foo bars'),
|
||||
array(false, 'FOO bars', 'foo BARS'),
|
||||
array(false, 'FÒÔ bàřs', 'fòô bàřs', true, 'UTF-8'),
|
||||
array(false, 'fòô bàřs', 'fòô BÀŘS', true, 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForToSpaces()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar ', ' foo bar '),
|
||||
array(' foo bar ', ' foo bar ', 5),
|
||||
array(' foo bar ', ' foo bar ', 2),
|
||||
array('foobar', ' foo bar ', 0),
|
||||
array(" foo\n bar", " foo\n bar"),
|
||||
array(" fòô\n bàř", " fòô\n bàř")
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForToTabs()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar ', ' foo bar '),
|
||||
array(' foo bar ', ' foo bar ', 5),
|
||||
array(' foo bar ', ' foo bar ', 2),
|
||||
array(" foo\n bar", " foo\n bar"),
|
||||
array(" fòô\n bàř", " fòô\n bàř")
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSlugify()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo-bar', ' foo bar '),
|
||||
array('foo-dbar', " Foo d'Bar "),
|
||||
array('a-string-with-dashes', 'A string-with-dashes'),
|
||||
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
|
||||
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
|
||||
array('numbers-1234', 'numbers 1234')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForContains()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'This string contains foo bar', 'foo bar'),
|
||||
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
|
||||
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'),
|
||||
array(false, 'This string contains foo bar', 'Foo bar'),
|
||||
array(false, 'This string contains foo bar', 'foobar'),
|
||||
array(false, 'This string contains foo bar', 'foo bar '),
|
||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSurround()
|
||||
{
|
||||
$testData = array(
|
||||
array('__foobar__', 'foobar', '__'),
|
||||
array('test', 'test', ''),
|
||||
array('**', '', '*'),
|
||||
array('¬fòô bàř¬', 'fòô bàř', '¬'),
|
||||
array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForInsert()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', 'oo bar', 'f', 0),
|
||||
array('foo bar', 'f bar', 'oo', 1),
|
||||
array('f bar', 'f bar', 'oo', 20),
|
||||
array('foo bar', 'foo ba', 'r', 6),
|
||||
array('fòô bàř', 'òô bàř', 'f', 0, 'UTF-8'),
|
||||
array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'),
|
||||
array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSafeTruncate()
|
||||
{
|
||||
$testData = array(
|
||||
array('Test foo bar', 'Test foo bar', 12),
|
||||
array('Test foo', 'Test foo bar', 11),
|
||||
array('Test foo', 'Test foo bar', 8),
|
||||
array('Test', 'Test foo bar', 7),
|
||||
array('Test', 'Test foo bar', 4),
|
||||
array('Test foo bar', 'Test foo bar', 12, '...'),
|
||||
array('Test foo...', 'Test foo bar', 11, '...'),
|
||||
array('Test...', 'Test foo bar', 8, '...'),
|
||||
array('Test...', 'Test foo bar', 7, '...'),
|
||||
array('...', 'Test foo bar', 4, '...'),
|
||||
array('Test....', 'Test foo bar', 11, '....'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'),
|
||||
array('Test fòô', 'Test fòô bàř', 11, '', 'UTF-8'),
|
||||
array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'),
|
||||
array('Test', 'Test fòô bàř', 7, '', 'UTF-8'),
|
||||
array('Test', 'Test fòô bàř', 4, '', 'UTF-8'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'),
|
||||
array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
|
||||
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForReverse()
|
||||
{
|
||||
$testData = array(
|
||||
array('', ''),
|
||||
array('raboof', 'foobar'),
|
||||
array('řàbôòf', 'fòôbàř', 'UTF-8'),
|
||||
array('řàb ôòf', 'fòô bàř', 'UTF-8'),
|
||||
array('∂∆ ˚åß', 'ßå˚ ∆∂', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForShuffle()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar'),
|
||||
array('∂∆ ˚åß', 'UTF-8'),
|
||||
array('å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
// A test is required so as not to throw an error
|
||||
// This is a lot cleaner than using PHPUnit's mocks to spy
|
||||
public function test() {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
262
tests/Stringy/StaticStringyTest.php
Normal file
262
tests/Stringy/StaticStringyTest.php
Normal file
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/StaticStringy.php");
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCaseFirst($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::lowerCaseFirst($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::camelize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCamelize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::dasherize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::underscored($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::swapCase($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::titleize($str, $ignore, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::humanize($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$result = S::tidy($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
{
|
||||
$result = S::collapseWhitespace($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStandardize
|
||||
*/
|
||||
public function testStandardize($expected, $str)
|
||||
{
|
||||
$result = S::standardize($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$result = S::pad($str, $length, $padStr, $padType, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padLeft($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padRight($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padBoth($str, $length, $padStr, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toSpaces($str, $tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toTabs($str, $tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
*/
|
||||
public function testSlugify($expected, $str)
|
||||
{
|
||||
$result = S::slugify($str);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
{
|
||||
$result = S::contains($haystack, $needle, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$result = S::surround($str, $substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::insert($str, $substring, $index, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::safeTruncate($str, $length, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::reverse($str, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$result = S::shuffle($str, $encoding);
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
}
|
||||
}
|
@@ -5,704 +5,258 @@ require("$base/src/Stringy/Stringy.php");
|
||||
|
||||
use Stringy\Stringy as S;
|
||||
|
||||
class StringyTestCase extends PHPUnit_Framework_TestCase
|
||||
class StringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $string, $encoding = null)
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCaseFirst($string, $encoding);
|
||||
$result = S::create($str, $encoding)->upperCaseFirst();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUpperCaseFirst()
|
||||
{
|
||||
$testData = array(
|
||||
array('Test', 'Test'),
|
||||
array('Test', 'test'),
|
||||
array('1a', '1a'),
|
||||
array('Σ test', 'σ test', 'UTF-8'),
|
||||
array(' σ test', ' σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $string, $encoding = null)
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::lowerCaseFirst($string, $encoding);
|
||||
$result = S::create($str, $encoding)->lowerCaseFirst();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForLowerCaseFirst()
|
||||
{
|
||||
$testData = array(
|
||||
array('test', 'Test'),
|
||||
array('test', 'test'),
|
||||
array('1a', '1a'),
|
||||
array('σ test', 'Σ test', 'UTF-8'),
|
||||
array(' Σ test', ' Σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
*/
|
||||
public function testCamelize($expected, $string, $encoding = null)
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::camelize($string, $encoding);
|
||||
$result = S::create($str, $encoding)->camelize();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForCamelize()
|
||||
{
|
||||
$testData = array(
|
||||
array('camelCase', 'CamelCase'),
|
||||
array('camelCase', 'Camel-Case'),
|
||||
array('camelCase', 'camel case'),
|
||||
array('camelCase', 'camel -case'),
|
||||
array('camelCase', 'camel - case'),
|
||||
array('camelCase', 'camel_case'),
|
||||
array('camelCTest', 'camel c test'),
|
||||
array('stringWith1Number', 'string_with1number'),
|
||||
array('stringWith22Numbers', 'string-with-2-2 numbers'),
|
||||
array('1Camel2Case', '1camel2case'),
|
||||
array('camelΣase', 'camel σase', 'UTF-8'),
|
||||
array('στανιλCase', 'Στανιλ case', 'UTF-8'),
|
||||
array('σamelCase', 'σamel Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
*/
|
||||
public function testUpperCamelize($expected, $string, $encoding = null)
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::upperCamelize($string, $encoding);
|
||||
$result = S::create($str, $encoding)->upperCamelize();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUpperCamelize()
|
||||
{
|
||||
$testData = array(
|
||||
array('CamelCase', 'camelCase'),
|
||||
array('CamelCase', 'Camel-Case'),
|
||||
array('CamelCase', 'camel case'),
|
||||
array('CamelCase', 'camel -case'),
|
||||
array('CamelCase', 'camel - case'),
|
||||
array('CamelCase', 'camel_case'),
|
||||
array('CamelCTest', 'camel c test'),
|
||||
array('StringWith1Number', 'string_with1number'),
|
||||
array('StringWith22Numbers', 'string-with-2-2 numbers'),
|
||||
array('1Camel2Case', '1camel2case'),
|
||||
array('CamelΣase', 'camel σase', 'UTF-8'),
|
||||
array('ΣτανιλCase', 'στανιλ case', 'UTF-8'),
|
||||
array('ΣamelCase', 'Σamel Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
*/
|
||||
public function testDasherize($expected, $string, $encoding = null)
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::dasherize($string, $encoding);
|
||||
$result = S::create($str, $encoding)->dasherize();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForDasherize()
|
||||
{
|
||||
$testData = 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-d-case', 'TestDCase'),
|
||||
array('test-c-c-test', 'TestCCTest'),
|
||||
array('string-with1number', 'string_with1number'),
|
||||
array('string-with-2-2-numbers', 'String-with_2_2 numbers'),
|
||||
array('1test2case', '1test2case'),
|
||||
array('dash-σase', 'dash Σase', 'UTF-8'),
|
||||
array('στανιλ-case', 'Στανιλ case', 'UTF-8'),
|
||||
array('σash-case', 'Σash Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
*/
|
||||
public function testUnderscored($expected, $string, $encoding = null)
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::underscored($string, $encoding);
|
||||
$result = S::create($str, $encoding)->underscored();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUnderscored()
|
||||
{
|
||||
$testData = 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('string_with_2_2_numbers', 'String-with_2_2 numbers'),
|
||||
array('1test2case', '1test2case'),
|
||||
array('test_σase', 'test Σase', 'UTF-8'),
|
||||
array('στανιλ_case', 'Στανιλ case', 'UTF-8'),
|
||||
array('σash_case', 'Σash Case', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
*/
|
||||
public function testSwapCase($expected, $string, $encoding = null)
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::swapCase($string, $encoding);
|
||||
$result = S::create($str, $encoding)->swapCase();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForSwapCase()
|
||||
{
|
||||
$testData = array(
|
||||
array('TESTcASE', 'testCase'),
|
||||
array('tEST-cASE', 'Test-Case'),
|
||||
array(' - σASH cASE', ' - Σash Case', 'UTF-8'),
|
||||
array('νΤΑΝΙΛ', 'Ντανιλ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
*/
|
||||
public function testTitleize($expected, $string, $ignore = null,
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::titleize($string, $ignore, $encoding);
|
||||
$result = S::create($str, $encoding)->titleize($ignore);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForTitleize()
|
||||
{
|
||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||
|
||||
$testData = array(
|
||||
array('Testing The Method', 'testing the method'),
|
||||
array('Testing the Method', 'testing the method', $ignore, 'UTF-8'),
|
||||
array('I Like to Watch DVDs at Home', 'i like to watch DVDs at home',
|
||||
$ignore, 'UTF-8'),
|
||||
array('Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
*/
|
||||
public function testHumanize($expected, $string, $encoding = null)
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::humanize($string, $encoding);
|
||||
$result = S::create($str, $encoding)->humanize();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForHumanize()
|
||||
{
|
||||
$testData = array(
|
||||
array('Author', 'author_id'),
|
||||
array('Test user', ' _test_user_'),
|
||||
array('Συγγραφέας', ' συγγραφέας_id ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
*/
|
||||
public function testTidy($expected, $string)
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
$result = S::tidy($string);
|
||||
$result = S::create($str)->tidy();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForTidy()
|
||||
{
|
||||
$testData = array(
|
||||
array('"I see..."', '“I see…”'),
|
||||
array("'This too'", "‘This too’"),
|
||||
array('test-dash', 'test—dash'),
|
||||
array('Ο συγγραφέας είπε...', 'Ο συγγραφέας είπε…')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $string)
|
||||
public function testCollapseWhitespace($expected, $str)
|
||||
{
|
||||
$result = S::collapseWhitespace($string);
|
||||
$result = S::create($str)->collapseWhitespace();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForCollapseWhitespace()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', ' foo bar '),
|
||||
array('test string', 'test string'),
|
||||
array('Ο συγγραφέας', ' Ο συγγραφέας '),
|
||||
array('123', ' 123 '),
|
||||
array('', ' '),
|
||||
array('', ''),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStandardize
|
||||
*/
|
||||
public function testStandardize($expected, $string)
|
||||
public function testStandardize($expected, $str)
|
||||
{
|
||||
$result = S::standardize($string);
|
||||
$result = S::create($str)->standardize();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForStandardize()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', 'fòô bàř'),
|
||||
array(' TEST ', ' ŤÉŚŢ '),
|
||||
array('φ = z = 3', 'φ = ź = 3')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
*/
|
||||
public function testPad($expected, $string, $length, $padStr = ' ',
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
{
|
||||
$result = S::pad($string, $length, $padStr, $padType, $encoding);
|
||||
$result = S::create($str, $encoding)->pad($length, $padStr, $padType);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForPad()
|
||||
{
|
||||
$testData = array(
|
||||
// $length <= $str
|
||||
array('foo bar', 'foo bar', -1),
|
||||
array('foo bar', 'foo bar', 7),
|
||||
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
|
||||
|
||||
// right
|
||||
array('foo bar ', 'foo bar', 9),
|
||||
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
|
||||
array('foo bar_*_', 'foo bar', 10, '_*', 'right'),
|
||||
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
|
||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'right', 'UTF-8'),
|
||||
|
||||
// left
|
||||
array(' foo bar', 'foo bar', 9, ' ', 'left'),
|
||||
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
|
||||
array('_*_foo bar', 'foo bar', 10, '_*', 'left'),
|
||||
array(' fòô bàř', 'fòô bàř', 9, ' ', 'left', 'UTF-8'),
|
||||
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'left', 'UTF-8'),
|
||||
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
|
||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'left', 'UTF-8'),
|
||||
|
||||
// both
|
||||
array('foo bar ', 'foo bar', 8, ' ', 'both'),
|
||||
array(' foo bar ', 'foo bar', 9, ' ', 'both'),
|
||||
array('fòô bàř ', 'fòô bàř', 8, ' ', 'both', 'UTF-8'),
|
||||
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'both', 'UTF-8'),
|
||||
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'both', 'UTF-8'),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
|
||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
*/
|
||||
public function testPadLeft($expected, $string, $length, $padStr = ' ',
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padLeft($string, $length, $padStr, $encoding);
|
||||
$result = S::create($str, $encoding)->padLeft($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForPadLeft()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar', 'foo bar', 9),
|
||||
array('_*_foo bar', 'foo bar', 10, '_*'),
|
||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
*/
|
||||
public function testPadRight($expected, $string, $length, $padStr = ' ',
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padRight($string, $length, $padStr, $encoding);
|
||||
$result = S::create($str, $encoding)->padRight($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForPadRight()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar ', 'foo bar', 9),
|
||||
array('foo bar_*_', 'foo bar', 10, '_*'),
|
||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
*/
|
||||
public function testPadBoth($expected, $string, $length, $padStr = ' ',
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::padBoth($string, $length, $padStr, $encoding);
|
||||
$result = S::create($str, $encoding)->padBoth($length, $padStr);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForPadBoth()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar ', 'foo bar', 8),
|
||||
array(' foo bar ', 'foo bar', 9, ' '),
|
||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
*/
|
||||
public function testStartsWith($expected, $string, $substring,
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::startsWith($string, $substring, $caseSensitive, $encoding);
|
||||
$result = S::create($str, $encoding)->startsWith($substring, $caseSensitive);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForStartsWith()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'foo bars', 'foo bar'),
|
||||
array(true, 'FOO bars', 'foo bar', false),
|
||||
array(true, 'FOO bars', 'foo BAR', false),
|
||||
array(true, 'FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'),
|
||||
array(true, 'fòô bàřs', 'fòô BÀŘ', false, 'UTF-8'),
|
||||
array(false, 'foo bar', 'bar'),
|
||||
array(false, 'foo bar', 'foo bars'),
|
||||
array(false, 'FOO bar', 'foo bars'),
|
||||
array(false, 'FOO bars', 'foo BAR'),
|
||||
array(false, 'FÒÔ bàřs', 'fòô bàř', true, 'UTF-8'),
|
||||
array(false, 'fòô bàřs', 'fòô BÀŘ', true, 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
*/
|
||||
public function testEndsWith($expected, $string, $substring,
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::endsWith($string, $substring, $caseSensitive, $encoding);
|
||||
$result = S::create($str, $encoding)->endsWith($substring, $caseSensitive);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForEndsWith()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'foo bars', 'o bars'),
|
||||
array(true, 'FOO bars', 'o bars', false),
|
||||
array(true, 'FOO bars', 'o BARs', false),
|
||||
array(true, 'FÒÔ bàřs', 'ô bàřs', false, 'UTF-8'),
|
||||
array(true, 'fòô bàřs', 'ô BÀŘs', false, 'UTF-8'),
|
||||
array(false, 'foo bar', 'foo'),
|
||||
array(false, 'foo bar', 'foo bars'),
|
||||
array(false, 'FOO bar', 'foo bars'),
|
||||
array(false, 'FOO bars', 'foo BARS'),
|
||||
array(false, 'FÒÔ bàřs', 'fòô bàřs', true, 'UTF-8'),
|
||||
array(false, 'fòô bàřs', 'fòô BÀŘS', true, 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
*/
|
||||
public function testToSpaces($expected, $string, $tabLength = 4)
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toSpaces($string, $tabLength);
|
||||
$result = S::create($str)->toSpaces($tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForToSpaces()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar ', ' foo bar '),
|
||||
array(' foo bar ', ' foo bar ', 5),
|
||||
array(' foo bar ', ' foo bar ', 2),
|
||||
array('foobar', ' foo bar ', 0),
|
||||
array(" foo\n bar", " foo\n bar"),
|
||||
array(" fòô\n bàř", " fòô\n bàř")
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
*/
|
||||
public function testToTabs($expected, $string, $tabLength = 4)
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
$result = S::toTabs($string, $tabLength);
|
||||
$result = S::create($str)->toTabs($tabLength);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForToTabs()
|
||||
{
|
||||
$testData = array(
|
||||
array(' foo bar ', ' foo bar '),
|
||||
array(' foo bar ', ' foo bar ', 5),
|
||||
array(' foo bar ', ' foo bar ', 2),
|
||||
array(" foo\n bar", " foo\n bar"),
|
||||
array(" fòô\n bàř", " fòô\n bàř")
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
*/
|
||||
public function testSlugify($expected, $string)
|
||||
public function testSlugify($expected, $str)
|
||||
{
|
||||
$result = S::slugify($string);
|
||||
$result = S::create($str)->slugify();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForSlugify()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo-bar', ' foo bar '),
|
||||
array('foo-dbar', " Foo d'Bar "),
|
||||
array('a-string-with-dashes', 'A string-with-dashes'),
|
||||
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
|
||||
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
|
||||
array('numbers-1234', 'numbers 1234')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle, $encoding = null)
|
||||
{
|
||||
$result = S::contains($haystack, $needle, $encoding);
|
||||
$result = S::create($haystack, $encoding)->contains($needle);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForContains()
|
||||
{
|
||||
$testData = array(
|
||||
array(true, 'This string contains foo bar', 'foo bar'),
|
||||
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
|
||||
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'),
|
||||
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'),
|
||||
array(false, 'This string contains foo bar', 'Foo bar'),
|
||||
array(false, 'This string contains foo bar', 'foobar'),
|
||||
array(false, 'This string contains foo bar', 'foo bar '),
|
||||
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'),
|
||||
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
*/
|
||||
public function testSurround($expected, $string, $substring)
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
$result = S::surround($string, $substring);
|
||||
$result = S::create($str)->surround($substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForSurround()
|
||||
{
|
||||
$testData = array(
|
||||
array('__foobar__', 'foobar', '__'),
|
||||
array('test', 'test', ''),
|
||||
array('**', '', '*'),
|
||||
array('¬fòô bàř¬', 'fòô bàř', '¬'),
|
||||
array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
*/
|
||||
public function testInsert($expected, $string, $substring, $index,
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::insert($string, $substring, $index, $encoding);
|
||||
$result = S::create($str, $encoding)->insert($substring, $index);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForInsert()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar', 'oo bar', 'f', 0),
|
||||
array('foo bar', 'f bar', 'oo', 1),
|
||||
array('f bar', 'f bar', 'oo', 20),
|
||||
array('foo bar', 'foo ba', 'r', 6),
|
||||
array('fòô bàř', 'òô bàř', 'f', 0, 'UTF-8'),
|
||||
array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'),
|
||||
array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
*/
|
||||
public function testSafeTruncate($expected, $string, $length, $substring = '',
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::safeTruncate($string, $length, $substring, $encoding);
|
||||
$result = S::create($str, $encoding)->safeTruncate($length, $substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForSafeTruncate()
|
||||
{
|
||||
$testData = array(
|
||||
array('Test foo bar', 'Test foo bar', 12),
|
||||
array('Test foo', 'Test foo bar', 11),
|
||||
array('Test foo', 'Test foo bar', 8),
|
||||
array('Test', 'Test foo bar', 7),
|
||||
array('Test', 'Test foo bar', 4),
|
||||
array('Test foo bar', 'Test foo bar', 12, '...'),
|
||||
array('Test foo...', 'Test foo bar', 11, '...'),
|
||||
array('Test...', 'Test foo bar', 8, '...'),
|
||||
array('Test...', 'Test foo bar', 7, '...'),
|
||||
array('...', 'Test foo bar', 4, '...'),
|
||||
array('Test....', 'Test foo bar', 11, '....'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'),
|
||||
array('Test fòô', 'Test fòô bàř', 11, '', 'UTF-8'),
|
||||
array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'),
|
||||
array('Test', 'Test fòô bàř', 7, '', 'UTF-8'),
|
||||
array('Test', 'Test fòô bàř', 4, '', 'UTF-8'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'),
|
||||
array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
|
||||
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
*/
|
||||
public function testReverse($expected, $string, $encoding = null)
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::reverse($string, $encoding);
|
||||
$result = S::create($str, $encoding)->reverse();
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForReverse()
|
||||
{
|
||||
$testData = array(
|
||||
array('', ''),
|
||||
array('raboof', 'foobar'),
|
||||
array('řàbôòf', 'fòôbàř', 'UTF-8'),
|
||||
array('řàb ôòf', 'fòô bàř', 'UTF-8'),
|
||||
array('∂∆ ˚åß', 'ßå˚ ∆∂', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
*/
|
||||
public function testShuffle($string, $encoding = null)
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
// We'll just make sure that the chars are present before/after shuffle
|
||||
$result = S::shuffle($string, $encoding);
|
||||
$this->assertEquals(count_chars($string), count_chars($result));
|
||||
$result = S::create($str, $encoding)->shuffle();
|
||||
$this->assertEquals(count_chars($str), count_chars($result));
|
||||
}
|
||||
|
||||
public function stringsForShuffle()
|
||||
{
|
||||
$testData = array(
|
||||
array('foo bar'),
|
||||
array('∂∆ ˚åß', 'UTF-8'),
|
||||
array('å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user