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

31 Commits
1.0.0 ... 1.2.0

Author SHA1 Message Date
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
7 changed files with 806 additions and 172 deletions

View File

@@ -1,3 +1,21 @@
### 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

132
README.md
View File

@@ -3,7 +3,6 @@
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)
[![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)
@@ -24,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)
@@ -36,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)
@@ -50,8 +53,10 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [tidy](#tidy)
* [titleize](#titleize)
* [toAscii](#toascii)
* [toLowerCase](#tolowercase)
* [toSpaces](#tospaces)
* [toTabs](#totabs)
* [toUpperCase](#touppercase)
* [trim](#trim)
* [truncate](#truncate)
* [underscored](#underscored)
@@ -98,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
@@ -109,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 a Stringy object or string 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
@@ -152,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 includes 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();
@@ -164,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 the string 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('συγγραφέας');
@@ -177,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 occurrences of $substring in the given string. 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('α');
@@ -337,6 +346,32 @@ 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()
@@ -350,6 +385,19 @@ 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()
@@ -359,8 +407,8 @@ S::isUpperCase(string $str [, string $encoding ])
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
@@ -501,6 +549,22 @@ 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)
@@ -529,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 ])
Returns a string with all occurrences of $search replaced with $replace.
Replaces all occurrences of $search in $str by $replacement.
```php
S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
@@ -700,6 +764,20 @@ 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 ])
@@ -729,6 +807,20 @@ 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()

View File

@@ -132,7 +132,6 @@ class StaticStringy
* 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)
@@ -142,14 +141,16 @@ class StaticStringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes 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;
}
/**
@@ -295,6 +296,32 @@ class StaticStringy
return Stringy::create($str)->toTabs($tabLength)->str;
}
/**
* Converts all characters in the string to lowercase. An alias for PHP's
* mb_strtolower().
*
* @param string $str String to convert case
* @param string $encoding The character encoding
* @return string The lowercase string
*/
public static function toLowerCase($str, $encoding = null)
{
return Stringy::create($str, $encoding)->toLowerCase()->str;
}
/**
* Converts all characters in the string to uppercase. An alias for PHP's
* mb_strtoupper().
*
* @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
@@ -310,16 +337,20 @@ class StaticStringy
}
/**
* Returns true if the string 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 $haystack String being checked
* @param string $needle Substring to look for
* @param string $encoding The character encoding
* @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);
}
/**
@@ -623,6 +654,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isBlank();
}
/**
* 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.
*
@@ -635,6 +678,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isLowerCase();
}
/**
* 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.
*
@@ -648,30 +703,65 @@ class StaticStringy
}
/**
* Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count()
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @param string $str The string to search through
* @param string $substring The substring to search for
* @param string $encoding The character encoding
* @return int The number of $substring occurrences
* @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();
}
/**
* Returns a string with all occurrences of $search replaced with $replace.
* 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)->str;
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,6 +8,20 @@ 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
@@ -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 self($str, $encoding);
}
/**
@@ -249,16 +257,21 @@ class Stringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes 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.
*
* @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();
}
/**
@@ -271,55 +284,81 @@ class Stringy
{
$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) {
@@ -336,9 +375,9 @@ class Stringy
* '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'
@@ -346,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'");
}
@@ -405,9 +444,9 @@ class Stringy
* 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 = ' ')
{
@@ -509,6 +548,32 @@ class Stringy
return $stringy;
}
/**
* 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
@@ -529,17 +594,20 @@ class Stringy
}
/**
* Returns true if the string 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);
}
/**
@@ -626,7 +694,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);
}
@@ -934,7 +1002,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)
@@ -969,6 +1037,16 @@ class Stringy
return $this->matchesPattern('^([[:alnum:]])*$');
}
/**
* 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.
*
@@ -979,6 +1057,21 @@ class Stringy
return $this->matchesPattern('^([[:space:]])*$');
}
/**
* 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.
*
@@ -1000,38 +1093,77 @@ class Stringy
}
/**
* Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count()
* Returns true if the string is serialized, false otherwise.
*
* @param string $substring The substring to search for
* @return bool Whether or not $str is serialized
*/
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)
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);
}
/**
* Returns a string with all occurrences of $search replaced with $replace.
* 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,6 +1,6 @@
<?php
class CommonTest extends PHPUnit_Framework_TestCase
abstract class CommonTest extends PHPUnit_Framework_TestCase
{
public function stringsForUpperCaseFirst()
{
@@ -173,6 +173,8 @@ class CommonTest extends PHPUnit_Framework_TestCase
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('', ''),
);
@@ -185,7 +187,10 @@ class CommonTest extends PHPUnit_Framework_TestCase
$testData = 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;
@@ -333,6 +338,32 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForToLowerCase()
{
$testData = 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'),
);
return $testData;
}
public function stringsForToUpperCase()
{
$testData = 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'),
);
return $testData;
}
public function stringsForSlugify()
{
$testData = array(
@@ -341,7 +372,9 @@ class CommonTest extends PHPUnit_Framework_TestCase
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', 'букварь с буквой ы')
);
return $testData;
@@ -353,14 +386,24 @@ class CommonTest extends PHPUnit_Framework_TestCase
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;
@@ -764,6 +807,28 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsJson()
{
$testData = 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'),
);
return $testData;
}
public function stringsForIsLowerCase()
{
$testData = array(
@@ -780,6 +845,21 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsSerialized()
{
$testData = 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'),
);
return $testData;
}
public function stringsForIsUpperCase()
{
$testData = array(
@@ -796,6 +876,27 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsHexadecimal()
{
$testData = 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'),
);
return $testData;
}
public function stringsForCount()
{
$testData = array(
@@ -806,7 +907,14 @@ 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;
@@ -817,12 +925,14 @@ class CommonTest extends PHPUnit_Framework_TestCase
$testData = 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'),
@@ -832,9 +942,18 @@ class CommonTest extends PHPUnit_Framework_TestCase
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 stringsForRegexReplace()
{
$testData = 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')
);
return $testData;
}
}

View File

@@ -111,9 +111,9 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider stringsForCollapseWhitespace
*/
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);
}
@@ -139,6 +139,14 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPadException()
{
$result = S::pad('string', 5, 'foo', 'bar');
}
/**
* @dataProvider stringsForPadLeft
*/
@@ -214,6 +222,26 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToLowerCase
*/
public function testToLowerCase($expected, $str, $encoding = null)
{
$result = S::toLowerCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToUpperCase
*/
public function testToUpperCase($expected, $str, $encoding = null)
{
$result = S::toUpperCase($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSlugify
*/
@@ -227,9 +255,10 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider stringsForContains
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
public function testContains($expected, $haystack, $needle,
$caseSensitive = true, $encoding = null)
{
$result = S::contains($haystack, $needle, $encoding);
$result = S::contains($haystack, $needle, $caseSensitive, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
@@ -292,10 +321,20 @@ class StaticStringyTestCase extends CommonTest
*/
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$result = S::shuffle($str, $encoding);
$encoding = $encoding ?: mb_internal_encoding();
$this->assertInternalType('string', $result);
$this->assertEquals(count_chars($str), count_chars($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);
}
}
/**
@@ -461,6 +500,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsJson
*/
public function testIsJson($expected, $str, $encoding = null)
{
$result = S::isJson($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsLowerCase
@@ -472,6 +521,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsSerialized
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
$result = S::isSerialized($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsUpperCase
*/
@@ -482,12 +541,23 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsHexadecimal
*/
public function testIsHexadecimal($expected, $str, $encoding = null)
{
$result = S::isHexadecimal($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCount
*/
public function testCount($expected, $str, $substring, $encoding = null)
public function testCount($expected, $str, $substring, $caseSensitive = true,
$encoding = null)
{
$result = S::count($str, $substring, $encoding);
$result = S::count($str, $substring, $caseSensitive, $encoding);
$this->assertInternalType('int', $result);
$this->assertEquals($expected, $result);
}
@@ -495,10 +565,21 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
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 stringsForRegexReplace
*/
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,6 +7,14 @@ 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');
@@ -145,9 +153,9 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider stringsForCollapseWhitespace
*/
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);
@@ -179,6 +187,15 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testPadException()
{
$stringy = S::create('foo');
$result = $stringy->pad(5, 'foo', 'bar');
}
/**
* @dataProvider stringsForPadLeft
*/
@@ -268,6 +285,30 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForToLowerCase
*/
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 stringsForToUpperCase
*/
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 stringsForSlugify
*/
@@ -283,10 +324,11 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider stringsForContains
*/
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);
@@ -347,12 +389,22 @@ class StringyTestCase extends CommonTest
*/
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->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals(count_chars($str), count_chars($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);
}
}
/**
@@ -538,6 +590,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsJson
*/
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 stringsForIsLowerCase
*/
@@ -550,6 +614,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsSerialized
*/
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 stringsForIsUpperCase
*/
@@ -563,12 +639,25 @@ class StringyTestCase extends CommonTest
}
/**
* @dataProvider stringsForCount
* @dataProvider stringsForIsHexadecimal
*/
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 stringsForCount
*/
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);
@@ -577,11 +666,24 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
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 stringsForRegexReplace
*/
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);