1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-31 16:51:57 +02:00

42 Commits

Author SHA1 Message Date
Daniel St. Jules
6ba29637b2 Updated changelog for 1.2.2 2013-12-04 23:36:17 -05:00
Daniel St. Jules
2ada00ff62 Add logo 2013-12-01 15:43:03 -05:00
Daniel St. Jules
2a197244a5 Update readme and comments for new slugify param 2013-11-27 01:14:09 -05:00
Daniel St. Jules
8b1a91fe0c Merge pull request #15 from glynnforrest/slug
Adding optional $replacement parameter to slugify.
2013-11-26 22:03:02 -08:00
Glynn Forrest
906f217dc1 Adding optional $replacement parameter to slugify. 2013-11-26 14:11:41 +00:00
Daniel St. Jules
d4baab7583 Merge pull request #14 from sergebezborodov/master
Use late static bindings in Stringy::create
2013-11-05 20:00:18 -08:00
Serge Bezborodov
0c8f0e9083 changes in create function 2013-11-01 09:55:38 +02:00
Daniel St. Jules
2155447ce8 Updated changelog for 1.2.1 2013-10-11 01:19:53 -04:00
Daniel St. Jules
77efce3a38 Added homepage to composer.json 2013-10-11 01:17:17 -04:00
Daniel St. Jules
f6085ed2cb Cleanup tests a bit
Getting over my irrational fear of multi-line return statements
2013-09-27 20:13:04 -04:00
Daniel St. Jules
fa36291254 Updated changelog for 1.2.0 2013-09-15 00:17:51 -04:00
Daniel St. Jules
88145e83e8 Update replace() to correctly apply preg_quote to pattern and replacement 2013-09-14 23:59:46 -04:00
Daniel St. Jules
9bd5a9c0c8 Added regexReplace() 2013-09-14 23:45:18 -04:00
Daniel St. Jules
41ea0277b2 Added $caseSensitive to count() 2013-09-14 14:58:11 -04:00
Daniel St. Jules
d862f9c24c Fix toUpperCase and toLowerCase not being static in StaticStringy 2013-09-14 11:31:51 -04:00
Daniel St. Jules
61cd5f5f4d Added toLowerCase() and toUpperCase() 2013-09-14 11:22:52 -04:00
Daniel St. Jules
ea20aebc91 Added $caseSensitive to contains() 2013-09-14 01:32:52 -04:00
Daniel St. Jules
49e1c5ad2a Merge pull request #13 from pborreli/typos
Closes Issue 13: Fixed typo in PHPDoc
2013-09-06 05:18:22 -07:00
Pascal Borreli
90913d39bc Fixed typos/PHPDoc 2013-09-06 08:44:42 +01:00
Daniel St. Jules
02da00c433 Merge branch 'pr/12' 2013-09-05 22:41:19 -04:00
Borales
07f23d6165 Adding cyrillic letters 2013-09-04 18:23:50 +03:00
Daniel St. Jules
d27683b570 Merge pull request #10 from ChristianGaertner/patch-2
Issue 10: Fixed Exception in pad() and added test coverage
2013-09-04 05:38:41 -07:00
ChristianGaertner
bc175f0e12 Added test to cover 100% + fixed fatal error 2013-09-04 08:02:56 +02:00
Daniel St. Jules
a6964e4561 Merge pull request #9 from ChristianGaertner/patch-1
Wrapped some things into code blocks
2013-09-03 14:13:28 -07:00
ChristianGaertner
5fff1a4124 Added codeblocks 2013-09-03 22:51:50 +02:00
Daniel St. Jules
545a5aec5f Updated changelog for 1.1.0 2013-08-31 19:34:28 -04:00
Daniel St. Jules
63298f36a8 Cleanup comments, release 1.1.0 2013-08-31 19:30:47 -04:00
Daniel St. Jules
a667c4aa6a Fix and merge branch 'isJson' 2013-08-31 19:12:46 -04:00
Lucas
cd5e897ac3 isJson() function, with tests 2013-08-31 18:43:16 -04:00
Daniel St. Jules
390671e131 Cleanup and merge branch 'isserialized' 2013-08-31 18:37:58 -04:00
Lucas
0b76c56333 function isSerialized(), with tests 2013-08-31 18:20:39 -04:00
Daniel St. Jules
5f35226926 Merge branch 'better-construct', update testConstruct and create 2013-08-27 01:34:44 -04:00
Daniel St. Jules
11f961cae6 Fixed testShuffle. It now tests that each mb char has the same number of occurrences before and after 2013-08-24 11:19:33 -04:00
Lucas
660b1d6de8 Added simple constructor test 2013-08-24 14:32:43 +02:00
Lucas
3d81e2ef70 Update create() and added __construct() 2013-08-24 14:17:16 +02:00
Daniel St. Jules
454e8e2f87 Added isHexadecimal to Readme 2013-08-23 01:08:31 -04:00
Daniel St. Jules
89c292c041 Merge pull request #2 from semalead/isHeaxdecimal
Added isHexadecimal
2013-08-22 21:51:27 -07:00
Lucas
a1d9787309 Added isHexadecimal 2013-08-22 15:40:43 +02:00
Daniel St. Jules
f1abc38c17 Fix for collapseWhitespace() not being able to handle multibyte space characters 2013-08-06 22:39:47 -04:00
Daniel St. Jules
7a64dad935 Remove dummy test from CommonTest.php 2013-08-06 22:16:53 -04:00
Daniel St. Jules
9d4ca96528 Removed packagist icon 2013-08-01 01:04:21 -04:00
Daniel St. Jules
f0063f8bac Cleanup and fixes for 1.0.0 Release 2013-08-01 00:53:54 -04:00
8 changed files with 1380 additions and 714 deletions

View File

@@ -1,3 +1,40 @@
### 1.2.2 (2013-12-04)
* Updated create function to use late static binding
* Added optional $replacement param to slugify
### 1.2.1 (2013-10-11)
* Cleaned up tests
* Added homepage to composer.json
### 1.2.0 (2013-09-15)
* Fixed pad's use of InvalidArgumentException
* Fixed replace(). It now correctly treats regex special chars as normal chars
* Added additional Cyrillic letters to toAscii
* Added $caseSensitive to contains() and count()
* Added toLowerCase()
* Added toUpperCase()
* Added regexReplace()
### 1.1.0 (2013-08-31)
* Fix for collapseWhitespace()
* Added isHexadecimal()
* Added constructor to Stringy\Stringy
* Added isSerialized()
* Added isJson()
### 1.0.0 (2013-08-1)
* 1.0.0 release
* Added test coverage for Stringy::create and method chaining
* Added tests for returned type
* Fixed StaticStringy::replace(). It was returning a Stringy object instead of string
* Renamed standardize() to the more appropriate toAscii()
* Cleaned up comments and README
### 1.0.0-rc.1 (2013-07-28)
* Release candidate

302
README.md
View File

@@ -1,10 +1,8 @@
# Stringy
![Stringy](http://danielstjules.com/stringy/logo.png)
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)
[![Total Downloads](https://poser.pugx.org/danielstjules/Stringy/downloads.png)](https://packagist.org/packages/danielstjules/stringy)
[![Latest Stable Version](https://poser.pugx.org/danielstjules/Stringy/v/stable.png)](https://packagist.org/packages/danielstjules/stringy)
* [Requiring/Loading](#requiringloading)
* [OO and Procedural](#oo-and-procedural)
@@ -25,7 +23,10 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [isAlpha](#isalpha)
* [isAlphanumeric](#isalphanumeric)
* [isBlank](#isblank)
* [isHexadecimal](#ishexadecimal)
* [isJson](#isjson)
* [isLowerCase](#islowercase)
* [isSerialized](#isserialized)
* [isUpperCase](#isuppercase)
* [last](#last)
* [length](#length)
@@ -37,6 +38,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [padBoth](#padboth)
* [padLeft](#padleft)
* [padRight](#padright)
* [regexReplace](#regexreplace)
* [removeLeft](#removeleft)
* [removeRight](#removeright)
* [replace](#replace)
@@ -44,15 +46,17 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [safeTruncate](#safetruncate)
* [shuffle](#shuffle)
* [slugify](#slugify)
* [standardize](#standardize)
* [startsWith](#startswith)
* [substr](#substr)
* [surround](#surround)
* [swapCase](#swapcase)
* [tidy](#tidy)
* [titleize](#titleize)
* [toAscii](#toascii)
* [toLowerCase](#tolowercase)
* [toSpaces](#tospaces)
* [toTabs](#totabs)
* [toUpperCase](#touppercase)
* [trim](#trim)
* [truncate](#truncate)
* [underscored](#underscored)
@@ -99,7 +103,7 @@ of the former is the following:
```php
use Stringy\Stringy as S;
echo S::create("Fòô Bàř", 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
```
`Stringy\Stringy` contains a __toString() method, which returns the current
@@ -110,16 +114,16 @@ Using the static wrapper, an alternative is the following:
```php
use Stringy\StaticStringy as S;
$string = S::collapseWhitespace("Fòô Bàř", 'UTF-8');
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
```
## Methods
In the list below, any static method other than S::create refers to a
method in Stringy\StaticStringy. For all others, they're found in Stringy\Stringy. Furthermore, all methods that return an object of type Stringy return a new object, and do not modify the original.
method in `Stringy\StaticStringy`. For all others, they're found in `Stringy\Stringy`. Furthermore, all methods that return a Stringy object or string do not modify the original.
*Note: If $encoding is not given, it defaults to mb_internal_encoding().*
*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*
#### at
@@ -127,7 +131,7 @@ $stringy->at(int $index)
S::substr(int $index [, string $encoding ])
Gets the character of $str at $index, with indexes starting at 0.
Returns the character of the string at $index, with indexes starting at 0.
```php
S::create('fòô bàř', 'UTF-8')->at(6);
@@ -140,9 +144,9 @@ $stringy->camelize();
S::camelize(string $str [, string $encoding ])
Returns a camelCase version of a supplied string, with multibyte support.
Trims surrounding spaces, capitalizes letters following digits, spaces,
dashes and underscores, and removes spaces, dashes, underscores.
Returns a camelCase version of the supplied string. Trims surrounding
spaces, capitalizes letters following digits, spaces, dashes and
underscores, and removes spaces, dashes, underscores.
```php
S::create('Camel-Case')->camelize();
@@ -153,10 +157,11 @@ S::camelize('Camel-Case'); // 'camelCase'
$stringy->collapseWhitespace()
S::collapseWhitespace(string $str)
S::collapseWhitespace(string $str [, string $encoding ])
Trims the string and replaces consecutive whitespace characters with a
single space. This inclues tabs and newline characters.
single space. This includes tabs and newline characters, as well as
multibyte whitespace such as the thin space and ideographic space.
```php
S::create(' Ο συγγραφέας ')->collapseWhitespace();
@@ -165,11 +170,13 @@ S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγρα
#### contains
$stringy->contains(string $needle)
$stringy->contains(string $needle [, boolean $caseSensitive = true ])
S::contains(string $haystack, string $needle [, string $encoding ])
S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]])
Returns true if $haystack contains $needle, false otherwise.
Returns true if the string contains $needle, false otherwise. By default,
the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
@@ -178,12 +185,13 @@ S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8')
#### count
$stringy->count(string $substring)
$stringy->count(string $substring [, boolean $caseSensitive = true ])
S::count(string $str, string $substring [, string $encoding ])
S::count(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
Returns the number of occurences of $substring in $str. An alias for
mb_substr_count()
Returns the number of occurrences of $substring in the given string.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
S::create('Ο συγγραφέας είπε', 'UTF-8')->count('α');
@@ -192,14 +200,14 @@ S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
#### create
$stringy = S::create(string $str, [, $encoding ])
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.
mb_internal_encoding(). It then returns the initialized object.
```php
S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
$stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
```
#### dasherize
@@ -208,10 +216,9 @@ $stringy->dasherize();
S::dasherize(string $str [, string $encoding ])
Returns a lowercase and trimmed string seperated by dashes, with
multibyte support. 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.
Returns a lowercase and trimmed string separated 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.
```php
S::create('TestDCase')->dasherize();
@@ -224,9 +231,9 @@ $stringy->endsWith(string $substring [, boolean $caseSensitive = true ])
S::endsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
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 the string ends with $substring, false otherwise. 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);
@@ -239,7 +246,7 @@ $stringy->ensureLeft(string $substring)
S::ensureLeft(string $substring [, string $encoding ])
Ensures that $str begins with $substring.
Ensures that the string begins with $substring. If it doesn't, it's prepended.
```php
S::create('foobar')->ensureLeft('http://');
@@ -252,7 +259,7 @@ $stringy->ensureRight(string $substring)
S::ensureRight(string $substring [, string $encoding ])
Ensures that $str ends with $substring.
Ensures that the string begins with $substring. If it doesn't, it's appended.
```php
S::create('foobar')->ensureRight('.com');
@@ -265,7 +272,7 @@ $stringy->first(int $n)
S::first(int $n [, string $encoding ])
Gets the first $n characters of $str.
Returns the first $n characters of the string.
```php
S::create('fòô bàř', 'UTF-8')->first(3);
@@ -278,8 +285,8 @@ $stringy->humanize()
S::humanize(string $str [, string $encoding ])
Capitalizes the first word of a string, replaces underscores with spaces,
and strips '_id'.
Capitalizes the first word of the string, replaces underscores with
spaces, and strips '_id'.
```php
S::create('author_id')->humanize();
@@ -292,7 +299,7 @@ $stringy->insert(int $index, string $substring)
S::insert(string $str, int $index, string $substring [, string $encoding ])
Inserts $substring into $str at the $index provided.
Inserts $substring into the string at the $index provided.
```php
S::create('fòô bà', 'UTF-8')->insert('ř', 6);
@@ -305,7 +312,7 @@ $stringy->isAlpha()
S::isAlpha(string $str [, string $encoding ])
Returns true if $str contains only alphabetic chars, false otherwise.
Returns true if the string contains only alphabetic chars, false otherwise.
```php
S::create('丹尼爾', 'UTF-8')->isAlpha();
@@ -318,7 +325,7 @@ $stringy->isAlphanumeric()
S::isAlphanumeric(string $str [, string $encoding ])
Returns true if $str contains only alphabetic and numeric chars, false
Returns true if the string contains only alphabetic and numeric chars, false
otherwise.
```php
@@ -332,37 +339,76 @@ $stringy->isBlank()
S::isBlank(string $str [, string $encoding ])
Returns true if $str contains only whitespace chars, false otherwise.
Returns true if the string contains only whitespace chars, false otherwise.
```php
S::create("\n\t \v\f")->isBlank();
S::isBlank("\n\t \v\f"); // true
```
#### isHexadecimal
$stringy->isHexadecimal()
S::isHexadecimal(string $str [, string $encoding ])
Returns true if the string contains only hexadecimal chars, false otherwise.
```php
S::create('A102F')->isHexadecimal();
S::isHexadecimal('A102F'); // true
```
#### isJson
$stringy->isJson()
S::isJson(string $str [, string $encoding ])
Returns true if the string is JSON, false otherwise.
```php
S::create('{"foo":"bar"}')->isJson();
S::isJson('{"foo":"bar"}'); // true
```
#### isLowerCase
$stringy->isLowerCase()
S::isLowerCase(string $str [, string $encoding ])
Returns true if $str contains only lower case chars, false otherwise.
Returns true if the string contains only lower case chars, false otherwise.
```php
S::create('fòô bàř', 'UTF-8')->isLowerCase();
S::isLowerCase('fòô bàř', 'UTF-8'); // true
```
#### isSerialized
$stringy->isSerialized()
S::isSerialized(string $str [, string $encoding ])
Returns true if the string is serialized, false otherwise.
```php
S::create('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8')->isSerialized();
S::isSerialized('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'); // true
```
#### isUpperCase
$stringy->isUpperCase()
S::isUpperCase(string $str [, string $encoding ])
Returns true if $str contains only upper case chars, false otherwise.
Returns true if the string contains only upper case chars, false otherwise.
```php
S::create('FÒÔBÀŘ',, 'UTF-8')->isUpperCase();
S::isUpperCase('FÒÔBÀŘ',, 'UTF-8'); // true
S::create('FÒÔBÀŘ', 'UTF-8')->isUpperCase();
S::isUpperCase('FÒÔBÀŘ', 'UTF-8'); // true
```
#### last
@@ -371,7 +417,7 @@ $stringy->last(int $n)
S::last(int $n [, string $encoding ])
Gets the last $n characters of $str.
Returns the last $n characters of the string.
```php
S::create('fòô bàř', 'UTF-8')->last(3);
@@ -384,7 +430,7 @@ $stringy->length()
S::length(string $str [, string $encoding ])
Returns the length of $str. An alias for PHP's mb_strlen() function.
Returns the length of the string. An alias for PHP's mb_strlen() function.
```php
S::create('fòô bàř', 'UTF-8')->length();
@@ -397,7 +443,7 @@ $stringy->longestCommonPrefix(string $otherStr)
S::longestCommonPrefix(string $str, string $otherStr [, $encoding ])
Finds the longest common prefix between $str and $otherStr.
Returns the longest common prefix between the string and $otherStr.
```php
S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar');
@@ -410,7 +456,7 @@ $stringy->longestCommonSuffix(string $otherStr)
S::longestCommonSuffix(string $str, string $otherStr [, $encoding ])
Finds the longest common suffix between $str and $otherStr.
Returns the longest common suffix between the string and $otherStr.
```php
S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř');
@@ -423,8 +469,8 @@ $stringy->longestCommonSubstring(string $otherStr)
S::longestCommonSubstring(string $str, string $otherStr [, $encoding ])
Finds the longest common substring between $str and $otherStr. In the
case of ties, returns that which occurs first.
Returns the longest common substring between the string and $otherStr. In the
case of ties, it returns that which occurs first.
```php
S::create('foo bar')->longestCommonSubstring('boo far');
@@ -437,8 +483,7 @@ $stringy->lowerCaseFirst();
S::lowerCaseFirst(string $str [, string $encoding ])
Converts the first character of the supplied string to lower case, with
support for multibyte strings.
Converts the first character of the supplied string to lower case.
```php
S::create('Σ test', 'UTF-8')->lowerCaseFirst();
@@ -451,11 +496,11 @@ $stringy->pad(int $length [, string $padStr = ' ' [, string $padType = 'right' ]
S::pad(string $str , int $length [, string $padStr = ' ' [, string $padType = 'right' [, string $encoding ]]])
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.
Pads the string to a given length with $padStr. If length is less than
or equal to the length of the string, 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 InvalidArgumentException if
$padType isn't one of those 3 values.
```php
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
@@ -469,7 +514,7 @@ $stringy->padBoth(int $length [, string $padStr = ' ' ])
S::padBoth(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
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)
string are padded. Alias for pad() with a $padType of 'both'.
```php
S::create('foo bar')->padBoth(9, ' ');
@@ -483,7 +528,7 @@ $stringy->padLeft(int $length [, string $padStr = ' ' ])
S::padLeft(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
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)
string is padded. Alias for pad() with a $padType of 'left'.
```php
S::create($str, $encoding)->padLeft($length, $padStr);
@@ -497,20 +542,36 @@ $stringy->padRight(int $length [, string $padStr = ' ' ])
S::padRight(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
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)
padded. Alias for pad() with a $padType of 'right'.
```php
S::create('foo bar')->padRight(10, '_*');
S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
```
#### regexReplace
$stringy->regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
S::regexReplace(string $str, string $pattern, string $replacement [, string $options = 'msr' [, string $encoding ]])
Replaces all occurrences of $pattern in $str by $replacement. An alias
for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
in the bundled version of Oniguruma in PHP 5.3.
```php
S::create('fòô ', 'UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
```
#### removeLeft
$stringy->removeLeft(string $substring)
S::removeLeft(string $str, string $substring [, string $encoding ])
Removes the prefix $substring if present.
Returns a new string with the prefix $substring removed, if it was present.
```php
S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
@@ -523,7 +584,7 @@ $stringy->removeRight(string $substring)
S::removeRight(string $str, string $substring [, string $encoding ])
Removes the suffix $substring if present.
Returns a new string with the suffix $substring removed, if it was present.
```php
S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
@@ -532,11 +593,11 @@ S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
#### replace
$stringy->replace(string $search, string $replace)
$stringy->replace(string $search, string $replacement)
S::replace(string $str, string $search, string $replace [, string $encoding ])
S::replace(string $str, string $search, string $replacement [, string $encoding ])
Replaces all occurrences of $search with $replace in $str.
Replaces all occurrences of $search in $str by $replacement.
```php
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
@@ -549,7 +610,7 @@ $stringy->reverse()
S::reverse(string $str, [, string $encoding ])
Reverses a string. A multibyte version of strrev.
Returns a reversed string. A multibyte version of strrev().
```php
S::create('fòô bàř', 'UTF-8')->reverse();
@@ -578,8 +639,7 @@ $stringy->shuffle()
S::shuffle(string $str [, string $encoding ])
A multibyte str_shuffle function. It randomizes the order of characters
in a string.
A multibyte str_shuffle() function. It returns a string with its characters in random order.
```php
S::create('fòô bàř', 'UTF-8')->shuffle();
@@ -588,40 +648,28 @@ S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
#### slugify
$stringy->slugify()
$stringy->slugify([ string $replacement = '-' ])
S::slugify(string $str)
S::slugify(string $str [, string $replacement = '-' ])
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 the string 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 $replacement.
The replacement defaults to a single dash, and 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'
```
#### standardize
$stringy->standardize()
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'
```
#### startsWith
$stringy->startsWith(string $substring [, boolean $caseSensitive = true ])
S::startsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
Returns true if the string $str begins with $substring, false otherwise.
Returns true if the string begins with $substring, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
@@ -636,7 +684,7 @@ $stringy->substr(int $start [, int $length ])
S::substr(string $str, int $start [, int $length [, string $encoding ]])
Gets the substring of $str beginning at $start with the specified $length.
Returns the substring beginning at $start with the specified $length.
It differs from the mb_substr() function in that providing a $length of
null will return the rest of the string, rather than an empty string.
@@ -664,7 +712,7 @@ $stringy->swapCase();
S::swapCase(string $str [, string $encoding ])
Returns a case swapped version of a string.
Returns a case swapped version of the string.
```php
S::create('Ντανιλ', 'UTF-8')->swapCase();
@@ -677,8 +725,8 @@ $stringy->tidy()
S::tidy(string $str)
Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
(and commonly used in Word documents) with their ASCII equivalents.
Returns a string with smart quotes, ellipsis characters, and dashes from
Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
```php
S::create('“I see…”')->tidy();
@@ -691,7 +739,7 @@ $stringy->titleize([ string $encoding ])
S::titleize(string $str [, array $ignore [, string $encoding ]])
Capitalizes the first letter of each word in a string, after trimming.
Returns a trimmed string with the first letter of each word capitalized.
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.
@@ -703,13 +751,41 @@ S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
// 'I Like to Watch DVDs at Home'
```
#### toAscii
$stringy->toAscii()
S::toAscii(string $str)
Returns an ASCII version of the string. A set of non-ASCII characters are
replaced with their closest ASCII counterparts, and the rest are removed.
```php
S::create('fòô bàř')->toAscii();
S::toAscii('fòô bàř'); // 'foo bar'
```
#### toLowerCase
$stringy->toLowerCase()
S::toLowerCase(string $str [, string $encoding ])
Converts all characters in the string to lowercase. An alias for PHP's
mb_strtolower().
```php
S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase();
S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř'
```
#### toSpaces
$stringy->toSpaces([ tabLength = 4 ])
S::toSpaces(string $str, [, int $tabLength = 4 ])
Converts each tab in a string to some number of spaces, as defined by
Converts each tab in the string to some number of spaces, as defined by
$tabLength. By default, each tab is converted to 4 consecutive spaces.
```php
@@ -723,7 +799,7 @@ $stringy->toTabs([ tabLength = 4 ])
S::toTabs(string $str, [, int $tabLength = 4 ])
Converts each occurence of some consecutive number of spaces, as defined
Converts each occurrence of some consecutive number of spaces, as defined
by $tabLength, to a tab. By default, each 4 consecutive spaces are
converted to a tab.
@@ -732,13 +808,27 @@ S::create(' fòô bàř')->toTabs();
S::toTabs(' fòô bàř'); // ' fòô bàř'
```
#### toUpperCase
$stringy->toUpperCase()
S::toUpperCase(string $str [, string $encoding ])
Converts all characters in the string to uppercase. An alias for PHP's
mb_strtoupper().
```php
S::create('fòô bàř', 'UTF-8')->toUpperCase();
S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ'
```
#### trim
$stringy->trim()
S::trim(string $str)
Trims $str. An alias for PHP's trim() function.
Returns the trimmed string. An alias for PHP's trim() function.
```php
S::create('fòô bàř', 'UTF-8')->trim();
@@ -751,7 +841,7 @@ $stringy->truncate(int $length, [, string $substring = '' ])
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
Truncates $str to a given length. If $substring is provided, and
Truncates the string to a given length. If $substring is provided, and
truncating occurs, the string is further truncated so that the substring
may be appended without exceeding the desired length.
@@ -766,10 +856,9 @@ $stringy->underscored();
S::underscored(string $str [, string $encoding ])
Returns a lowercase and trimmed string seperated by underscores, with
multibyte support. 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.
Returns a lowercase and trimmed string separated 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.
```php
S::create('TestUCase')->underscored();
@@ -782,9 +871,9 @@ $stringy->upperCamelize();
S::upperCamelize(string $str [, string $encoding ])
Returns an UpperCamelCase version of a supplied string, with multibyte
support. Trims surrounding spaces, capitalizes letters following digits,
spaces, dashes and underscores, and removes spaces, dashes, underscores.
Returns an UpperCamelCase version of the supplied string. It trims
surrounding spaces, capitalizes letters following digits, spaces, dashes
and underscores, and removes spaces, dashes, underscores.
```php
S::create('Upper Camel-Case')->upperCamelize();
@@ -797,8 +886,7 @@ $stringy->upperCaseFirst();
S::upperCaseFirst(string $str [, string $encoding ])
Converts the first character of the supplied string to upper case, with
support for multibyte strings.
Converts the first character of the supplied string to upper case.
```php
S::create('σ test', 'UTF-8')->upperCaseFirst();

View File

@@ -5,6 +5,7 @@
"multibyte", "string", "manipulation", "utility", "methods", "utf-8",
"helpers", "utils", "utf"
],
"homepage": "https://github.com/danielstjules/Stringy",
"license": "MIT",
"authors": [
{

View File

@@ -29,7 +29,7 @@ class StaticStringy
}
/**
* Returns a camelCase version of a supplied string. Trims surrounding
* Returns a camelCase version of the supplied string. Trims surrounding
* spaces, capitalizes letters following digits, spaces, dashes and
* underscores, and removes spaces, dashes, underscores.
*
@@ -43,9 +43,9 @@ class StaticStringy
}
/**
* Returns an UpperCamelCase version of a supplied string. Trims surrounding
* spaces, capitalizes letters following digits, spaces, dashes and
* underscores, and removes spaces, dashes, underscores.
* Returns an UpperCamelCase version of the supplied string. It 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
@@ -57,7 +57,7 @@ class StaticStringy
}
/**
* Returns a lowercase and trimmed string seperated by dashes. Dashes are
* Returns a lowercase and trimmed string separated 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.
*
@@ -71,7 +71,7 @@ class StaticStringy
}
/**
* Returns a lowercase and trimmed string seperated by underscores.
* Returns a lowercase and trimmed string separated 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.
@@ -86,7 +86,7 @@ class StaticStringy
}
/**
* Returns a case swapped version of a string.
* Returns a case swapped version of the string.
*
* @param string $str String to swap case
* @param string $encoding The character encoding
@@ -98,7 +98,7 @@ class StaticStringy
}
/**
* Capitalizes the first letter of each word in a string, after trimming.
* Returns a trimmed string with the first letter of each word capitalized.
* 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.
@@ -114,8 +114,8 @@ class StaticStringy
}
/**
* Capitalizes the first word of a string, replaces underscores with spaces,
* and strips '_id'.
* Capitalizes the first word of the string, replaces underscores with
* spaces, and strips '_id'.
*
* @param string $str String to humanize
* @param string $encoding The character encoding
@@ -127,11 +127,11 @@ class StaticStringy
}
/**
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
* (and commonly used in Word documents) with their ASCII equivalents.
* Returns a string with smart quotes, ellipsis characters, and dashes from
* Windows-1252 (commonly used in Word documents) replaced by 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)
@@ -141,33 +141,36 @@ class StaticStringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This inclues tabs and newline characters.
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @param string $str The string to cleanup whitespace
* @param string $str The string to cleanup whitespace
* @param string $encoding The character encoding
* @return string The trimmed string with condensed whitespace
*/
public static function collapseWhitespace($str)
public static function collapseWhitespace($str, $encoding = null)
{
return $result = Stringy::create($str)->collapseWhitespace()->str;
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
}
/**
* Converts some non-ASCII characters to their closest ASCII counterparts.
* Returns an ASCII version of the string. A set of non-ASCII characters are
* replaced with their closest ASCII counterparts, and the rest are removed.
*
* @param string $str A string with non-ASCII characters
* @return string The string after the replacements
* @return string A string containing only ASCII characters
*/
public static function standardize($str)
public static function toAscii($str)
{
return $result = Stringy::create($str)->standardize()->str;
return $result = Stringy::create($str)->toAscii()->str;
}
/**
* 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.
* Pads the string to a given length with $padStr. If length is less than
* or equal to the length of the string, 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 InvalidArgumentException if
* $padType isn't one of those 3 values.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
@@ -187,7 +190,7 @@ class StaticStringy
/**
* 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)
* string is padded. Alias for pad() with a $padType of 'left'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
@@ -202,7 +205,7 @@ class StaticStringy
/**
* 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)
* padded. Alias for pad() with a $padType of 'right'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
@@ -217,7 +220,7 @@ class StaticStringy
/**
* 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)
* string are padded. Alias for pad() with a $padType of 'both'.
*
* @param string $str String to pad
* @param int $length Desired string length after padding
@@ -231,13 +234,13 @@ class StaticStringy
}
/**
* Returns true if the string $str begins with $substring, false otherwise.
* Returns true if the string 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 bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str starts with $substring
*/
@@ -249,13 +252,13 @@ class StaticStringy
}
/**
* 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 the string 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 bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $str ends with $substring
*/
@@ -267,7 +270,7 @@ class StaticStringy
}
/**
* Converts each tab in a string to some number of spaces, as defined by
* Converts each tab in the 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
@@ -280,7 +283,7 @@ class StaticStringy
}
/**
* Converts each occurence of some consecutive number of spaces, as defined
* Converts each occurrence of some consecutive number of spaces, as defined
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
* converted to a tab.
*
@@ -294,30 +297,62 @@ class StaticStringy
}
/**
* 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 all characters in the string to lowercase. An alias for PHP's
* mb_strtolower().
*
* @param string $str Text to transform into an URL slug
* @return string The corresponding URL slug
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The lowercase string
*/
public static function slugify($str)
public static function toLowerCase($str, $encoding = null)
{
return Stringy::create($str)->slugify()->str;
return Stringy::create($str, $encoding)->toLowerCase()->str;
}
/**
* Returns true if $haystack contains $needle, false otherwise.
* Converts all characters in the string to uppercase. An alias for PHP's
* mb_strtoupper().
*
* @param string $haystack String being checked
* @param string $needle Substring to look for
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The uppercase string
*/
public static function toUpperCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->toUpperCase()->str;
}
/**
* Converts the string 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 $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $str Text to transform into an URL slug
* @param string $replacement The string used to replace whitespace
* @return string The corresponding URL slug
*/
public static function slugify($str, $replacement = '-')
{
return Stringy::create($str)->slugify($replacement)->str;
}
/**
* Returns true if the string contains $needle, false otherwise. By default,
* the comparison is case-sensitive, but can be made insensitive by setting
* $caseSensitive to false.
*
* @param string $haystack String being checked
* @param string $needle Substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return bool Whether or not $haystack contains $needle
*/
public static function contains($haystack, $needle, $encoding = null)
public static function contains($haystack, $needle, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($haystack, $encoding)->contains($needle);
return Stringy::create($haystack, $encoding)->contains($needle, $caseSensitive);
}
/**
@@ -333,7 +368,7 @@ class StaticStringy
}
/**
* Inserts $substring into $str at the $index provided.
* Inserts $substring into the string at the $index provided.
*
* @param string $str String to insert into
* @param string $substring String to be inserted
@@ -347,14 +382,14 @@ class StaticStringy
}
/**
* Truncates $str to a given length. If $substring is provided, and
* Truncates the string to a given length. 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
* @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 truncate($str, $length, $substring = '',
@@ -370,10 +405,10 @@ class StaticStringy
* 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
* @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 = '',
@@ -384,10 +419,10 @@ class StaticStringy
}
/**
* Reverses a string. A multibyte version of strrev.
* Returns a reversed string. A multibyte version of strrev().
*
* @param string $str String to reverse
* @param string $encoding The character encoding
* @param string $str String to reverse
* @param string $encoding The character encoding
* @return string The reversed string
*/
public static function reverse($str, $encoding = null)
@@ -396,8 +431,8 @@ class StaticStringy
}
/**
* A multibyte str_shuffle function. It randomizes the order of characters
* in a string.
* A multibyte str_shuffle() function. It returns a string with its
* characters in random order.
*
* @param string $str String to shuffle
* @param string $encoding The character encoding
@@ -409,7 +444,7 @@ class StaticStringy
}
/**
* Trims $str. An alias for PHP's trim() function.
* Returns the trimmed string. An alias for PHP's trim() function.
*
* @param string $str String to trim
* @return string Trimmed $str
@@ -420,7 +455,7 @@ class StaticStringy
}
/**
* Finds the longest common prefix between $str and $otherStr.
* Returns the longest common prefix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
@@ -434,7 +469,7 @@ class StaticStringy
}
/**
* Finds the longest common suffix between $str and $otherStr.
* Returns the longest common suffix between the string and $otherStr.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
@@ -448,8 +483,8 @@ class StaticStringy
}
/**
* Finds the longest common substring between $str and $otherStr. In the
* case of ties, returns that which occurs first.
* Returns the longest common substring between the string and $otherStr.
* In the case of ties, it returns that which occurs first.
*
* @param string $str First string for comparison
* @param string $otherStr Second string for comparison
@@ -464,7 +499,7 @@ class StaticStringy
}
/**
* Returns the length of $str. An alias for PHP's mb_strlen() function.
* Returns the length of the string. An alias for PHP's mb_strlen() function.
*
* @param string $str The string to get the length of
* @param string $encoding The character encoding
@@ -476,13 +511,13 @@ class StaticStringy
}
/**
* Gets the substring of $str beginning at $start with the specified $length.
* Returns the substring beginning at $start with the specified $length.
* It differs from the mb_substr() function in that providing a $length of
* null will return the rest of the string, rather than an empty string.
*
* @param string $str The string to get the length of
* @param int $start Position of the first character to use from str
* @param int $length Maximum number of characters used
* @param string $str The string to get the length of
* @param int $start Position of the first character to use
* @param int $length Maximum number of characters used
* @param string $encoding The character encoding
* @return string The substring of $str
*/
@@ -492,7 +527,7 @@ class StaticStringy
}
/**
* Gets the character of $str at $index, with indexes starting at 0.
* Returns the character of the string at $index, with indexes starting at 0.
*
* @param string $str The string from which to get the char
* @param int $index Position of the character
@@ -505,7 +540,7 @@ class StaticStringy
}
/**
* Gets the first $n characters of $str.
* Returns the first $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the start
@@ -518,7 +553,7 @@ class StaticStringy
}
/**
* Gets the last $n characters of $str.
* Returns the last $n characters of the string.
*
* @param string $str The string from which to get the substring
* @param int $n Number of chars to retrieve from the end
@@ -531,7 +566,8 @@ class StaticStringy
}
/**
* Ensures that $str begins with $substring.
* Ensures that the string begins with $substring. If it doesn't, it's
* prepended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
@@ -544,7 +580,8 @@ class StaticStringy
}
/**
* Ensures that $str ends with $substring.
* Ensures that the string begins with $substring. If it doesn't, it's
* appended.
*
* @param string $str The string to modify
* @param string $substring The substring to add if not present
@@ -557,7 +594,7 @@ class StaticStringy
}
/**
* Removes the prefix $substring if present.
* Returns a new string with the prefix $substring removed, if it was present.
*
* @param string $str String from which to remove the prefix
* @param string $substring The prefix to remove
@@ -570,7 +607,7 @@ class StaticStringy
}
/**
* Removes the suffix $substring if present.
* Returns a new string with the suffix $substring removed, if it was present.
*
* @param string $str String from which to remove the suffix
* @param string $substring The suffix to remove
@@ -583,7 +620,7 @@ class StaticStringy
}
/**
* Returns true if $str contains only alphabetic chars, false otherwise.
* Returns true if the string contains only alphabetic chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
@@ -595,8 +632,8 @@ class StaticStringy
}
/**
* Returns true if $str contains only alphabetic and numeric chars, false
* otherwise.
* Returns true if the string contains only alphabetic and numeric chars,
* false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
@@ -608,7 +645,7 @@ class StaticStringy
}
/**
* Returns true if $str contains only whitespace chars, false otherwise.
* Returns true if the string contains only whitespace chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
@@ -620,7 +657,19 @@ class StaticStringy
}
/**
* Returns true if $str contains only lower case chars, false otherwise.
* Returns true if the string is JSON, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is JSON
*/
public static function isJson($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isJson();
}
/**
* Returns true if the string contains only lower case chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
@@ -632,7 +681,19 @@ class StaticStringy
}
/**
* Returns true if $str contains only upper case chars, false otherwise.
* Returns true if the string is serialized, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is serialized
*/
public static function isSerialized($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isSerialized();
}
/**
* Returns true if the string contains only upper case chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
@@ -644,30 +705,65 @@ class StaticStringy
}
/**
* Returns the number of occurences of $substring in $str. An alias for
* mb_substr_count()
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @param string $str The haystack to search through
* @param string $substring The substring to search for
* @param string $encoding The character encoding
* @return int The number of $substring occurences
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only hexadecimal characters
*/
public static function count($str, $substring, $encoding = null)
public static function isHexadecimal($str, $encoding = null)
{
return Stringy::create($str, $encoding)->count($substring);
return Stringy::create($str, $encoding)->isHexadecimal();
}
/**
* Replaces all occurrences of $search with $replace in $str.
* Returns the number of occurrences of $substring in the given string.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $str The haystack to search through
* @param string $search The needle to search for
* @param string $replace The string to replace with
* @param string $encoding The character encoding
* @param string $str The string to search through
* @param string $substring The substring to search for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @param string $encoding The character encoding
* @return int The number of $substring occurrences
*/
public static function count($str, $substring, $caseSensitive = true,
$encoding = null)
{
return Stringy::create($str, $encoding)->count($substring, $caseSensitive);
}
/**
* Replaces all occurrences of $search in $str by $replacement.
*
* @param string $str The haystack to search through
* @param string $search The needle to search for
* @param string $replacement The string to replace with
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function replace($str, $search, $replace, $encoding = null)
public static function replace($str, $search, $replacement, $encoding = null)
{
return Stringy::create($str, $encoding)->replace($search, $replace);
return Stringy::create($str, $encoding)->replace($search, $replacement)->str;
}
/**
* Replaces all occurrences of $pattern in $str by $replacement. An alias
* for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
* in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
* in the bundled version of Oniguruma in PHP 5.3.
*
* @param string $str The haystack to search through
* @param string $pattern The regular expression pattern
* @param string $replacement The string to replace with
* @param string $options Matching conditions to be used
* @param string $encoding The character encoding
* @return string The resulting string after the replacements
*/
public static function regexReplace($str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
return Stringy::create($str, $encoding)->regexReplace($pattern,
$replacement, $options, $encoding)->str;
}
}

View File

@@ -8,10 +8,24 @@ class Stringy
public $encoding;
/**
* Initializes a Stringy object and assigns both str and encoding properties
* the supplied values. If $encoding is not specified, it defaults to
* mb_internal_encoding().
*
* @param string $str String to modify
* @param string $encoding The character encoding
*/
public function __construct($str, $encoding = null)
{
$this->str = $str;
$this->encoding = $encoding ?: mb_internal_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.
* mb_internal_encoding(). It then returns the initialized object.
*
* @param string $str String to modify
* @param string $encoding The character encoding
@@ -19,13 +33,7 @@ class Stringy
*/
public static function create($str, $encoding = null)
{
$encoding = $encoding ?: mb_internal_encoding();
$stringyObj = new self();
$stringyObj->str = $str;
$stringyObj->encoding = $encoding;
return $stringyObj;
return new static($str, $encoding);
}
/**
@@ -39,7 +47,7 @@ class Stringy
}
/**
* Converts the first character of the string to upper case.
* Converts the first character of the supplied string to upper case.
*
* @return Stringy Object with the first character of $str being upper case
*/
@@ -71,9 +79,9 @@ class Stringy
}
/**
* Converts the string to camelCase. Trims surrounding spaces, capitalizes
* letters following digits, spaces, dashes and underscores, and removes
* spaces, dashes, underscores.
* Returns a camelCase version of the supplied string. Trims surrounding
* spaces, capitalizes letters following digits, spaces, dashes and
* underscores, and removes spaces, dashes, underscores.
*
* @return Stringy Object with $str in camelCase
*/
@@ -102,9 +110,9 @@ class Stringy
}
/**
* Converts the string to UpperCamelCase. Trims surrounding spaces, capitalizes
* letters following digits, spaces, dashes and underscores, and removes
* spaces, dashes, underscores.
* Returns an UpperCamelCase version of the supplied string. It trims
* surrounding spaces, capitalizes letters following digits, spaces, dashes
* and underscores, and removes spaces, dashes, underscores.
*
* @return Stringy Object with $str in UpperCamelCase
*/
@@ -114,8 +122,8 @@ class Stringy
}
/**
* Sets the string to lowercase, trims it, and seperates is by dashes. Dashes
* are inserted before uppercase characters (with the exception of the first
* Returns a lowercase and trimmed string separated 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.
*
* @return Stringy Object with a dasherized $str
@@ -137,7 +145,7 @@ class Stringy
}
/**
* Sets $str to a lowercase and trimmed string seperated by underscores.
* Returns a lowercase and trimmed string separated 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.
@@ -161,7 +169,7 @@ class Stringy
}
/**
* Sets $str to a case swapped version of the string.
* Returns a case swapped version of the string.
*
* @return Stringy Object whose $str has each character's case swapped
*/
@@ -185,7 +193,7 @@ class Stringy
}
/**
* Capitalizes the first letter of each word in $str, after trimming.
* Returns a trimmed string with the first letter of each word capitalized.
* 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.
@@ -213,8 +221,8 @@ class Stringy
}
/**
* Capitalizes the first word of $str, replaces underscores with spaces,
* and strips '_id'.
* Capitalizes the first word of the string, replaces underscores with
* spaces, and strips '_id'.
*
* @return Stringy Object with a humanized $str
*/
@@ -229,8 +237,9 @@ class Stringy
}
/**
* Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
* (and commonly used in Word documents) with their ASCII equivalents.
* Returns a string with smart quotes, ellipsis characters, and dashes from
* Windows-1252 (commonly used in Word documents) replaced by their ASCII
* equivalents.
*
* @return Stringy Object whose $str has those characters removed
*/
@@ -247,77 +256,109 @@ class Stringy
}
/**
* Trims $str and replaces consecutive whitespace characters with a single
* space. This inclues tabs and newline characters.
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @return Stringy Object with a trimmed $str with condensed whitespace
* @return Stringy Object with a trimmed $str and condensed whitespace
*/
public function collapseWhitespace()
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/\s+/u', ' ', $stringy->trim());
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($this->encoding);
return $stringy;
$stringy = self::create($this->str, $this->encoding);
$stringy->str = mb_ereg_replace('[[:space:]]+', ' ', $stringy);
mb_regex_encoding($regexEncoding);
return $stringy->trim();
}
/**
* Converts some non-ASCII characters to their closest ASCII counterparts.
* Returns an ASCII version of the string. A set of non-ASCII characters are
* replaced with their closest ASCII counterparts, and the rest are removed.
*
* @return Stringy Object whose $str had those characters replaced
* @return Stringy Object whose $str contains only ASCII characters
*/
public function standardize()
public function toAscii()
{
$stringy = self::create($this->str, $this->encoding);
$charsArray = array(
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
'ᾶ', 'ᾷ'),
'b' => array('б', 'β'),
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ'),
'e' => array('è', 'é', 'ê', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ',
'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э'),
'g' => array('ĝ', 'ğ', 'ġ', '),
'h' => array('ĥ', 'ħ'),
'i' => array('ì', 'í', 'î', 'ï', 'ī', ', 'ĭ', 'į', 'ı', 'ι', 'ί',
'ϊ', 'ΐ', '', '', '', '', '', '', '', '', '',
', '', '', '', 'ΐ', '', ''),
'j' => array('ĵ'),
'k' => array('ķ', '),
'l' => array('ł', 'ľ', ', 'ļ', 'ŀ'),
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν'),
'o' => array('ò', 'ó', 'ô', 'õ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ό', 'ὀ',
'', '', '', '', '', '', 'ό', 'ö'),
'r' => array('ŕ', 'ř', 'ŗ'),
's' => array('ś', 'š', 'ş'),
't' => array('ť', '),
'u' => array('ü', 'ù', ', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ũ', 'ų', '),
'w' => array('ŵ'),
'y' => array('ÿ', 'ý', 'ŷ'),
'z' => array('ź', 'ž', 'ż'),
'oe' => array('œ'),
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
'', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', '', '', '', '', '',
'', 'ᾎ', 'ᾏ', 'Ᾰ', '', '', 'Ά', ''),
'B' => array('Б'),
'C' => array('Ć', 'Č', 'Ĉ', '),
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ'),
'E' => array('É', 'Ê', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ',
'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', '),
'G' => array('Ğ', 'Ġ', '),
'I' => array('Í', 'Î', 'Ï', 'Ī', 'Ĩ', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ',
'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί'),
'L' => array('Ĺ', '),
'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ',),
'O' => array('Ó', 'Ô', 'Õ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', '', '',
'Ὂ', '', '', '', '', 'Ό'),
'R' => array('Ř', '),
'S' => array('Ş', 'Ŝ', 'Ș', 'Š', '),
'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț'),
'U' => array('Ù', 'Ú', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ũ', 'Ų'),
'Y' => array('Ý', 'Ÿ', '', '', '', 'Ύ'),
'Z' => array('Ź', 'Ž', 'Ż')
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
'ᾶ', 'ᾷ', 'а'),
'b' => array('б', 'β'),
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'),
'e' => array('è', 'é', 'ê', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ',
'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є'),
'f' => array('ф'),
'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', '),
'h' => array('ĥ', 'ħ'),
'i' => array('ì', 'í', 'î', 'ï', 'ī', 'ĩ', 'ĭ', 'į', 'ı', 'ι', 'ί',
'ϊ', 'ΐ', 'ἰ', 'ἱ', '', '', '', '', '', 'ἷ', 'ὶ',
'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и'),
'j' => array('ĵ'),
'k' => array('ķ', 'ĸ', 'к'),
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л'),
'm' => array('м'),
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н'),
'o' => array('ò', 'ó', 'ô', 'õ', 'ø', 'ō', 'ő', ', 'ο', 'ό', 'ὀ',
'', 'ὂ', 'ὃ', 'ὄ', '', '', 'ό', 'ö', 'о'),
'p' => array('п'),
'r' => array('ŕ', 'ř', 'ŗ', 'р'),
's' => array('ś', 'š', 'ş', 'с'),
't' => array('ť', 'ţ', 'т'),
'u' => array('ü', 'ù', 'ú', 'û', 'ū', 'ů', 'ű', 'ŭ', ', 'ų', 'µ', 'у'),
'v' => array('в'),
'w' => array('ŵ'),
'y' => array('ÿ', 'ý', 'ŷ', 'й', 'ы'),
'z' => array('ź', 'ž', 'ż', 'з'),
'ch' => array('ч'),
'kh' => array('х'),
'oe' => array('œ'),
'sh' => array('ш'),
'shch' => array('),
'ts' => array('ц'),
'ya' => array('я'),
'yu' => array('ю'),
'zh' => array('ж'),
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
'', '', '', '', '', '', '', '', '', '', '',
'ᾍ', 'ᾎ', 'ᾏ', '', '', '', 'Ά', 'ᾼ', 'А'),
'B' => array('Б'),
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
'D' => array('Ď', 'Ð', 'Đ', ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', '),
'E' => array('É', 'Ê', ', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', '',
'', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', '', 'Е', 'Ё', 'Э', 'Є'),
'F' => array('Ф'),
'G' => array('Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ'),
'I' => array('Í', 'Î', 'Ï', 'Ī', 'Ĩ', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ',
'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί',
'И', 'І', 'Ї'),
'K' => array('К'),
'L' => array('Ĺ', 'Ł', 'Л'),
'M' => array('М'),
'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н'),
'O' => array('Ó', 'Ô', 'Õ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ',
'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О'),
'P' => array('П'),
'R' => array('Ř', 'Ŕ', 'Р'),
'S' => array('Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С'),
'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț', 'Т'),
'U' => array('Ù', 'Ú', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ũ', 'Ų', 'У'),
'V' => array('В'),
'Y' => array('Ý', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й'),
'Z' => array('Ź', 'Ž', 'Ż', 'З'),
'CH' => array('Ч'),
'KH' => array('Х'),
'SH' => array('Ш'),
'SHCH' => array('Щ'),
'TS' => array('Ц'),
'YA' => array('Я'),
'YU' => array('Ю'),
'ZH' => array('Ж')
);
foreach ($charsArray as $key => $value) {
@@ -328,15 +369,15 @@ class Stringy
}
/**
* 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
* Pads the string to a given length with $padStr. If length is less than
* or equal to the length of the string, 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.
* 'right', 'both') is 'right'. Throws an InvalidArgumentException if
* $padType isn't one of those 3 values.
*
* @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 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'
* @return Stringy Object with a padded $str
* @throws InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both'
@@ -344,7 +385,7 @@ class Stringy
public function pad($length, $padStr = ' ', $padType = 'right')
{
if (!in_array($padType, array('left', 'right', 'both'))) {
throw new InvalidArgumentException('Pad expects $padType ' .
throw new \InvalidArgumentException('Pad expects $padType ' .
"to be one of 'left', 'right' or 'both'");
}
@@ -387,8 +428,8 @@ class Stringy
}
/**
* Pads $str to a given length from the begining of the string.
* Alias for pad($length, $padStr, 'left')
* Returns a new string of a given length such that the beginning of the
* string is padded. Alias for pad() with a $padType of 'left'.
*
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
@@ -400,12 +441,12 @@ class Stringy
}
/**
* Pads $str to a given length from the end of the string.
* Alias for pad($length, $padStr, 'left')
* Returns a new string of a given length such that the end of the string is
* padded. Alias for pad() with a $padType of 'right'.
*
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @return string Object with a right padded $str
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @return Stringy Object with a right padded $str
*/
public function padRight($length, $padStr = ' ')
{
@@ -413,8 +454,8 @@ class Stringy
}
/**
* Pads $str to a given length such that both sides of the string string are
* padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
* Returns a new string of a given length such that both sides of the string
* string are padded. Alias for pad() with a $padType of 'both'.
*
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
@@ -426,12 +467,12 @@ class Stringy
}
/**
* 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.
* Returns true if the string begins with $substring, false otherwise.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str starts with $substring
*/
public function startsWith($substring, $caseSensitive = true)
@@ -448,12 +489,12 @@ class Stringy
}
/**
* 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.
* Returns true if the string ends with $substring, false otherwise. By
* default, the comparison is case-sensitive, but can be made insensitive by
* setting $caseSensitive to false.
*
* @param string $substring The substring to look for
* @param bool $caseSensitive Whether or not to enfore case-sensitivity
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str ends with $substring
*/
public function endsWith($substring, $caseSensitive = true)
@@ -473,7 +514,7 @@ class Stringy
}
/**
* Converts each tab in $str to some number of spaces, as defined by
* Converts each tab in the string to some number of spaces, as defined by
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
*
* @param int $tabLength Number of spaces to replace each tab with
@@ -490,7 +531,7 @@ class Stringy
}
/**
* Converts each occurence of some consecutive number of spaces, as defined
* Converts each occurrence of some consecutive number of spaces, as defined
* by $tabLength, to a tab. By default, each 4 consecutive spaces are
* converted to a tab.
*
@@ -508,36 +549,67 @@ class Stringy
}
/**
* 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.
* Converts all characters in the string to lowercase. An alias for PHP's
* mb_strtolower().
*
* @return Stringy Object with all characters of $str being lowercase
*/
public function toLowerCase()
{
$str = mb_strtolower($this->str, $this->encoding);
return self::create($str, $this->encoding);
}
/**
* Converts all characters in the string to uppercase. An alias for PHP's
* mb_strtoupper().
*
* @return Stringy Object with all characters of $str being uppercase
*/
public function toUpperCase()
{
$str = mb_strtoupper($this->str, $this->encoding);
return self::create($str, $this->encoding);
}
/**
* Converts the string 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 $replacement.
* The replacement defaults to a single dash, and the string is also
* converted to lowercase.
*
* @param string $replacement The string used to replace whitespace
* @return Stringy Object whose $str has been converted to an URL slug
*/
public function slugify()
public function slugify($replacement = '-')
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/[^a-zA-Z\d -]/u', '', $stringy->standardize());
$stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '', $stringy->toAscii());
$stringy->str = $stringy->collapseWhitespace()->str;
$stringy->str = str_replace(' ', '-', strtolower($stringy->str));
$stringy->str = str_replace(' ', $replacement, strtolower($stringy->str));
return $stringy;
}
/**
* Returns true if $str contains $needle, false otherwise.
* Returns true if the string contains $needle, false otherwise. By default
* the comparison is case-sensitive, but can be made insensitive by setting
* $caseSensitive to false.
*
* @param string $needle Substring to look for
* @param string $needle Substring to look for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return bool Whether or not $str contains $needle
*/
public function contains($needle)
public function contains($needle, $caseSensitive = true)
{
if (mb_strpos($this->str, $needle, 0, $this->encoding) !== false)
return true;
return false;
if ($caseSensitive)
return (mb_strpos($this->str, $needle, 0, $this->encoding) !== false);
else
return (mb_stripos($this->str, $needle, 0, $this->encoding) !== false);
}
/**
@@ -555,7 +627,7 @@ class Stringy
}
/**
* Inserts $substring into $str at the $index provided.
* Inserts $substring into the string at the $index provided.
*
* @param string $substring String to be inserted
* @param int $index The index at which to insert the substring
@@ -576,7 +648,7 @@ class Stringy
}
/**
* Truncates $str to a given length. If $substring is provided, and
* Truncates the string to a given length. If $substring is provided, and
* truncating occurs, the string is further truncated so that the substring
* may be appended without exceeding the desired length.
*
@@ -601,8 +673,8 @@ class Stringy
}
/**
* Truncates $str to a given length, while ensuring that it does not chop
* words. If $substring is provided, and truncating occurs, the string
* 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.
*
@@ -624,7 +696,7 @@ class Stringy
// If the last word was truncated
if (mb_strpos($stringy->str, ' ', $length - 1, $stringy->encoding) != $length) {
// Find pos of the last occurence of a space, and get everything up until
// Find pos of the last occurrence of a space, and get everything up until
$lastPos = mb_strrpos($truncated, ' ', 0, $stringy->encoding);
$truncated = mb_substr($truncated, 0, $lastPos, $stringy->encoding);
}
@@ -635,7 +707,7 @@ class Stringy
}
/**
* Reverses $str. A multibyte version of strrev.
* Returns a reversed string. A multibyte version of strrev().
*
* @return Stringy Object with a reversed $str
*/
@@ -653,8 +725,8 @@ class Stringy
}
/**
* A multibyte str_shuffle function. It randomizes the order of characters
* in $str.
* A multibyte str_shuffle() function. It returns a string with its
* characters in random order.
*
* @return Stringy Object with a shuffled $str
*/
@@ -672,7 +744,7 @@ class Stringy
}
/**
* Trims $str. An alias for PHP's trim() function.
* Returns the trimmed string. An alias for PHP's trim() function.
*
* @return Stringy Object with a trimmed $str
*/
@@ -682,7 +754,7 @@ class Stringy
}
/**
* Finds the longest common prefix between $str and $otherStr.
* Returns the longest common prefix between the string and $otherStr.
*
* @param string $otherStr Second string for comparison
* @return Stringy Object with its $str being the longest common prefix
@@ -706,7 +778,7 @@ class Stringy
}
/**
* Finds the longest common suffix between $str and $otherStr.
* Returns the longest common suffix between the string and $otherStr.
*
* @param string $otherStr Second string for comparison
* @return Stringy Object with its $str being the longest common suffix
@@ -730,8 +802,8 @@ class Stringy
}
/**
* Finds the longest common substring between $str and $otherStr. In the
* case of ties, returns that which occurs first.
* Returns the longest common substring between the string and $otherStr.
* In the case of ties, it returns that which occurs first.
*
* @param string $otherStr Second string for comparison
* @return Stringy Object with its $str being the longest common substring
@@ -777,7 +849,7 @@ class Stringy
}
/**
* Returns the length of $str. An alias for PHP's mb_strlen() function.
* Returns the length of the string. An alias for PHP's mb_strlen() function.
*
* @return int The number of characters in $str given the encoding
*/
@@ -787,12 +859,12 @@ class Stringy
}
/**
* Gets the substring of $str beginning at $start with the specified $length.
* Returns the substring beginning at $start with the specified $length.
* It differs from the mb_substr() function in that providing a $length of
* null will return the rest of the string, rather than an empty string.
*
* @param int $start Position of the first character to use from str
* @param int $length Maximum number of characters used
* @param int $start Position of the first character to use
* @param int $length Maximum number of characters used
* @return Stringy Object with its $str being the substring
*/
public function substr($start, $length = null)
@@ -811,10 +883,10 @@ class Stringy
}
/**
* Gets the character of $str at $index, with indexes starting at 0.
* Returns the character of the string at $index, with indexes starting at 0.
*
* @param int $index Position of the character
* @return Stringy Object with its $str being the selected character
* @param int $index Position of the character
* @return Stringy The character at $index
*/
public function at($index)
{
@@ -822,7 +894,7 @@ class Stringy
}
/**
* Gets the first $n characters of $str.
* Returns the first $n characters of the string.
*
* @param int $n Number of characters to retrieve from the start
* @return Stringy Object with its $str being the first $n chars
@@ -841,7 +913,7 @@ class Stringy
}
/**
* Gets the last $n characters of $str.
* Returns the last $n characters of the string.
*
* @param int $n Number of characters to retrieve from the end
* @return Stringy Object with its $str being the last $n chars
@@ -860,7 +932,8 @@ class Stringy
}
/**
* Ensures that $str begins with $substring.
* Ensures that the string begins with $substring. If it doesn't, it's
* prepended.
*
* @param string $substring The substring to add if not present
* @return Stringy Object with its $str prefixed by the $substring
@@ -876,7 +949,8 @@ class Stringy
}
/**
* Ensures that $str ends with $substring.
* Ensures that the string begins with $substring. If it doesn't, it's
* appended.
*
* @param string $substring The substring to add if not present
* @return Stringy Object with its $str suffixed by the $substring
@@ -892,7 +966,7 @@ class Stringy
}
/**
* Removes the prefix $substring if present.
* Returns a new string with the prefix $substring removed, if it was present.
*
* @param string $substring The prefix to remove
* @return Stringy Object having a $str without the prefix $substring
@@ -910,7 +984,7 @@ class Stringy
}
/**
* Removes the suffix $substring if present.
* Returns a new string with the suffix $substring removed, if it was present.
*
* @param string $substring The suffix to remove
* @return Stringy Object having a $str without the suffix $substring
@@ -930,7 +1004,7 @@ class Stringy
/**
* Returns true if $str matches the supplied pattern, false otherwise.
*
* @param string Regex pattern to match against
* @param string $pattern Regex pattern to match against
* @return bool Whether or not $str matches the pattern
*/
private function matchesPattern($pattern)
@@ -945,7 +1019,7 @@ class Stringy
}
/**
* Returns true if $str contains only alphabetic chars, false otherwise.
* Returns true if the string contains only alphabetic chars, false otherwise.
*
* @return bool Whether or not $str contains only alphabetic chars
*/
@@ -955,8 +1029,8 @@ class Stringy
}
/**
* Returns true if $str contains only alphabetic and numeric chars, false
* otherwise.
* Returns true if the string contains only alphabetic and numeric chars,
* false otherwise.
*
* @return bool Whether or not $str contains only alphanumeric chars
*/
@@ -966,7 +1040,17 @@ class Stringy
}
/**
* Returns true if $str contains only whitespace chars, false otherwise.
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @return bool Whether or not $str contains only hexadecimal chars
*/
public function isHexadecimal()
{
return $this->matchesPattern('^([[:xdigit:]])*$');
}
/**
* Returns true if the string contains only whitespace chars, false otherwise.
*
* @return bool Whether or not $str contains only whitespace characters
*/
@@ -976,7 +1060,22 @@ class Stringy
}
/**
* Returns true if $str contains only lower case chars, false otherwise.
* Returns true if the string is JSON, false otherwise.
*
* @return bool Whether or not $str is JSON
*/
public function isJson()
{
if (!$this->endsWith('}') && !$this->endsWith(']')) {
return false;
}
return !is_null(json_decode($this->str));
}
/**
* Returns true if the string contains only lower case chars, false otherwise.
*
* @return bool Whether or not $str contains only lower case characters
*/
@@ -986,7 +1085,7 @@ class Stringy
}
/**
* Returns true if $str contains only lower case chars, false otherwise.
* Returns true if the string contains only lower case chars, false otherwise.
*
* @return bool Whether or not $str contains only lower case characters
*/
@@ -996,38 +1095,77 @@ class Stringy
}
/**
* Returns the number of occurences of $substring in $str. An alias for
* mb_substr_count()
* Returns true if the string is serialized, false otherwise.
*
* @param string $substring The substring to search for
* @return int The number of $substring occurences
* @return bool Whether or not $str is serialized
*/
public function count($substring)
public function isSerialized()
{
return $this->str === 'b:0;' || @unserialize($this->str) !== false;
}
/**
* Returns the number of occurrences of $substring in the given string.
* By default, the comparison is case-sensitive, but can be made insensitive
* by setting $caseSensitive to false.
*
* @param string $substring The substring to search for
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
* @return int The number of $substring occurrences
*/
public function count($substring, $caseSensitive = true)
{
if (!$caseSensitive) {
$str = mb_strtoupper($this->str, $this->encoding);
$substring = mb_strtoupper($substring, $this->encoding);
return mb_substr_count($str, $substring, $this->encoding);
}
return mb_substr_count($this->str, $substring, $this->encoding);
}
/**
* Replaces all occurrences of $search with $replace in $str.
* Replaces all occurrences of $search in $str by $replacement.
*
* @param string $search The needle to search for
* @param string $replace The string to replace with
* @param string $search The needle to search for
* @param string $replacement The string to replace with
* @return Stringy Object with the resulting $str after the replacements
*/
public function replace($search, $replace)
public function replace($search, $replacement)
{
$stringy = self::create($this->str, $this->encoding);
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($stringy->encoding);
mb_regex_encoding($this->encoding);
// Don't want the args accidentally being parsed as regex
$quotedSearch = preg_quote($search);
$quotedReplace = preg_quote($replace);
// Don't want the args being parsed as regex
$search = preg_quote($search);
$replacement = preg_quote($replacement);
$stringy->str = mb_ereg_replace($search, $replace, $stringy->str);
$str = mb_ereg_replace($search, $replacement, $this->str);
mb_regex_encoding($regexEncoding);
return $stringy;
return self::create($str, $this->encoding);
}
/**
* Replaces all occurrences of $pattern in $str by $replacement. An alias
* for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
* in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
* in the bundled version of Oniguruma in PHP 5.3.
*
* @param string $pattern The regular expression pattern
* @param string $replacement The string to replace with
* @param string $options Matching conditions to be used
* @return Stringy Object with the resulting $str after the replacements
*/
public function regexReplace($pattern, $replacement, $options = 'msr')
{
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($this->encoding);
$str = mb_ereg_replace($pattern, $replacement, $this->str, $options);
mb_regex_encoding($regexEncoding);
return self::create($str, $this->encoding);
}
}

View File

@@ -1,36 +1,32 @@
<?php
class CommonTest extends PHPUnit_Framework_TestCase
abstract class CommonTest extends PHPUnit_Framework_TestCase
{
public function stringsForUpperCaseFirst()
public function upperCaseFirstProvider()
{
$testData = array(
return 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()
public function lowerCaseFirstProvider()
{
$testData = array(
return 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()
public function camelizeProvider()
{
$testData = array(
return array(
array('camelCase', 'CamelCase'),
array('camelCase', 'Camel-Case'),
array('camelCase', 'camel case'),
@@ -45,13 +41,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('στανιλCase', 'Στανιλ case', 'UTF-8'),
array('σamelCase', 'σamel Case', 'UTF-8')
);
return $testData;
}
public function stringsForUpperCamelize()
public function upperCamelizeProvider()
{
$testData = array(
return array(
array('CamelCase', 'camelCase'),
array('CamelCase', 'Camel-Case'),
array('CamelCase', 'camel case'),
@@ -66,13 +60,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('ΣτανιλCase', 'στανιλ case', 'UTF-8'),
array('ΣamelCase', 'Σamel Case', 'UTF-8')
);
return $testData;
}
public function stringsForDasherize()
public function dasherizeProvider()
{
$testData = array(
return array(
array('test-case', 'testCase'),
array('test-case', 'Test-Case'),
array('test-case', 'test case'),
@@ -89,13 +81,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('στανιλ-case', 'Στανιλ case', 'UTF-8'),
array('σash-case', 'Σash Case', 'UTF-8')
);
return $testData;
}
public function stringsForUnderscored()
public function underscoredProvider()
{
$testData = array(
return array(
array('test_case', 'testCase'),
array('test_case', 'Test-Case'),
array('test_case', 'test case'),
@@ -112,88 +102,79 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('στανιλ_case', 'Στανιλ case', 'UTF-8'),
array('σash_case', 'Σash Case', 'UTF-8')
);
return $testData;
}
public function stringsForSwapCase()
public function swapCaseProvider()
{
$testData = array(
return array(
array('TESTcASE', 'testCase'),
array('tEST-cASE', 'Test-Case'),
array(' - σASH cASE', ' - Σash Case', 'UTF-8'),
array('νΤΑΝΙΛ', 'Ντανιλ', 'UTF-8')
);
return $testData;
}
public function stringsForTitleize()
public function titleizeProvider()
{
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
$testData = array(
return 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()
public function humanizeProvider()
{
$testData = array(
return array(
array('Author', 'author_id'),
array('Test user', ' _test_user_'),
array('Συγγραφέας', ' συγγραφέας_id ', 'UTF-8')
);
return $testData;
}
public function stringsForTidy()
public function tidyProvider()
{
$testData = array(
return array(
array('"I see..."', '“I see…”'),
array("'This too'", "This too"),
array('test-dash', 'test—dash'),
array('Ο συγγραφέας είπε...', 'Ο συγγραφέας είπε…')
);
return $testData;
}
public function stringsForCollapseWhitespace()
public function collapseWhitespaceProvider()
{
$testData = array(
return array(
array('foo bar', ' foo bar '),
array('test string', 'test string'),
array('Ο συγγραφέας', ' Ο συγγραφέας '),
array('123', ' 123 '),
array('1 2 3', '  1  2  3  ', 'UTF-8'), // ideographic spaces
array('', ' ', 'UTF-8'), // thin space and space
array('', ' '),
array('', ''),
);
return $testData;
}
public function stringsForStandardize()
public function toAsciiProvider()
{
$testData = array(
return array(
array('foo bar', 'fòô bàř'),
array(' TEST ', ' ŤÉŚŢ '),
array('φ = z = 3', 'φ = ź = 3')
array('φ = z = 3', 'φ = ź = 3'),
array('perevirka', 'перевірка'),
array('lysaya gora', 'лысая гора'),
array('shchuka', 'щука')
);
return $testData;
}
public function stringsForPad()
public function padProvider()
{
$testData = array(
return array(
// $length <= $str
array('foo bar', 'foo bar', -1),
array('foo bar', 'foo bar', 7),
@@ -230,47 +211,39 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
);
return $testData;
}
public function stringsForPadLeft()
public function padLeftProvider()
{
$testData = array(
return 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()
public function padRightProvider()
{
$testData = array(
return 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()
public function padBothProvider()
{
$testData = array(
return 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()
public function startsWithProvider()
{
$testData = array(
return array(
array(true, 'foo bars', 'foo bar'),
array(true, 'FOO bars', 'foo bar', false),
array(true, 'FOO bars', 'foo BAR', false),
@@ -283,13 +256,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
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()
public function endsWithProvider()
{
$testData = array(
return array(
array(true, 'foo bars', 'o bars'),
array(true, 'FOO bars', 'o bars', false),
array(true, 'FOO bars', 'o BARs', false),
@@ -302,13 +273,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
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()
public function toSpacesProvider()
{
$testData = array(
return array(
array(' foo bar ', ' foo bar '),
array(' foo bar ', ' foo bar ', 5),
array(' foo bar ', ' foo bar ', 2),
@@ -316,72 +285,98 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(" foo\n bar", " foo\n bar"),
array(" fòô\n bàř", " fòô\n bàř")
);
return $testData;
}
public function stringsForToTabs()
public function toTabsProvider()
{
$testData = array(
return 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()
public function toLowerCaseProvider()
{
$testData = array(
return array(
array('foo bar', 'FOO BAR'),
array(' foo_bar ', ' FOO_bar '),
array('fòô bàř', 'FÒÔ BÀŘ', 'UTF-8'),
array(' fòô_bàř ', ' FÒÔ_bàř ', 'UTF-8'),
array('αυτοκίνητο', 'ΑΥΤΟΚΊΝΗΤΟ', 'UTF-8'),
);
}
public function toUpperCaseProvider()
{
return array(
array('FOO BAR', 'foo bar'),
array(' FOO_BAR ', ' FOO_bar '),
array('FÒÔ BÀŘ', 'fòô bàř', 'UTF-8'),
array(' FÒÔ_BÀŘ ', ' FÒÔ_bàř ', 'UTF-8'),
array('ΑΥΤΟΚΊΝΗΤΟ', 'αυτοκίνητο', 'UTF-8'),
);
}
public function slugifyProvider()
{
return 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')
array('numbers-1234', 'numbers 1234'),
array('perevirka-ryadka', 'перевірка рядка'),
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
array('foo:bar:baz', 'Foo bar baz', ':'),
array('a_string_with_underscores', 'A_string with_underscores', '_')
);
return $testData;
}
public function stringsForContains()
public function containsProvider()
{
$testData = array(
return 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(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', true, 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', true, 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 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')
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', true, 'UTF-8'),
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', true, 'UTF-8'),
array(true, 'This string contains foo bar', 'Foo bar', false),
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%', false),
array(true, 'Ο συγγραφέας είπε', 'ΣΥΓΓΡΑΦΈΑΣ', false, 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å´¥©', false, 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'Å˚ ∆', false, 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'ØŒ¬', false, 'UTF-8'),
array(false, 'This string contains foo bar', 'foobar', false),
array(false, 'This string contains foo bar', 'foo bar ', false),
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', false, 'UTF-8'),
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßÅ˚', false, 'UTF-8')
);
return $testData;
}
public function stringsForSurround()
public function surroundProvider()
{
$testData = array(
return array(
array('__foobar__', 'foobar', '__'),
array('test', 'test', ''),
array('**', '', '*'),
array('¬fòô bàř¬', 'fòô bàř', '¬'),
array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚')
);
return $testData;
}
public function stringsForInsert()
public function insertProvider()
{
$testData = array(
return array(
array('foo bar', 'oo bar', 'f', 0),
array('foo bar', 'f bar', 'oo', 1),
array('f bar', 'f bar', 'oo', 20),
@@ -390,13 +385,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'),
array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8')
);
return $testData;
}
public function stringsForTruncate()
public function truncateProvider()
{
$testData = array(
return array(
array('Test foo bar', 'Test foo bar', 12),
array('Test foo ba', 'Test foo bar', 11),
array('Test foo', 'Test foo bar', 8),
@@ -420,13 +413,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('Teϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'),
array('What are your pl...', 'What are your plans today?', 19, '...')
);
return $testData;
}
public function stringsForSafeTruncate()
public function safeTruncateProvider()
{
$testData = array(
return array(
array('Test foo bar', 'Test foo bar', 12),
array('Test foo', 'Test foo bar', 11),
array('Test foo', 'Test foo bar', 8),
@@ -450,37 +441,31 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'),
array('What are your plans...', 'What are your plans today?', 22, '...')
);
return $testData;
}
public function stringsForReverse()
public function reverseProvider()
{
$testData = array(
return 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()
public function shuffleProvider()
{
$testData = array(
return array(
array('foo bar'),
array('∂∆ ˚åß', 'UTF-8'),
array('å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'UTF-8')
);
return $testData;
}
public function stringsForTrim()
public function trimProvider()
{
$testData = array(
return array(
array('foo bar', ' foo bar '),
array('foo bar', ' foo bar'),
array('foo bar', 'foo bar '),
@@ -490,13 +475,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'fòô bàř '),
array('fòô bàř', "\n\t fòô bàř \n\t")
);
return $testData;
}
public function stringsForLongestCommonPrefix()
public function longestCommonPrefixProvider()
{
$testData = array(
return array(
array('foo', 'foobar', 'foo bar'),
array('foo bar', 'foo bar', 'foo bar'),
array('f', 'foo bar', 'far boo'),
@@ -508,13 +491,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('', 'toy car', 'fòô bar', 'UTF-8'),
array('', 'fòô bar', '', 'UTF-8'),
);
return $testData;
}
public function stringsForLongestCommonSuffix()
public function longestCommonSuffixProvider()
{
$testData = array(
return array(
array('bar', 'foobar', 'foo bar'),
array('foo bar', 'foo bar', 'foo bar'),
array('ar', 'foo bar', 'boo far'),
@@ -526,13 +507,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('', 'toy car', 'fòô bàř', 'UTF-8'),
array('', 'fòô bàř', '', 'UTF-8'),
);
return $testData;
}
public function stringsForLongestCommonSubstring()
public function longestCommonSubstringProvider()
{
$testData = array(
return array(
array('foo', 'foobar', 'foo bar'),
array('foo bar', 'foo bar', 'foo bar'),
array('oo ', 'foo bar', 'boo far'),
@@ -544,25 +523,21 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(' ', 'toy car', 'fòô bàř', 'UTF-8'),
array('', 'fòô bàř', '', 'UTF-8'),
);
return $testData;
}
public function stringsForLength()
public function lengthProvider()
{
$testData = array(
return array(
array(11, ' foo bar '),
array(1, 'f'),
array(0, ''),
array(7, 'fòô bàř', 'UTF-8')
);
return $testData;
}
public function stringsForSubstr()
public function substrProvider()
{
$testData = array(
return array(
array('foo bar', 'foo bar', 0),
array('bar', 'foo bar', 4),
array('bar', 'foo bar', 4, null),
@@ -573,13 +548,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('ô b', 'fòô bàř', 2, 3, 'UTF-8'),
array('', 'fòô bàř', 4, 0, 'UTF-8')
);
return $testData;
}
public function stringsForAt()
public function atProvider()
{
$testData = array(
return array(
array('f', 'foo bar', 0),
array('o', 'foo bar', 1),
array('r', 'foo bar', 6),
@@ -589,13 +562,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('ř', 'fòô bàř', 6, 'UTF-8'),
array('', 'fòô bàř', 7, 'UTF-8'),
);
return $testData;
}
public function stringsForFirst()
public function firstProvider()
{
$testData = array(
return array(
array('', 'foo bar', -5),
array('', 'foo bar', 0),
array('f', 'foo bar', 1),
@@ -609,13 +580,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'fòô bàř', 7, 'UTF-8'),
array('fòô bàř', 'fòô bàř', 8, 'UTF-8'),
);
return $testData;
}
public function stringsForLast()
public function lastProvider()
{
$testData = array(
return array(
array('', 'foo bar', -5),
array('', 'foo bar', 0),
array('r', 'foo bar', 1),
@@ -629,13 +598,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'fòô bàř', 7, 'UTF-8'),
array('fòô bàř', 'fòô bàř', 8, 'UTF-8'),
);
return $testData;
}
public function stringsForEnsureLeft()
public function ensureLeftProvider()
{
$testData = array(
return array(
array('foobar', 'foobar', 'f'),
array('foobar', 'foobar', 'foo'),
array('foo/foobar', 'foobar', 'foo/'),
@@ -647,13 +614,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('http://fòôbàř', 'fòôbàř', 'http://', 'UTF-8'),
array('http://fòôbàř', 'http://fòôbàř', 'http://', 'UTF-8'),
);
return $testData;
}
public function stringsForEnsureRight()
public function ensureRightProvider()
{
$testData = array(
return array(
array('foobar', 'foobar', 'r'),
array('foobar', 'foobar', 'bar'),
array('foobar/bar', 'foobar', '/bar'),
@@ -665,13 +630,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòôbàř.com/', 'fòôbàř', '.com/', 'UTF-8'),
array('fòôbàř.com/', 'fòôbàř.com/', '.com/', 'UTF-8'),
);
return $testData;
}
public function stringsForRemoveLeft()
public function removeLeftProvider()
{
$testData = array(
return array(
array('foo bar', 'foo bar', ''),
array('oo bar', 'foo bar', 'f'),
array('bar', 'foo bar', 'foo '),
@@ -683,13 +646,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'fòô bàř', 'òô', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'òô bàř', 'UTF-8')
);
return $testData;
}
public function stringsForRemoveRight()
public function removeRightProvider()
{
$testData = array(
return array(
array('foo bar', 'foo bar', ''),
array('foo ba', 'foo bar', 'r'),
array('foo', 'foo bar', ' bar'),
@@ -701,13 +662,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('fòô bàř', 'fòô bàř', 'bà', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'fòô bà', 'UTF-8')
);
return $testData;
}
public function stringsForIsAlpha()
public function isAlphaProvider()
{
$testData = array(
return array(
array(true, ''),
array(true, 'foobar'),
array(false, 'foo bar'),
@@ -719,13 +678,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(false, 'ҠѨњ¨ˆфгШ', 'UTF-8'),
array(true, '丹尼爾', 'UTF-8')
);
return $testData;
}
public function stringsForIsAlphanumeric()
public function isAlphanumericProvider()
{
$testData = array(
return array(
array(true, ''),
array(true, 'foobar1'),
array(false, 'foo bar'),
@@ -740,13 +697,11 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(true, انيال1', 'UTF-8'),
array(false, انيال1 ', 'UTF-8')
);
return $testData;
}
public function stringsForIsBlank()
public function isBlankProvider()
{
$testData = array(
return array(
array(true, ''),
array(true, ' '),
array(true, "\n\t "),
@@ -760,13 +715,31 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(false, ' z', 'UTF-8'),
array(false, ' 1', 'UTF-8'),
);
return $testData;
}
public function stringsForIsLowerCase()
public function isJsonProvider()
{
$testData = array(
return array(
array(false, ''),
array(false, '123'),
array(true, '{"foo": "bar"}'),
array(false, '{"foo":"bar",}'),
array(false, '{"foo"}'),
array(true, '["foo"]'),
array(false, '{"foo": "bar"]'),
array(false, '123', 'UTF-8'),
array(true, '{"fòô": "bàř"}', 'UTF-8'),
array(false, '{"fòô":"bàř",}', 'UTF-8'),
array(false, '{"fòô"}', 'UTF-8'),
array(false, '["fòô": "bàř"]', 'UTF-8'),
array(true, '["fòô"]', 'UTF-8'),
array(false, '{"fòô": "bàř"]', 'UTF-8'),
);
}
public function isLowerCaseProvider()
{
return array(
array(true, ''),
array(true, 'foobar'),
array(false, 'foo bar'),
@@ -776,13 +749,24 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(false, 'fòô bàř', 'UTF-8'),
array(false, 'fòôbÀŘ', 'UTF-8'),
);
return $testData;
}
public function stringsForIsUpperCase()
public function isSerializedProvider()
{
$testData = array(
return array(
array(false, ''),
array(true, 'a:1:{s:3:"foo";s:3:"bar";}'),
array(false, 'a:1:{s:3:"foo";s:3:"bar"}'),
array(true, serialize(array('foo' => 'bar'))),
array(true, 'a:1:{s:5:"fòô";s:5:"bàř";}', 'UTF-8'),
array(false, 'a:1:{s:5:"fòô";s:5:"bàř"}', 'UTF-8'),
array(true, serialize(array('fòô' => 'bár')), 'UTF-8'),
);
}
public function isUpperCaseProvider()
{
return array(
array(true, ''),
array(true, 'FOOBAR'),
array(false, 'FOO BAR'),
@@ -792,13 +776,30 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(false, 'FÒÔ BÀŘ', 'UTF-8'),
array(false, 'FÒÔBàř', 'UTF-8'),
);
return $testData;
}
public function stringsForCount()
public function isHexadecimalProvider()
{
$testData = array(
return array(
array(true, ''),
array(true, 'abcdef'),
array(true, 'ABCDEF'),
array(true, '0123456789'),
array(true, '0123456789AbCdEf'),
array(false, '0123456789x'),
array(false, 'ABCDEFx'),
array(true, 'abcdef', 'UTF-8'),
array(true, 'ABCDEF', 'UTF-8'),
array(true, '0123456789', 'UTF-8'),
array(true, '0123456789AbCdEf', 'UTF-8'),
array(false, '0123456789x', 'UTF-8'),
array(false, 'ABCDEFx', 'UTF-8'),
);
}
public function countProvider()
{
return array(
array(0, '', 'foo'),
array(0, 'foo', 'bar'),
array(1, 'foo bar', 'foo'),
@@ -806,35 +807,47 @@ class CommonTest extends PHPUnit_Framework_TestCase
array(0, '', 'fòô', 'UTF-8'),
array(0, 'fòô', 'bàř', 'UTF-8'),
array(1, 'fòô bàř', 'fòô', 'UTF-8'),
array(2, 'fôòô bàř', 'ô', 'UTF-8')
array(2, 'fôòô bàř', 'ô', 'UTF-8'),
array(0, 'fÔÒÔ bàř', 'ô', 'UTF-8'),
array(0, 'foo', 'BAR', false),
array(1, 'foo bar', 'FOo', false),
array(2, 'foo bar', 'O', false),
array(1, 'fòô bàř', 'fÒÔ', false, 'UTF-8'),
array(2, 'fôòô bàř', 'Ô', false, 'UTF-8'),
array(2, 'συγγραφέας', 'Σ', false, 'UTF-8')
);
return $testData;
}
public function stringsForReplace()
public function replaceProvider()
{
$testData = array(
return array(
array('', '', '', ''),
array('foo', '', '', 'foo'),
array('foo', '\s', '\s', 'foo'),
array('foo bar', 'foo bar', '', ''),
array('bar', 'foo bar', 'foo ', ''),
array('far bar', 'foo bar', 'foo', 'far'),
array('bar bar', 'foo bar foo bar', 'foo ', ''),
array('', '', '', '', 'UTF-8'),
array('fòô', '', '', 'fòô', 'UTF-8'),
array('fòô', '\s', '\s', 'fòô', 'UTF-8'),
array('fòô bàř', 'fòô bàř', '', '', 'UTF-8'),
array('bàř', 'fòô bàř', 'fòô ', '', 'UTF-8'),
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),
array('bàř bàř', 'fòô bàř fòô bàř', 'fòô ', '', '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);
public function regexReplaceProvider()
{
return array(
array('', '', '', ''),
array('bar', 'foo', 'f[o]+', 'bar'),
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
array('', '', '', '', 'msr', 'UTF-8'),
array('bàř', 'fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'),
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
);
}
}

View File

@@ -8,450 +8,579 @@ use Stringy\StaticStringy as S;
class StaticStringyTestCase extends CommonTest
{
/**
* @dataProvider stringsForUpperCaseFirst
* @dataProvider upperCaseFirstProvider()
*/
public function testUpperCaseFirst($expected, $str, $encoding = null)
{
$result = S::upperCaseFirst($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLowerCaseFirst
* @dataProvider lowerCaseFirstProvider()
*/
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
$result = S::lowerCaseFirst($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCamelize
* @dataProvider camelizeProvider()
*/
public function testCamelize($expected, $str, $encoding = null)
{
$result = S::camelize($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForUpperCamelize
* @dataProvider upperCamelizeProvider()
*/
public function testUpperCamelize($expected, $str, $encoding = null)
{
$result = S::upperCamelize($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForDasherize
* @dataProvider dasherizeProvider()
*/
public function testDasherize($expected, $str, $encoding = null)
{
$result = S::dasherize($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForUnderscored
* @dataProvider underscoredProvider()
*/
public function testUnderscored($expected, $str, $encoding = null)
{
$result = S::underscored($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSwapCase
* @dataProvider swapCaseProvider()
*/
public function testSwapCase($expected, $str, $encoding = null)
{
$result = S::swapCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForTitleize
* @dataProvider titleizeProvider()
*/
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
{
$result = S::titleize($str, $ignore, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForHumanize
* @dataProvider humanizeProvider()
*/
public function testHumanize($expected, $str, $encoding = null)
{
$result = S::humanize($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForTidy
* @dataProvider tidyProvider()
*/
public function testTidy($expected, $str)
{
$result = S::tidy($str);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCollapseWhitespace
* @dataProvider collapseWhitespaceProvider()
*/
public function testCollapseWhitespace($expected, $str)
public function testCollapseWhitespace($expected, $str, $encoding = null)
{
$result = S::collapseWhitespace($str);
$result = S::collapseWhitespace($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForStandardize
* @dataProvider toAsciiProvider()
*/
public function testStandardize($expected, $str)
public function testToAscii($expected, $str)
{
$result = S::standardize($str);
$result = S::toAscii($str);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPad
* @dataProvider padProvider()
*/
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
{
$result = S::pad($str, $length, $padStr, $padType, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadLeft
* @expectedException \InvalidArgumentException
*/
public function testPadException()
{
$result = S::pad('string', 5, 'foo', 'bar');
}
/**
* @dataProvider padLeftProvider()
*/
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padLeft($str, $length, $padStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadRight
* @dataProvider padRightProvider()
*/
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padRight($str, $length, $padStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadBoth
* @dataProvider padBothProvider()
*/
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padBoth($str, $length, $padStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForStartsWith
* @dataProvider startsWithProvider()
*/
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForEndsWith
* @dataProvider endsWithProvider()
*/
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToSpaces
* @dataProvider toSpacesProvider()
*/
public function testToSpaces($expected, $str, $tabLength = 4)
{
$result = S::toSpaces($str, $tabLength);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToTabs
* @dataProvider toTabsProvider()
*/
public function testToTabs($expected, $str, $tabLength = 4)
{
$result = S::toTabs($str, $tabLength);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSlugify
* @dataProvider toLowerCaseProvider()
*/
public function testSlugify($expected, $str)
public function testToLowerCase($expected, $str, $encoding = null)
{
$result = S::slugify($str);
$result = S::toLowerCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForContains
* @dataProvider toUpperCaseProvider()
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
public function testToUpperCase($expected, $str, $encoding = null)
{
$result = S::contains($haystack, $needle, $encoding);
$result = S::toUpperCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSurround
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str, $replacement = '-')
{
$result = S::slugify($str, $replacement);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider containsProvider()
*/
public function testContains($expected, $haystack, $needle,
$caseSensitive = true, $encoding = null)
{
$result = S::contains($haystack, $needle, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider surroundProvider()
*/
public function testSurround($expected, $str, $substring)
{
$result = S::surround($str, $substring);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForInsert
* @dataProvider insertProvider()
*/
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
{
$result = S::insert($str, $substring, $index, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForTruncate
* @dataProvider truncateProvider()
*/
public function testTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$result = S::truncate($str, $length, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSafeTruncate
* @dataProvider safeTruncateProvider()
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$result = S::safeTruncate($str, $length, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForReverse
* @dataProvider reverseProvider()
*/
public function testReverse($expected, $str, $encoding = null)
{
$result = S::reverse($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForShuffle
* @dataProvider shuffleProvider()
*/
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));
$encoding = $encoding ?: mb_internal_encoding();
$this->assertInternalType('string', $result);
$this->assertEquals(mb_strlen($str, $encoding),
mb_strlen($result, $encoding));
// We'll make sure that the chars are present after shuffle
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
$char = mb_substr($str, $i, 1, $encoding);
$countBefore = mb_substr_count($str, $char, $encoding);
$countAfter = mb_substr_count($result, $char, $encoding);
$this->assertEquals($countBefore, $countAfter);
}
}
/**
* @dataProvider stringsForTrim
* @dataProvider trimProvider()
*/
public function testTrim($expected, $str)
{
$result = S::trim($str);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonPrefix
* @dataProvider longestCommonPrefixProvider()
*/
public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null)
{
$result = S::longestCommonPrefix($str, $otherStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonSuffix
* @dataProvider longestCommonSuffixProvider()
*/
public function testLongestCommonSuffix($expected, $str, $otherStr,
$encoding = null)
{
$result = S::longestCommonSuffix($str, $otherStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLongestCommonSubstring
* @dataProvider longestCommonSubstringProvider()
*/
public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null)
{
$result = S::longestCommonSubstring($str, $otherStr, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLength
* @dataProvider lengthProvider()
*/
public function testLength($expected, $str, $encoding = null)
{
$result = S::length($str, $encoding);
$this->assertEquals($expected, $result);
$this->assertInternalType('int', $result);
}
/**
* @dataProvider stringsForSubstr
* @dataProvider substrProvider()
*/
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
{
$result = S::substr($str, $start, $length, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForAt
* @dataProvider atProvider()
*/
public function testAt($expected, $str, $index, $encoding = null)
{
$result = S::at($str, $index, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForFirst
* @dataProvider firstProvider()
*/
public function testFirst($expected, $str, $n, $encoding = null)
{
$result = S::first($str, $n, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLast
* @dataProvider lastProvider()
*/
public function testLast($expected, $str, $n, $encoding = null)
{
$result = S::last($str, $n, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForEnsureLeft
* @dataProvider ensureLeftProvider()
*/
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
{
$result = S::ensureLeft($str, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForEnsureRight
* @dataProvider ensureRightProvider()
*/
public function testEnsureRight($expected, $str, $substring, $encoding = null)
{
$result = S::ensureRight($str, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveLeft
* @dataProvider removeLeftProvider()
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
$result = S::removeLeft($str, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveRight
* @dataProvider removeRightProvider()
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
$result = S::removeRight($str, $substring, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsAlpha
* @dataProvider isAlphaProvider()
*/
public function testIsAlpha($expected, $str, $encoding = null)
{
$result = S::isAlpha($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsAlphanumeric
* @dataProvider isAlphanumericProvider()
*/
public function testIsAlphanumeric($expected, $str, $encoding = null)
{
$result = S::isAlphanumeric($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsBlank
* @dataProvider isBlankProvider()
*/
public function testIsBlank($expected, $str, $encoding = null)
{
$result = S::isBlank($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsLowerCase
* @dataProvider isJsonProvider()
*/
public function testIsJson($expected, $str, $encoding = null)
{
$result = S::isJson($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider isLowerCaseProvider()
*/
public function testIsLowerCase($expected, $str, $encoding = null)
{
$result = S::isLowerCase($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsUpperCase
* @dataProvider isSerializedProvider()
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
$result = S::isSerialized($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider isUpperCaseProvider()
*/
public function testIsUpperCase($expected, $str, $encoding = null)
{
$result = S::isUpperCase($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCount
* @dataProvider isHexadecimalProvider()
*/
public function testCount($expected, $str, $substring, $encoding = null)
public function testIsHexadecimal($expected, $str, $encoding = null)
{
$result = S::count($str, $substring, $encoding);
$result = S::isHexadecimal($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForReplace
* @dataProvider countProvider()
*/
public function testReplace($expected, $str, $search, $replace,
public function testCount($expected, $str, $substring, $caseSensitive = true,
$encoding = null)
{
$result = S::count($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('int', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider replaceProvider()
*/
public function testReplace($expected, $str, $search, $replacement,
$encoding = null)
{
$result = S::replace($str, $search, $replace, $encoding);
$result = S::replace($str, $search, $replacement, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider regexReplaceProvider()
*/
public function testRegexReplace($expected, $str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
$result = S::regexReplace($str, $pattern, $replacement, $options, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
}

View File

@@ -7,520 +7,684 @@ use Stringy\Stringy as S;
class StringyTestCase extends CommonTest
{
public function testConstruct()
{
$stringy = new S('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', $stringy->str);
$this->assertEquals('UTF-8', $stringy->encoding);
}
public function testCreate()
{
$stringy = S::create('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', $stringy->str);
$this->assertEquals('UTF-8', $stringy->encoding);
}
public function testChaining()
{
$stringy = S::create("Fòô Bàř", 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$result = $stringy->collapseWhitespace()->swapCase()->upperCaseFirst();
$this->assertEquals('FÒÔ bÀŘ', $result);
}
/**
* @dataProvider stringsForUpperCaseFirst
* @dataProvider upperCaseFirstProvider()
*/
public function testUpperCaseFirst($expected, $str, $encoding = null)
{
$result = S::create($str, $encoding)->upperCaseFirst();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLowerCaseFirst
* @dataProvider lowerCaseFirstProvider()
*/
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->lowerCaseFirst();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForCamelize
* @dataProvider camelizeProvider()
*/
public function testCamelize($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->camelize();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForUpperCamelize
* @dataProvider upperCamelizeProvider()
*/
public function testUpperCamelize($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->upperCamelize();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForDasherize
* @dataProvider dasherizeProvider()
*/
public function testDasherize($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->dasherize();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForUnderscored
* @dataProvider underscoredProvider()
*/
public function testUnderscored($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->underscored();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForSwapCase
* @dataProvider swapCaseProvider()
*/
public function testSwapCase($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->swapCase();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForTitleize
* @dataProvider titleizeProvider()
*/
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->titleize($ignore);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForHumanize
* @dataProvider humanizeProvider()
*/
public function testHumanize($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->humanize();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForTidy
* @dataProvider tidyProvider()
*/
public function testTidy($expected, $str)
{
$stringy = S::create($str);
$result = $stringy->tidy();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForCollapseWhitespace
* @dataProvider collapseWhitespaceProvider()
*/
public function testCollapseWhitespace($expected, $str)
public function testCollapseWhitespace($expected, $str, $encoding = null)
{
$stringy = S::create($str);
$stringy = S::create($str, $encoding);
$result = $stringy->collapseWhitespace();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForStandardize
* @dataProvider toAsciiProvider()
*/
public function testStandardize($expected, $str)
public function testToAscii($expected, $str)
{
$stringy = S::create($str);
$result = $stringy->standardize();
$result = $stringy->toAscii();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForPad
* @dataProvider padProvider()
*/
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->pad($length, $padStr, $padType);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForPadLeft
* @expectedException \InvalidArgumentException
*/
public function testPadException()
{
$stringy = S::create('foo');
$result = $stringy->pad(5, 'foo', 'bar');
}
/**
* @dataProvider padLeftProvider()
*/
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->padLeft($length, $padStr);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForPadRight
* @dataProvider padRightProvider()
*/
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->padRight($length, $padStr);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForPadBoth
* @dataProvider padBothProvider()
*/
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->padBoth($length, $padStr);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForStartsWith
* @dataProvider startsWithProvider()
*/
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->startsWith($substring, $caseSensitive);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForEndsWith
* @dataProvider endsWithProvider()
*/
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
$caseSensitive = true, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->endsWith($substring, $caseSensitive);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForToSpaces
* @dataProvider toSpacesProvider()
*/
public function testToSpaces($expected, $str, $tabLength = 4)
{
$stringy = S::create($str);
$result = $stringy->toSpaces($tabLength);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForToTabs
* @dataProvider toTabsProvider()
*/
public function testToTabs($expected, $str, $tabLength = 4)
{
$stringy = S::create($str);
$result = $stringy->toTabs($tabLength);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForSlugify
* @dataProvider toLowerCaseProvider()
*/
public function testSlugify($expected, $str)
public function testToLowerCase($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->toLowerCase();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider toUpperCaseProvider()
*/
public function testToUpperCase($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->toUpperCase();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider slugifyProvider()
*/
public function testSlugify($expected, $str, $replacement = '-')
{
$stringy = S::create($str);
$result = $stringy->slugify();
$result = $stringy->slugify($replacement);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForContains
* @dataProvider containsProvider()
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
public function testContains($expected, $haystack, $needle,
$caseSensitive = true, $encoding = null)
{
$stringy = S::create($haystack, $encoding);
$result = $stringy->contains($needle);
$result = $stringy->contains($needle, $caseSensitive);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($haystack, $stringy);
}
/**
* @dataProvider stringsForSurround
* @dataProvider surroundProvider()
*/
public function testSurround($expected, $str, $substring)
{
$stringy = S::create($str);
$result = $stringy->surround($substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForInsert
* @dataProvider insertProvider()
*/
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->insert($substring, $index);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForTruncate
* @dataProvider truncateProvider()
*/
public function testTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->truncate($length, $substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForReverse
* @dataProvider reverseProvider()
*/
public function testReverse($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->reverse();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForShuffle
* @dataProvider shuffleProvider()
*/
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$stringy = S::create($str, $encoding);
$encoding = $encoding ?: mb_internal_encoding();
$result = $stringy->shuffle();
$this->assertEquals(count_chars($str), count_chars($result));
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($str, $stringy);
$this->assertEquals(mb_strlen($str, $encoding),
mb_strlen($result, $encoding));
// We'll make sure that the chars are present after shuffle
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
$char = mb_substr($str, $i, 1, $encoding);
$countBefore = mb_substr_count($str, $char, $encoding);
$countAfter = mb_substr_count($result, $char, $encoding);
$this->assertEquals($countBefore, $countAfter);
}
}
/**
* @dataProvider stringsForTrim
* @dataProvider trimProvider()
*/
public function testTrim($expected, $str)
{
$stringy = S::create($str);
$result = $stringy->trim();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForLongestCommonPrefix
* @dataProvider longestCommonPrefixProvider()
*/
public function testLongestCommonPrefix($expected, $str, $otherStr,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->longestCommonPrefix($otherStr);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForLongestCommonSubstring
* @dataProvider longestCommonSubstringProvider()
*/
public function testLongestCommonSubstring($expected, $str, $otherStr,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->longestCommonSubstring($otherStr);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForLength
* @dataProvider lengthProvider()
*/
public function testLength($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->length();
$this->assertInternalType('int', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForSubstr
* @dataProvider substrProvider()
*/
public function testSubstr($expected, $str, $start, $length = null,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->substr($start, $length);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForAt
* @dataProvider atProvider()
*/
public function testAt($expected, $str, $index, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->at($index);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForFirst
* @dataProvider firstProvider()
*/
public function testFirst($expected, $str, $n, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->first($n);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForLast
* @dataProvider lastProvider()
*/
public function testLast($expected, $str, $n, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->last($n);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForEnsureLeft
* @dataProvider ensureLeftProvider()
*/
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->ensureLeft($substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForEnsureRight
* @dataProvider ensureRightProvider()
*/
public function testEnsureRight($expected, $str, $substring, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->ensureRight($substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForRemoveLeft
* @dataProvider removeLeftProvider()
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->removeLeft($substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForRemoveRight
* @dataProvider removeRightProvider()
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->removeRight($substring);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsAlpha
* @dataProvider isAlphaProvider()
*/
public function testIsAlpha($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isAlpha();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsAlphanumeric
* @dataProvider isAlphanumericProvider()
*/
public function testIsAlphanumeric($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isAlphanumeric();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsBlank
* @dataProvider isBlankProvider()
*/
public function testIsBlank($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isBlank();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsLowerCase
* @dataProvider isJsonProvider()
*/
public function testIsJson($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isJson();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider isLowerCaseProvider()
*/
public function testIsLowerCase($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isLowerCase();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsUpperCase
* @dataProvider isSerializedProvider()
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isSerialized();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider isUpperCaseProvider()
*/
public function testIsUpperCase($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isUpperCase();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForCount
* @dataProvider isHexadecimalProvider()
*/
public function testCount($expected, $str, $substring, $encoding = null)
public function testIsHexadecimal($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->count($substring);
$result = $stringy->isHexadecimal();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForReplace
* @dataProvider countProvider()
*/
public function testReplace($expected, $str, $search, $replace,
public function testCount($expected, $str, $substring, $caseSensitive = true,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->count($substring, $caseSensitive);
$this->assertInternalType('int', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider replaceProvider()
*/
public function testReplace($expected, $str, $search, $replacement,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->replace($search, $replace);
$result = $stringy->replace($search, $replacement);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider regexReplaceProvider()
*/
public function testregexReplace($expected, $str, $pattern, $replacement,
$options = 'msr', $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->regexReplace($pattern, $replacement, $options);
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}