mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-05 02:41:46 +02:00
Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b990df3c61 | ||
|
220d4f95c0 | ||
|
b3e922c517 | ||
|
edaf567a13 | ||
|
1d186ca79f | ||
|
ffe6c2575a | ||
|
3f5b6a2413 | ||
|
ebc58a51ef | ||
|
1c13f2ad7e | ||
|
7313dcc07e | ||
|
f14e8c16a4 | ||
|
9f930262af | ||
|
c230885a13 | ||
|
1f25d0c793 | ||
|
0e70d72352 | ||
|
d54628e6ae | ||
|
7335ee5c88 | ||
|
5e01d895db | ||
|
3458fd79fd | ||
|
6e5e5e055f | ||
|
88d5973701 | ||
|
75404a0338 | ||
|
bc96cdad0c | ||
|
6014ff52df | ||
|
6ba29637b2 | ||
|
2ada00ff62 | ||
|
2a197244a5 | ||
|
8b1a91fe0c | ||
|
906f217dc1 | ||
|
d4baab7583 | ||
|
0c8f0e9083 | ||
|
2155447ce8 | ||
|
77efce3a38 | ||
|
f6085ed2cb |
27
CHANGELOG.md
27
CHANGELOG.md
@@ -1,3 +1,30 @@
|
||||
### 1.4.0 (2014-02-12)
|
||||
|
||||
* Implemented the IteratorAggregate interface, added chars()
|
||||
* Renamed count() to countSubstr()
|
||||
* Updated count() to implement Countable interface
|
||||
* Implemented the ArrayAccess interface with positive and negative indices
|
||||
* Switched from PSR-0 to PSR-4 autoloading
|
||||
|
||||
### 1.3.0 (2013-12-16)
|
||||
|
||||
* Additional Bulgarian support for toAscii
|
||||
* str property made private
|
||||
* Constructor casts first argument to string
|
||||
* Constructor throws an InvalidArgumentException when given an array
|
||||
* Constructor throws an InvalidArgumentException when given an object without
|
||||
a __toString method
|
||||
|
||||
### 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
|
||||
|
142
README.md
142
README.md
@@ -1,17 +1,21 @@
|
||||
# Stringy
|
||||

|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
[](https://travis-ci.org/danielstjules/Stringy)
|
||||
|
||||
* [Requiring/Loading](#requiringloading)
|
||||
* [OO and Procedural](#oo-and-procedural)
|
||||
* [Implemented Interfaces](#implemented-interfaces)
|
||||
* [Methods](#methods)
|
||||
* [at](#at)
|
||||
* [camelize](#camelize)
|
||||
* [chars](#chars)
|
||||
* [collapseWhitespace](#collapsewhitespace)
|
||||
* [contains](#contains)
|
||||
* [count](#count)
|
||||
* [countSubstr](#countsubstr)
|
||||
* [create](#create)
|
||||
* [dasherize](#dasherize)
|
||||
* [endsWith](#endswith)
|
||||
@@ -67,13 +71,15 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
||||
|
||||
## Requiring/Loading
|
||||
|
||||
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
|
||||
If you're using Composer to manage dependencies, you can include the following
|
||||
in your composer.json file:
|
||||
|
||||
"require": {
|
||||
"danielstjules/stringy": "dev-master"
|
||||
}
|
||||
|
||||
Then, after running `composer update` or `php composer.phar update`, you can load the class using Composer's autoloading:
|
||||
Then, after running `composer update` or `php composer.phar update`, you can
|
||||
load the class using Composer's autoloading:
|
||||
|
||||
```php
|
||||
require 'vendor/autoload.php';
|
||||
@@ -82,9 +88,9 @@ require 'vendor/autoload.php';
|
||||
Otherwise, you can simply require the file directly:
|
||||
|
||||
```php
|
||||
require_once 'path/to/Stringy/src/Stringy/Stringy.php';
|
||||
require_once 'path/to/Stringy/src/Stringy.php';
|
||||
// or
|
||||
require_once 'path/to/Stringy/src/Stringy/StaticStringy.php';
|
||||
require_once 'path/to/Stringy/src/StaticStringy.php';
|
||||
```
|
||||
|
||||
And in either case, I'd suggest using an alias.
|
||||
@@ -106,9 +112,9 @@ use Stringy\Stringy as S;
|
||||
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
`Stringy\Stringy` contains a __toString() method, which returns the current
|
||||
string when the object is used in a string context. Its $str property is also
|
||||
public, and can be accessed directly if required, ie: `S::create('foo')->str // 'foo'`
|
||||
`Stringy\Stringy` has a __toString() method, which returns the current string
|
||||
when the object is used in a string context, ie:
|
||||
`(string) S::create('foo') // 'foo'`
|
||||
|
||||
Using the static wrapper, an alternative is the following:
|
||||
|
||||
@@ -118,10 +124,50 @@ $string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
|
||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
|
||||
```
|
||||
|
||||
## Implemented Interfaces
|
||||
|
||||
`Stringy\Stringy` implements the `IteratorAggregate` interface, meaning that
|
||||
`foreach` can be used with an instance of the class:
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Fòô Bàř', 'UTF-8');
|
||||
foreach ($stringy as $char) {
|
||||
echo $char;
|
||||
}
|
||||
// 'Fòô Bàř'
|
||||
```
|
||||
|
||||
It implements the `Countable` interface, enabling the use of `count()` to
|
||||
retrieve the number of characters in the string:
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Fòô', 'UTF-8');
|
||||
count($stringy); // 3
|
||||
```
|
||||
|
||||
Furthermore, the `ArrayAccess` interface has been implemented. As a result,
|
||||
`isset()` can be used to check if a character at a specific index exists. And
|
||||
since `Stringy\Stringy` is immutable, any call to `offsetSet` or `offsetUnset`
|
||||
will throw an exception. `offsetGet` has been implemented, however, and accepts
|
||||
both positive and negative indexes. Invalid indexes result in an
|
||||
`OutOfBoundsException`.
|
||||
|
||||
``` php
|
||||
$stringy = S::create('Bàř', 'UTF-8');
|
||||
echo $stringy[2]; // 'ř'
|
||||
echo $stringy[-2]; // 'à'
|
||||
isset($stringy[-4]); // false
|
||||
|
||||
$stringy[3]; // OutOfBoundsException
|
||||
$stringy[2] = 'a'; // Exception
|
||||
```
|
||||
|
||||
## 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.
|
||||
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. Stringy objects are immutable.
|
||||
|
||||
*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*
|
||||
|
||||
@@ -129,9 +175,9 @@ method in `Stringy\StaticStringy`. For all others, they're found in `Stringy\Str
|
||||
|
||||
$stringy->at(int $index)
|
||||
|
||||
S::substr(int $index [, string $encoding ])
|
||||
S::at(int $index [, string $encoding ])
|
||||
|
||||
Returns the character of the string at $index, with indexes starting at 0.
|
||||
Returns the character at $index, with indexes starting at 0.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->at(6);
|
||||
@@ -144,15 +190,28 @@ $stringy->camelize();
|
||||
|
||||
S::camelize(string $str [, string $encoding ])
|
||||
|
||||
Returns a camelCase version of the supplied string. Trims surrounding
|
||||
spaces, capitalizes letters following digits, spaces, dashes and
|
||||
underscores, and removes spaces, dashes, underscores.
|
||||
Returns a camelCase version of the string. Trims surrounding spaces,
|
||||
capitalizes letters following digits, spaces, dashes and underscores,
|
||||
and removes spaces, dashes, as well as underscores.
|
||||
|
||||
```php
|
||||
S::create('Camel-Case')->camelize();
|
||||
S::camelize('Camel-Case'); // 'camelCase'
|
||||
```
|
||||
|
||||
#### chars
|
||||
|
||||
$stringy->chars();
|
||||
|
||||
S::chars(string $str [, string $encoding ])
|
||||
|
||||
Returns an array consisting of the characters in the string.
|
||||
|
||||
```php
|
||||
S::create('Fòô Bàř', 'UTF-8')->chars();
|
||||
S::chars('Fòô Bàř', 'UTF-8'); // array(F', 'ò', 'ô', ' ', 'B', 'à', 'ř')
|
||||
```
|
||||
|
||||
#### collapseWhitespace
|
||||
|
||||
$stringy->collapseWhitespace()
|
||||
@@ -183,28 +242,30 @@ S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφ
|
||||
S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'); // true
|
||||
```
|
||||
|
||||
#### count
|
||||
#### countSubstr
|
||||
|
||||
$stringy->count(string $substring [, boolean $caseSensitive = true ])
|
||||
$stringy->countSubstr(string $substring [, boolean $caseSensitive = true ])
|
||||
|
||||
S::count(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
S::countSubstr(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
|
||||
|
||||
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('α');
|
||||
S::count('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
|
||||
S::create('Ο συγγραφέας είπε', 'UTF-8')->countSubstr('α');
|
||||
S::countSubstr('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
|
||||
```
|
||||
|
||||
#### create
|
||||
|
||||
S::create(string $str, [, $encoding ])
|
||||
S::create(mixed $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 initialized object.
|
||||
the supplied values. $str is cast to a string prior to assignment, and if
|
||||
$encoding is not specified, it defaults to mb_internal_encoding(). It
|
||||
then returns the initialized object. Throws an InvalidArgumentException
|
||||
if the first argument is an array or object without a __toString method.
|
||||
|
||||
```php
|
||||
$stringy = S::create('fòô bàř', 'UTF-8'); // 'fòô bàř'
|
||||
@@ -503,7 +564,7 @@ string used for padding is a space, and the default type (one of 'left',
|
||||
$padType isn't one of those 3 values.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left',);
|
||||
S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left');
|
||||
S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
|
||||
```
|
||||
|
||||
@@ -571,7 +632,7 @@ $stringy->removeLeft(string $substring)
|
||||
|
||||
S::removeLeft(string $str, string $substring [, string $encoding ])
|
||||
|
||||
Returns a new string with the prefix $substring removed, if it was present.
|
||||
Returns a new string with the prefix $substring removed, if present.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
|
||||
@@ -584,7 +645,7 @@ $stringy->removeRight(string $substring)
|
||||
|
||||
S::removeRight(string $str, string $substring [, string $encoding ])
|
||||
|
||||
Returns a new string with the suffix $substring removed, if it was present.
|
||||
Returns a new string with the suffix $substring removed, if present.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
|
||||
@@ -624,8 +685,8 @@ $stringy->safeTruncate(int $length, [, string $substring = '' ])
|
||||
S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||
|
||||
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
|
||||
split 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.
|
||||
|
||||
```php
|
||||
@@ -639,7 +700,8 @@ $stringy->shuffle()
|
||||
|
||||
S::shuffle(string $str [, string $encoding ])
|
||||
|
||||
A multibyte str_shuffle() function. It returns a string with its characters in random order.
|
||||
A multibyte str_shuffle() function. It returns a string with its characters in
|
||||
random order.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->shuffle();
|
||||
@@ -648,13 +710,14 @@ 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 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 dashes. The string
|
||||
characters with their closest ASCII equivalents, removing remaining
|
||||
non-ASCII and non-alphanumeric characters, and replacing whitespace with
|
||||
$replacement. The replacement defaults to a single dash, and the string
|
||||
is also converted to lowercase.
|
||||
|
||||
```php
|
||||
@@ -739,9 +802,8 @@ $stringy->titleize([ string $encoding ])
|
||||
S::titleize(string $str [, array $ignore [, string $encoding ]])
|
||||
|
||||
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.
|
||||
Ignores the case of other letters, preserving any acronyms. Also accepts
|
||||
an array, $ignore, allowing you to list words not to be capitalized.
|
||||
|
||||
```php
|
||||
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
|
||||
@@ -845,8 +907,8 @@ truncating occurs, the string is further truncated so that the substring
|
||||
may be appended without exceeding the desired length.
|
||||
|
||||
```php
|
||||
S::create('What are your plans today?')->safeTruncate(19, '...');
|
||||
S::safeTruncate('What are your plans today?', 19, '...'); // 'What are your pl...'
|
||||
S::create('What are your plans today?')->truncate(19, '...');
|
||||
S::truncate('What are your plans today?', 19, '...'); // 'What are your pl...'
|
||||
```
|
||||
|
||||
#### underscored
|
||||
|
@@ -5,6 +5,7 @@
|
||||
"multibyte", "string", "manipulation", "utility", "methods", "utf-8",
|
||||
"helpers", "utils", "utf"
|
||||
],
|
||||
"homepage": "https://github.com/danielstjules/Stringy",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@@ -16,7 +17,11 @@
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"support": {
|
||||
"issues": "https://github.com/danielstjules/Stringy/issues",
|
||||
"source": "https://github.com/danielstjules/Stringy"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "Stringy": "src/" }
|
||||
"psr-4": { "Stringy\\": "src/" }
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<directory>tests/Stringy</directory>
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
|
792
src/StaticStringy.php
Normal file
792
src/StaticStringy.php
Normal file
@@ -0,0 +1,792 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* Returns an array consisting of the characters in the string.
|
||||
*
|
||||
* @param string $str String for which to return the chars
|
||||
* @param string $encoding The character encoding
|
||||
* @return array An array of string chars
|
||||
*/
|
||||
public static function chars($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->chars();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->upperCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->lowerCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of the string. Trims surrounding spaces,
|
||||
* capitalizes letters following digits, spaces, dashes and underscores,
|
||||
* and removes spaces, dashes, as well as underscores.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->camelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->upperCamelize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->dasherize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->underscored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of the string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->swapCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a trimmed string with the first letter of each word capitalized.
|
||||
* Ignores the case of other letters, preserving any acronyms. Also accepts
|
||||
* an array, $ignore, allowing you to list words not to be capitalized.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->titleize($ignore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string A humanized string
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->humanize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string String with those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
{
|
||||
return (string) Stringy::create($str)->tidy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->collapseWhitespace();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 A string containing only ASCII characters
|
||||
*/
|
||||
public static function toAscii($str)
|
||||
{
|
||||
return (string) Stringy::create($str)->toAscii();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @throws \InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->pad($length, $padStr, $padType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the beginning of the
|
||||
* string is padded. Alias for pad() with a $padType of 'left'.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padLeft($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that the end of the string
|
||||
* is padded. Alias for pad() with a $padType of 'right'.
|
||||
*
|
||||
* @param string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padRight($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that both sides of the
|
||||
* 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
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->padBoth($length, $padStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->startsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string 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 enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->endsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each tab in 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
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
{
|
||||
return (string) Stringy::create($str)->toSpaces($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
{
|
||||
return (string) Stringy::create($str)->toTabs($tabLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (string) Stringy::create($str, $encoding)->toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (string) Stringy::create($str, $encoding)->toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the string into an URL slug. This includes replacing non-ASCII
|
||||
* characters with their closest ASCII equivalents, removing remaining
|
||||
* non-ASCII and non-alphanumeric 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 (string) Stringy::create($str)->slugify($replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)
|
||||
->contains($needle, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
{
|
||||
return (string) Stringy::create($str)->surround($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into the string at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->insert($substring, $index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function truncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->truncate($length, $substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* split words. If $substring is provided, and truncating occurs, the
|
||||
* string is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->safeTruncate($length, $substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed string. A multibyte version of strrev().
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string The shuffled string
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->shuffle();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trimmed string. An alias for PHP's trim() function.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common prefix between the string and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common prefix
|
||||
*/
|
||||
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonPrefix($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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common suffix
|
||||
*/
|
||||
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonSuffix($otherStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common substring
|
||||
*/
|
||||
public static function longestCommonSubstring($str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->longestCommonSubstring($otherStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return int The number of characters in $str given the encoding
|
||||
*/
|
||||
public static function length($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->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
|
||||
* @param int $length Maximum number of characters used
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The substring of $str
|
||||
*/
|
||||
public static function substr($str, $start, $length = null, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->substr($start, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The character at $index
|
||||
*/
|
||||
public static function at($str, $index, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->at($index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The first $n characters
|
||||
*/
|
||||
public static function first($str, $n, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->first($n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The last $n characters
|
||||
*/
|
||||
public static function last($str, $n, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->last($n);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string prefixed by the $substring
|
||||
*/
|
||||
public static function ensureLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->ensureLeft($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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string suffixed by the $substring
|
||||
*/
|
||||
public static function ensureRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->ensureRight($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the prefix $substring removed, if present.
|
||||
*
|
||||
* @param string $str String from which to remove the prefix
|
||||
* @param string $substring The prefix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the prefix $substring
|
||||
*/
|
||||
public static function removeLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->removeLeft($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the suffix $substring removed, if present.
|
||||
*
|
||||
* @param string $str String from which to remove the suffix
|
||||
* @param string $substring The suffix to remove
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the suffix $substring
|
||||
*/
|
||||
public static function removeRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->removeRight($substring);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only alphabetic chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphabetic chars
|
||||
*/
|
||||
public static function isAlpha($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlpha();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return bool Whether or not $str contains only alphanumeric chars
|
||||
*/
|
||||
public static function isAlphanumeric($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlphanumeric();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only whitespace characters
|
||||
*/
|
||||
public static function isBlank($str, $encoding = null)
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only lower case characters
|
||||
*/
|
||||
public static function isLowerCase($str, $encoding = null)
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only upper case characters
|
||||
*/
|
||||
public static function isUpperCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @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 isHexadecimal($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isHexadecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 countSubstr($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->countSubstr($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, $replacement, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)
|
||||
->replace($search,$replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (string) Stringy::create($str, $encoding)
|
||||
->regexReplace($pattern, $replacement, $options, $encoding);
|
||||
}
|
||||
}
|
@@ -2,38 +2,56 @@
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class Stringy
|
||||
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
{
|
||||
public $str;
|
||||
private $str;
|
||||
|
||||
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().
|
||||
* the supplied values. $str is cast to a string prior to assignment, and if
|
||||
* $encoding is not specified, it defaults to mb_internal_encoding(). Throws
|
||||
* an InvalidArgumentException if the first argument is an array or object
|
||||
* without a __toString method.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param mixed $str Value to modify, after being cast to string
|
||||
* @param string $encoding The character encoding
|
||||
* @throws \InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
*/
|
||||
public function __construct($str, $encoding = null)
|
||||
{
|
||||
$this->str = $str;
|
||||
if (is_array($str)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Passed value cannot be an array'
|
||||
);
|
||||
} else if (is_object($str) && !method_exists($str, '__toString')) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Passed object must have a __toString method'
|
||||
);
|
||||
}
|
||||
|
||||
$this->str = (string) $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 initialized object.
|
||||
* the supplied values. $str is cast to a string prior to assignment, and if
|
||||
* $encoding is not specified, it defaults to mb_internal_encoding(). It
|
||||
* then returns the initialized object. Throws an InvalidArgumentException
|
||||
* if the first argument is an array or object without a __toString method.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param mixed $str Value to modify, after being cast to string
|
||||
* @param string $encoding The character encoding
|
||||
* @return Stringy A Stringy object
|
||||
* @throws \InvalidArgumentException if an array or object without a
|
||||
* __toString method is passed as the first argument
|
||||
*/
|
||||
public static function create($str, $encoding = null)
|
||||
{
|
||||
return new self($str, $encoding);
|
||||
return new static($str, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,6 +64,110 @@ class Stringy
|
||||
return $this->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the string, implementing the countable interface.
|
||||
*
|
||||
* @return int The number of characters in the string, given the encoding
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return $this->length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new ArrayIterator, thus implementing the IteratorAggregate
|
||||
* interface. The ArrayIterator's constructor is passed an array of chars
|
||||
* in the multibyte string. This enables the use of foreach with instances
|
||||
* of Stringy\Stringy.
|
||||
*
|
||||
* @return \ArrayIterator An iterator for the characters in the string
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->chars());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether or not a character exists at an index. Offsets may be
|
||||
* negative to count from the last character in the string. Implements
|
||||
* part of the ArrayAccess interface.
|
||||
*
|
||||
* @param mixed $offset The index to check
|
||||
* @return boolean Whether or not the index exists
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
$length = $this->length();
|
||||
$offset = (int) $offset;
|
||||
|
||||
if ($offset >= 0) {
|
||||
return ($length > $offset);
|
||||
}
|
||||
|
||||
return ($length >= abs($offset));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character at the given index. Offsets may be negative to
|
||||
* count from the last character in the string. Implements part of the
|
||||
* ArrayAccess interface, and throws an OutOfBoundsException if the index
|
||||
* does not exist.
|
||||
*
|
||||
* @param mixed $offset The index from which to retrieve the char
|
||||
* @return mixed The character at the specified index
|
||||
* @throws \OutOfBoundsException If the positive or negative offset does
|
||||
* not exist
|
||||
*/
|
||||
public function offsetGet($offset) {
|
||||
$offset = (int) $offset;
|
||||
$length = $this->length();
|
||||
|
||||
if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) {
|
||||
throw new \OutOfBoundsException('No character exists at the index');
|
||||
}
|
||||
|
||||
return mb_substr($this->str, $offset, 1, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements part of the ArrayAccess interface, but throws an exception
|
||||
* when called. This maintains the immutability of Stringy objects.
|
||||
*
|
||||
* @param mixed $offset The index of the character
|
||||
* @param mixed $value Value to set
|
||||
* @throws \Exception When called
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
// Stringy is immutable, cannot directly set char
|
||||
throw new \Exception('Stringy object is immutable, cannot modify char');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements part of the ArrayAccess interface, but throws an exception
|
||||
* when called. This maintains the immutability of Stringy objects.
|
||||
*
|
||||
* @param mixed $offset The index of the character
|
||||
* @throws \Exception When called
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
// Don't allow directly modifying the string
|
||||
throw new \Exception('Stringy object is immutable, cannot unset char');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array consisting of the characters in the string.
|
||||
*
|
||||
* @return array An array of string chars
|
||||
*/
|
||||
public function chars()
|
||||
{
|
||||
$chars = array();
|
||||
for ($i = 0; $i < $this->length(); $i++) {
|
||||
$chars[] = $this->at($i)->str;
|
||||
}
|
||||
|
||||
return $chars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
@@ -79,9 +201,9 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of the supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
* Returns a camelCase version of the string. Trims surrounding spaces,
|
||||
* capitalizes letters following digits, spaces, dashes and underscores,
|
||||
* and removes spaces, dashes, as well as underscores.
|
||||
*
|
||||
* @return Stringy Object with $str in camelCase
|
||||
*/
|
||||
@@ -92,15 +214,15 @@ class Stringy
|
||||
|
||||
$camelCase = preg_replace_callback(
|
||||
'/[-_\s]+(.)?/u',
|
||||
function ($match) use (&$encoding) {
|
||||
return $match[1] ? mb_strtoupper($match[1], $encoding) : "";
|
||||
function($match) use ($encoding) {
|
||||
return $match[1] ? mb_strtoupper($match[1], $encoding) : '';
|
||||
},
|
||||
$stringy->trim()->lowerCaseFirst()->str
|
||||
);
|
||||
|
||||
$stringy->str = preg_replace_callback(
|
||||
'/[\d]+(.)?/u',
|
||||
function ($match) use (&$encoding) {
|
||||
function($match) use ($encoding) {
|
||||
return mb_strtoupper($match[0], $encoding);
|
||||
},
|
||||
$camelCase
|
||||
@@ -134,14 +256,13 @@ class Stringy
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$stringy = self::create($this->str, $this->encoding)->trim();
|
||||
$dasherized = mb_ereg_replace('\B([A-Z])', '-\1', $stringy->str);
|
||||
$dasherized = mb_ereg_replace('[-_\s]+', '-', $dasherized);
|
||||
$str = mb_ereg_replace('\B([A-Z])', '-\1', trim($this->str));
|
||||
$str = mb_ereg_replace('[-_\s]+', '-', $str);
|
||||
$str = mb_strtolower($str, $this->encoding);
|
||||
|
||||
mb_regex_encoding($regexEncoding);
|
||||
$stringy->str = mb_strtolower($dasherized, $stringy->encoding);
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,14 +279,13 @@ class Stringy
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$stringy = self::create($this->str, $this->encoding)->trim();
|
||||
$underscored = mb_ereg_replace('\B([A-Z])', '_\1', $stringy->str);
|
||||
$underscored = mb_ereg_replace('[-_\s]+', '_', $underscored);
|
||||
$str = mb_ereg_replace('\B([A-Z])', '_\1', trim($this->str));
|
||||
$str = mb_ereg_replace('[-_\s]+', '_', $str);
|
||||
$str = mb_strtolower($str, $this->encoding);
|
||||
|
||||
mb_regex_encoding($regexEncoding);
|
||||
$stringy->str = mb_strtolower($underscored, $stringy->encoding);
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,11 +300,12 @@ class Stringy
|
||||
|
||||
$stringy->str = preg_replace_callback(
|
||||
'/[\S]/u',
|
||||
function ($match) use (&$encoding) {
|
||||
if ($match[0] == mb_strtoupper($match[0], $encoding))
|
||||
function ($match) use ($encoding) {
|
||||
if ($match[0] == mb_strtoupper($match[0], $encoding)) {
|
||||
return mb_strtolower($match[0], $encoding);
|
||||
else
|
||||
} else {
|
||||
return mb_strtoupper($match[0], $encoding);
|
||||
}
|
||||
},
|
||||
$stringy->str
|
||||
);
|
||||
@@ -194,30 +315,31 @@ class Stringy
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Ignores the case of other letters, preserving any acronyms. Also accepts
|
||||
* an array, $ignore, allowing you to list words not to be capitalized.
|
||||
*
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return Stringy Object with a titleized $str
|
||||
*/
|
||||
public function titleize($ignore = null)
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding)->trim();
|
||||
$encoding = $stringy->encoding;
|
||||
$buffer = $this->trim();
|
||||
$encoding = $this->encoding;
|
||||
|
||||
$stringy->str = preg_replace_callback(
|
||||
$buffer = preg_replace_callback(
|
||||
'/([\S]+)/u',
|
||||
function ($match) use (&$encoding, &$ignore, &$stringy) {
|
||||
if ($ignore && in_array($match[0], $ignore))
|
||||
function ($match) use (&$encoding, &$ignore) {
|
||||
if ($ignore && in_array($match[0], $ignore)) {
|
||||
return $match[0];
|
||||
$stringy->str = $match[0];
|
||||
return $stringy->upperCaseFirst();
|
||||
} else {
|
||||
$stringy = new Stringy($match[0], $encoding);
|
||||
return (string) $stringy->upperCaseFirst();
|
||||
}
|
||||
},
|
||||
$stringy->str
|
||||
$buffer
|
||||
);
|
||||
|
||||
return $stringy;
|
||||
return new Stringy($buffer, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,12 +350,10 @@ class Stringy
|
||||
*/
|
||||
public function humanize()
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$str = str_replace('_id', '', $this->str);
|
||||
$str = str_replace('_', ' ', $str);
|
||||
|
||||
$humanized = str_replace('_id', '', $stringy->str);
|
||||
$stringy->str = str_replace('_', ' ', $humanized);
|
||||
|
||||
return $stringy->trim()->upperCaseFirst();
|
||||
return self::create($str, $this->encoding)->trim()->upperCaseFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,14 +365,13 @@ class Stringy
|
||||
*/
|
||||
public function tidy()
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$str = $this->str;
|
||||
$str = preg_replace('/\x{2026}/u', '...', $str);
|
||||
$str = preg_replace('/[\x{201C}\x{201D}]/u', '"', $str);
|
||||
$str = preg_replace('/[\x{2018}\x{2019}]/u', "'", $str);
|
||||
$str = preg_replace('/[\x{2013}\x{2014}]/u', '-', $str);
|
||||
|
||||
$stringy->str = preg_replace('/\x{2026}/u', '...', $stringy->str);
|
||||
$stringy->str = preg_replace('/[\x{201C}\x{201D}]/u', '"', $stringy->str);
|
||||
$stringy->str = preg_replace('/[\x{2018}\x{2019}]/u', "'", $stringy->str);
|
||||
$stringy->str = preg_replace('/[\x{2013}\x{2014}]/u', '-', $stringy->str);
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,11 +386,10 @@ class Stringy
|
||||
$regexEncoding = mb_regex_encoding();
|
||||
mb_regex_encoding($this->encoding);
|
||||
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$stringy->str = mb_ereg_replace('[[:space:]]+', ' ', $stringy);
|
||||
$str = mb_ereg_replace('[[:space:]]+', ' ', $this->str);
|
||||
mb_regex_encoding($regexEncoding);
|
||||
|
||||
return $stringy->trim();
|
||||
return self::create($str, $this->encoding)->trim();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,12 +400,12 @@ class Stringy
|
||||
*/
|
||||
public function toAscii()
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$str = $this->str;
|
||||
$charsArray = array(
|
||||
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
|
||||
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
|
||||
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
|
||||
'ᾶ', 'ᾷ', 'а'),
|
||||
'ᾶ', 'ᾷ', 'а', 'ъ'),
|
||||
'b' => array('б', 'β'),
|
||||
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
|
||||
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'),
|
||||
@@ -326,7 +444,7 @@ class Stringy
|
||||
'zh' => array('ж'),
|
||||
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
|
||||
'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ',
|
||||
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
|
||||
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ъ'),
|
||||
'B' => array('Б'),
|
||||
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
|
||||
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'),
|
||||
@@ -362,18 +480,18 @@ class Stringy
|
||||
);
|
||||
|
||||
foreach ($charsArray as $key => $value) {
|
||||
$stringy->str = str_replace($value, $key, $stringy->str);
|
||||
$str = str_replace($value, $key, $str);
|
||||
}
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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 int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
@@ -390,11 +508,13 @@ class Stringy
|
||||
}
|
||||
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$encoding = $stringy->encoding;
|
||||
$strLength = $stringy->length();
|
||||
$padStrLength = mb_strlen($padStr, $stringy->encoding);
|
||||
$padStrLength = mb_strlen($padStr, $encoding);
|
||||
|
||||
if ($length <= $strLength || $padStrLength <= 0)
|
||||
if ($length <= $strLength || $padStrLength <= 0) {
|
||||
return $stringy;
|
||||
}
|
||||
|
||||
// Number of times to repeat the padStr if left or right
|
||||
$times = ceil(($length - $strLength) / $padStrLength);
|
||||
@@ -403,12 +523,12 @@ class Stringy
|
||||
if ($padType == 'left') {
|
||||
// Repeat the pad, cut it, and prepend
|
||||
$leftPad = str_repeat($padStr, $times);
|
||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $this->encoding);
|
||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $encoding);
|
||||
$stringy->str = $leftPad . $stringy->str;
|
||||
} elseif ($padType == 'right') {
|
||||
// Append the repeated pad and get a substring of the given length
|
||||
$stringy->str = $stringy->str . str_repeat($padStr, $times);
|
||||
$stringy->str = mb_substr($stringy->str, 0, $length, $this->encoding);
|
||||
$stringy->str = mb_substr($stringy->str, 0, $length, $encoding);
|
||||
} else {
|
||||
// Number of times to repeat the padStr on both sides
|
||||
$paddingSize = ($length - $strLength) / 2;
|
||||
@@ -416,10 +536,10 @@ class Stringy
|
||||
|
||||
// Favour right padding over left, as with str_pad()
|
||||
$rightPad = str_repeat($padStr, $times);
|
||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $this->encoding);
|
||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $encoding);
|
||||
|
||||
$leftPad = str_repeat($padStr, $times);
|
||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $this->encoding);
|
||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $encoding);
|
||||
|
||||
$stringy->str = $leftPad . $stringy->str . $rightPad;
|
||||
}
|
||||
@@ -441,8 +561,8 @@ 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'.
|
||||
* 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
|
||||
@@ -454,7 +574,7 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string of a given length such that both sides of the string
|
||||
* Returns a new string of a given length such that both sides of the
|
||||
* string are padded. Alias for pad() with a $padType of 'both'.
|
||||
*
|
||||
* @param int $length Desired string length after padding
|
||||
@@ -467,8 +587,8 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string begins with $substring, false otherwise.
|
||||
* By default, the comparison is case-sensitive, but can be made insensitive
|
||||
* 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
|
||||
@@ -478,7 +598,8 @@ class Stringy
|
||||
public function startsWith($substring, $caseSensitive = true)
|
||||
{
|
||||
$substringLength = mb_strlen($substring, $this->encoding);
|
||||
$startOfStr = mb_substr($this->str, 0, $substringLength, $this->encoding);
|
||||
$startOfStr = mb_substr($this->str, 0, $substringLength,
|
||||
$this->encoding);
|
||||
|
||||
if (!$caseSensitive) {
|
||||
$substring = mb_strtolower($substring, $this->encoding);
|
||||
@@ -490,8 +611,8 @@ class Stringy
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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 enforce case-sensitivity
|
||||
@@ -522,30 +643,26 @@ class Stringy
|
||||
*/
|
||||
public function toSpaces($tabLength = 4)
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
|
||||
$spaces = str_repeat(' ', $tabLength);
|
||||
$stringy->str = str_replace("\t", $spaces, $stringy->str);
|
||||
$str = str_replace("\t", $spaces, $this->str);
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return Stringy Object whose $str has had spaces switched to tabs
|
||||
*/
|
||||
public function toTabs($tabLength = 4)
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
|
||||
$spaces = str_repeat(' ', $tabLength);
|
||||
$stringy->str = str_replace($spaces, "\t", $stringy->str);
|
||||
$str = str_replace($spaces, "\t", $this->str);
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -576,19 +693,22 @@ class Stringy
|
||||
|
||||
/**
|
||||
* 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 dashes. The string
|
||||
* characters with their closest ASCII equivalents, removing remaining
|
||||
* non-ASCII and non-alphanumeric 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->toAscii());
|
||||
$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));
|
||||
|
||||
return $stringy;
|
||||
}
|
||||
@@ -604,10 +724,13 @@ class Stringy
|
||||
*/
|
||||
public function contains($needle, $caseSensitive = true)
|
||||
{
|
||||
if ($caseSensitive)
|
||||
return (mb_strpos($this->str, $needle, 0, $this->encoding) !== false);
|
||||
else
|
||||
return (mb_stripos($this->str, $needle, 0, $this->encoding) !== false);
|
||||
$encoding = $this->encoding;
|
||||
|
||||
if ($caseSensitive) {
|
||||
return (mb_strpos($this->str, $needle, 0, $encoding) !== false);
|
||||
} else {
|
||||
return (mb_stripos($this->str, $needle, 0, $encoding) !== false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -618,10 +741,9 @@ class Stringy
|
||||
*/
|
||||
public function surround($substring)
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$stringy->str = implode('', array($substring, $stringy->str, $substring));
|
||||
$str = implode('', array($substring, $this->str, $substring));
|
||||
|
||||
return $stringy;
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -634,11 +756,13 @@ class Stringy
|
||||
public function insert($substring, $index)
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
if ($index > $stringy->length())
|
||||
if ($index > $stringy->length()) {
|
||||
return $stringy;
|
||||
}
|
||||
|
||||
$start = mb_substr($stringy->str, 0, $index, $stringy->encoding);
|
||||
$end = mb_substr($stringy->str, $index, $stringy->length(), $stringy->encoding);
|
||||
$end = mb_substr($stringy->str, $index, $stringy->length(),
|
||||
$stringy->encoding);
|
||||
|
||||
$stringy->str = $start . $substring . $end;
|
||||
|
||||
@@ -657,8 +781,9 @@ class Stringy
|
||||
public function truncate($length, $substring = '')
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
if ($length >= $stringy->length())
|
||||
if ($length >= $stringy->length()) {
|
||||
return $stringy;
|
||||
}
|
||||
|
||||
// Need to further trim the string so we can append the substring
|
||||
$substringLength = mb_strlen($substring, $stringy->encoding);
|
||||
@@ -672,8 +797,8 @@ class Stringy
|
||||
|
||||
/**
|
||||
* 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
|
||||
* split words. If $substring is provided, and truncating occurs, the
|
||||
* string is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param int $length Desired length of the truncated string
|
||||
@@ -683,20 +808,22 @@ class Stringy
|
||||
public function safeTruncate($length, $substring = '')
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
if ($length >= $stringy->length())
|
||||
if ($length >= $stringy->length()) {
|
||||
return $stringy;
|
||||
}
|
||||
|
||||
// Need to further trim the string so we can append the substring
|
||||
$substringLength = mb_strlen($substring, $stringy->encoding);
|
||||
$encoding = $stringy->encoding;
|
||||
$substringLength = mb_strlen($substring, $encoding);
|
||||
$length = $length - $substringLength;
|
||||
|
||||
$truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding);
|
||||
$truncated = mb_substr($stringy->str, 0, $length, $encoding);
|
||||
|
||||
// If the last word was truncated
|
||||
if (mb_strpos($stringy->str, ' ', $length - 1, $stringy->encoding) != $length) {
|
||||
// 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);
|
||||
if (mb_strpos($stringy->str, ' ', $length - 1, $encoding) != $length) {
|
||||
// Find pos of the last occurrence of a space, get up to that
|
||||
$lastPos = mb_strrpos($truncated, ' ', 0, $encoding);
|
||||
$truncated = mb_substr($truncated, 0, $lastPos, $encoding);
|
||||
}
|
||||
|
||||
$stringy->str = $truncated . $substring;
|
||||
@@ -759,20 +886,21 @@ class Stringy
|
||||
*/
|
||||
public function longestCommonPrefix($otherStr)
|
||||
{
|
||||
$maxLength = min($this->length(), mb_strlen($otherStr, $this->encoding));
|
||||
$encoding = $this->encoding;
|
||||
$maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
|
||||
|
||||
$longestCommonPrefix = '';
|
||||
for ($i = 0; $i < $maxLength; $i++) {
|
||||
$char = mb_substr($this->str, $i, 1, $this->encoding);
|
||||
$char = mb_substr($this->str, $i, 1, $encoding);
|
||||
|
||||
if ($char == mb_substr($otherStr, $i, 1, $this->encoding)) {
|
||||
if ($char == mb_substr($otherStr, $i, 1, $encoding)) {
|
||||
$longestCommonPrefix .= $char;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return self::create($longestCommonPrefix, $this->encoding);
|
||||
return self::create($longestCommonPrefix, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -783,20 +911,21 @@ class Stringy
|
||||
*/
|
||||
public function longestCommonSuffix($otherStr)
|
||||
{
|
||||
$maxLength = min($this->length(), mb_strlen($otherStr, $this->encoding));
|
||||
$encoding = $this->encoding;
|
||||
$maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
|
||||
|
||||
$longestCommonSuffix = '';
|
||||
for ($i = 1; $i <= $maxLength; $i++) {
|
||||
$char = mb_substr($this->str, -$i, 1, $this->encoding);
|
||||
$char = mb_substr($this->str, -$i, 1, $encoding);
|
||||
|
||||
if ($char == mb_substr($otherStr, -$i, 1, $this->encoding)) {
|
||||
if ($char == mb_substr($otherStr, -$i, 1, $encoding)) {
|
||||
$longestCommonSuffix = $char . $longestCommonSuffix;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return self::create($longestCommonSuffix, $this->encoding);
|
||||
return self::create($longestCommonSuffix, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -810,9 +939,10 @@ class Stringy
|
||||
{
|
||||
// Uses dynamic programming to solve
|
||||
// http://en.wikipedia.org/wiki/Longest_common_substring_problem
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
$encoding = $this->encoding;
|
||||
$stringy = self::create($this->str, $encoding);
|
||||
$strLength = $stringy->length();
|
||||
$otherLength = mb_strlen($otherStr, $stringy->encoding);
|
||||
$otherLength = mb_strlen($otherStr, $encoding);
|
||||
|
||||
// Return if either string is empty
|
||||
if ($strLength == 0 || $otherLength == 0) {
|
||||
@@ -826,8 +956,8 @@ class Stringy
|
||||
|
||||
for ($i = 1; $i <= $strLength; $i++){
|
||||
for ($j = 1; $j <= $otherLength; $j++){
|
||||
$strChar = mb_substr($stringy->str, $i - 1, 1, $stringy->encoding);
|
||||
$otherChar = mb_substr($otherStr, $j - 1, 1, $stringy->encoding);
|
||||
$strChar = mb_substr($stringy->str, $i - 1, 1, $encoding);
|
||||
$otherChar = mb_substr($otherStr, $j - 1, 1, $encoding);
|
||||
|
||||
if ($strChar == $otherChar) {
|
||||
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
|
||||
@@ -841,7 +971,7 @@ class Stringy
|
||||
}
|
||||
}
|
||||
|
||||
$stringy->str = mb_substr($stringy->str, $end - $len, $len, $stringy->encoding);
|
||||
$stringy->str = mb_substr($stringy->str, $end - $len, $len, $encoding);
|
||||
|
||||
return $stringy;
|
||||
}
|
||||
@@ -881,7 +1011,7 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character of the string at $index, with indexes starting at 0.
|
||||
* Returns the character at $index, with indexes starting at 0.
|
||||
*
|
||||
* @param int $index Position of the character
|
||||
* @return Stringy The character at $index
|
||||
@@ -940,8 +1070,9 @@ class Stringy
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
|
||||
if (!$stringy->startsWith($substring))
|
||||
if (!$stringy->startsWith($substring)) {
|
||||
$stringy->str = $substring . $stringy->str;
|
||||
}
|
||||
|
||||
return $stringy;
|
||||
}
|
||||
@@ -957,14 +1088,15 @@ class Stringy
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
|
||||
if (!$stringy->endsWith($substring))
|
||||
if (!$stringy->endsWith($substring)) {
|
||||
$stringy->str .= $substring;
|
||||
}
|
||||
|
||||
return $stringy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the prefix $substring removed, if it was present.
|
||||
* Returns a new string with the prefix $substring removed, if present.
|
||||
*
|
||||
* @param string $substring The prefix to remove
|
||||
* @return Stringy Object having a $str without the prefix $substring
|
||||
@@ -982,7 +1114,7 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new string with the suffix $substring removed, if it was present.
|
||||
* Returns a new string with the suffix $substring removed, if present.
|
||||
*
|
||||
* @param string $substring The suffix to remove
|
||||
* @return Stringy Object having a $str without the suffix $substring
|
||||
@@ -1017,7 +1149,8 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string 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
|
||||
*/
|
||||
@@ -1038,7 +1171,8 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
* Returns true if the string contains only hexadecimal chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str contains only hexadecimal chars
|
||||
*/
|
||||
@@ -1048,7 +1182,8 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false otherwise.
|
||||
* Returns true if the string contains only whitespace chars, false
|
||||
* otherwise.
|
||||
*
|
||||
* @return bool Whether or not $str contains only whitespace characters
|
||||
*/
|
||||
@@ -1073,7 +1208,8 @@ class Stringy
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if the string 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
|
||||
*/
|
||||
@@ -1083,7 +1219,8 @@ class Stringy
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string 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
|
||||
*/
|
||||
@@ -1111,18 +1248,18 @@ class Stringy
|
||||
* @param bool $caseSensitive Whether or not to enforce case-sensitivity
|
||||
* @return int The number of $substring occurrences
|
||||
*/
|
||||
public function count($substring, $caseSensitive = true)
|
||||
public function countSubstr($substring, $caseSensitive = true)
|
||||
{
|
||||
if (!$caseSensitive) {
|
||||
if ($caseSensitive) {
|
||||
return mb_substr_count($this->str, $substring, $this->encoding);
|
||||
}
|
||||
|
||||
$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 in $str by $replacement.
|
||||
*
|
@@ -1,767 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class StaticStringy
|
||||
{
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
public static function upperCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCaseFirst()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case.
|
||||
*
|
||||
* @param string $str String to modify
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
public static function lowerCaseFirst($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->lowerCaseFirst()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of the supplied string. Trims surrounding
|
||||
* spaces, capitalizes letters following digits, spaces, dashes and
|
||||
* underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $str String to convert to camelCase
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
public static function camelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->camelize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
public static function upperCamelize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->upperCamelize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Dasherized string
|
||||
*/
|
||||
public static function dasherize($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->dasherize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert
|
||||
* @param string $encoding The character encoding
|
||||
* @return string Underscored string
|
||||
*/
|
||||
public static function underscored($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->underscored()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a case swapped version of the string.
|
||||
*
|
||||
* @param string $str String to swap case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string String with each character's case swapped
|
||||
*/
|
||||
public static function swapCase($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->swapCase()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to titleize
|
||||
* @param string $encoding The character encoding
|
||||
* @param array $ignore An array of words not to capitalize
|
||||
* @return string Titleized string
|
||||
*/
|
||||
public static function titleize($str, $ignore = null, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->titleize($ignore)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string A humanized string
|
||||
*/
|
||||
public static function humanize($str, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->humanize()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string String with those characters removed
|
||||
*/
|
||||
public static function tidy($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->tidy()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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, $encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->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.
|
||||
*
|
||||
* @param string $str A string with non-ASCII characters
|
||||
* @return string A string containing only ASCII characters
|
||||
*/
|
||||
public static function toAscii($str)
|
||||
{
|
||||
return $result = Stringy::create($str)->toAscii()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $padType One of 'left', 'right', 'both'
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
* @throws InvalidArgumentException If $padType isn't one of 'right',
|
||||
* 'left' or 'both'
|
||||
*/
|
||||
public static function pad($str, $length, $padStr = ' ', $padType = 'right',
|
||||
$encoding = null)
|
||||
{
|
||||
return $result = Stringy::create($str, $encoding)
|
||||
->pad($length, $padStr, $padType)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padLeft($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padRight($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padRight($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 string $str String to pad
|
||||
* @param int $length Desired string length after padding
|
||||
* @param string $padStr String used to pad, defaults to space
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The padded string
|
||||
*/
|
||||
public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->padBoth($length, $padStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str starts with $substring
|
||||
*/
|
||||
public static function startsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->startsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string 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 enforce case-sensitivity
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str ends with $substring
|
||||
*/
|
||||
public static function endsWith($str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->endsWith($substring, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts each tab in 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
|
||||
* @param int $tabLength Number of spaces to replace each tab with
|
||||
* @return string String with tabs switched to spaces
|
||||
*/
|
||||
public static function toSpaces($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toSpaces($tabLength)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param string $str String to convert spaces to tabs
|
||||
* @param int $tabLength Number of spaces to replace with a tab
|
||||
* @return string String with spaces switched to tabs
|
||||
*/
|
||||
public static function toTabs($str, $tabLength = 4)
|
||||
{
|
||||
return Stringy::create($str)->toTabs($tabLength)->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
|
||||
* and non-ASCII characters, and replacing whitespace with dashes. The string
|
||||
* is also converted to lowercase.
|
||||
*
|
||||
* @param string $str Text to transform into an URL slug
|
||||
* @return string The corresponding URL slug
|
||||
*/
|
||||
public static function slugify($str)
|
||||
{
|
||||
return Stringy::create($str)->slugify()->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, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($haystack, $encoding)->contains($needle, $caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Surrounds a string with the given substring.
|
||||
*
|
||||
* @param string $str The string to surround
|
||||
* @param string $substring The substring to add to both sides
|
||||
* @return string The string with the substring prepended and appended
|
||||
*/
|
||||
public static function surround($str, $substring)
|
||||
{
|
||||
return Stringy::create($str)->surround($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts $substring into the string at the $index provided.
|
||||
*
|
||||
* @param string $str String to insert into
|
||||
* @param string $substring String to be inserted
|
||||
* @param int $index The index at which to insert the substring
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after the insertion
|
||||
*/
|
||||
public static function insert($str, $substring, $index, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->insert($substring, $index)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function truncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->truncate($length, $substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* chop words. If $substring is provided, and truncating occurs, the string
|
||||
* is further truncated so that the substring may be appended without
|
||||
* exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function safeTruncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->safeTruncate($length, $substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reversed string. A multibyte version of strrev().
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->reverse()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return string The shuffled string
|
||||
*/
|
||||
public static function shuffle($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->shuffle()->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the trimmed string. An alias for PHP's trim() function.
|
||||
*
|
||||
* @param string $str String to trim
|
||||
* @return string Trimmed $str
|
||||
*/
|
||||
public static function trim($str)
|
||||
{
|
||||
return trim($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common prefix between the string and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common prefix
|
||||
*/
|
||||
public static function longestCommonPrefix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonPrefix($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the longest common suffix between the string and $otherStr.
|
||||
*
|
||||
* @param string $str First string for comparison
|
||||
* @param string $otherStr Second string for comparison
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common suffix
|
||||
*/
|
||||
public static function longestCommonSuffix($str, $otherStr, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonSuffix($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The longest common substring
|
||||
*/
|
||||
public static function longestCommonSubstring($str, $otherStr,
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->longestCommonSubstring($otherStr)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return int The number of characters in $str given the encoding
|
||||
*/
|
||||
public static function length($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->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
|
||||
* @param int $length Maximum number of characters used
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The substring of $str
|
||||
*/
|
||||
public static function substr($str, $start, $length = null, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->substr($start, $length)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The character at $index
|
||||
*/
|
||||
public static function at($str, $index, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->at($index)->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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The first $n characters
|
||||
*/
|
||||
public static function first($str, $n, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->first($n)->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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The last $n characters
|
||||
*/
|
||||
public static function last($str, $n, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->last($n)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string prefixed by the $substring
|
||||
*/
|
||||
public static function ensureLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->ensureLeft($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string suffixed by the $substring
|
||||
*/
|
||||
public static function ensureRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->ensureRight($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the prefix $substring
|
||||
*/
|
||||
public static function removeLeft($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->removeLeft($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The string without the suffix $substring
|
||||
*/
|
||||
public static function removeRight($str, $substring, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->removeRight($substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only alphabetic chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only alphabetic chars
|
||||
*/
|
||||
public static function isAlpha($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlpha();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return bool Whether or not $str contains only alphanumeric chars
|
||||
*/
|
||||
public static function isAlphanumeric($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isAlphanumeric();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only whitespace chars, false otherwise.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only whitespace characters
|
||||
*/
|
||||
public static function isBlank($str, $encoding = null)
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only lower case characters
|
||||
*/
|
||||
public static function isLowerCase($str, $encoding = null)
|
||||
{
|
||||
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.
|
||||
*
|
||||
* @param string $str String to check
|
||||
* @param string $encoding The character encoding
|
||||
* @return bool Whether or not $str contains only upper case characters
|
||||
*/
|
||||
public static function isUpperCase($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string contains only hexadecimal chars, false otherwise.
|
||||
*
|
||||
* @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 isHexadecimal($str, $encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)->isHexadecimal();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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, $replacement, $encoding = null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
@@ -2,35 +2,40 @@
|
||||
|
||||
abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function stringsForUpperCaseFirst()
|
||||
public function charsProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(array(), ''),
|
||||
array(array('T', 'e', 's', 't'), 'Test'),
|
||||
array(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), 'Fòô Bàř', 'UTF-8')
|
||||
);
|
||||
}
|
||||
|
||||
public function upperCaseFirstProvider()
|
||||
{
|
||||
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 +50,11 @@ abstract 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 +69,11 @@ abstract 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 +90,11 @@ abstract 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,63 +111,53 @@ abstract 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('Ο συγγραφέας', ' Ο συγγραφέας '),
|
||||
@@ -178,13 +167,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('', ' '),
|
||||
array('', ''),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForToAscii()
|
||||
public function toAsciiProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array('foo bar', 'fòô bàř'),
|
||||
array(' TEST ', ' ŤÉŚŢ '),
|
||||
array('φ = z = 3', 'φ = ź = 3'),
|
||||
@@ -192,13 +179,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
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),
|
||||
@@ -235,47 +220,39 @@ abstract 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),
|
||||
@@ -288,13 +265,11 @@ abstract 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),
|
||||
@@ -307,13 +282,11 @@ abstract 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),
|
||||
@@ -321,52 +294,44 @@ abstract 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 stringsForToLowerCase()
|
||||
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'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForToUpperCase()
|
||||
public function toUpperCaseProvider()
|
||||
{
|
||||
$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'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSlugify()
|
||||
public function slugifyProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array('foo-bar', ' foo bar '),
|
||||
array('foo-dbar', " Foo d'Bar "),
|
||||
array('a-string-with-dashes', 'A string-with-dashes'),
|
||||
@@ -374,15 +339,16 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
|
||||
array('numbers-1234', 'numbers 1234'),
|
||||
array('perevirka-ryadka', 'перевірка рядка'),
|
||||
array('bukvar-s-bukvoy-y', 'букварь с буквой ы')
|
||||
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
|
||||
array('barzi-i-yarostni', 'бързи и яростни'),
|
||||
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'),
|
||||
@@ -405,26 +371,22 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
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),
|
||||
@@ -433,13 +395,11 @@ abstract 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),
|
||||
@@ -463,13 +423,11 @@ abstract 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),
|
||||
@@ -493,37 +451,31 @@ abstract 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 '),
|
||||
@@ -533,13 +485,11 @@ abstract 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'),
|
||||
@@ -551,13 +501,11 @@ abstract 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'),
|
||||
@@ -569,13 +517,11 @@ abstract 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'),
|
||||
@@ -587,25 +533,21 @@ abstract 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),
|
||||
@@ -616,13 +558,11 @@ abstract 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),
|
||||
@@ -632,13 +572,11 @@ abstract 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),
|
||||
@@ -652,13 +590,11 @@ abstract 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),
|
||||
@@ -672,13 +608,11 @@ abstract 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/'),
|
||||
@@ -690,13 +624,11 @@ abstract 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'),
|
||||
@@ -708,13 +640,11 @@ abstract 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 '),
|
||||
@@ -726,13 +656,11 @@ abstract 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'),
|
||||
@@ -744,13 +672,11 @@ abstract 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'),
|
||||
@@ -762,13 +688,11 @@ abstract 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'),
|
||||
@@ -783,13 +707,11 @@ abstract 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 "),
|
||||
@@ -803,13 +725,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array(false, ' z', 'UTF-8'),
|
||||
array(false, ' 1', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsJson()
|
||||
public function isJsonProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(false, ''),
|
||||
array(false, '123'),
|
||||
array(true, '{"foo": "bar"}'),
|
||||
@@ -825,13 +745,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array(true, '["fòô"]', 'UTF-8'),
|
||||
array(false, '{"fòô": "bàř"]', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsLowerCase()
|
||||
public function isLowerCaseProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(true, ''),
|
||||
array(true, 'foobar'),
|
||||
array(false, 'foo bar'),
|
||||
@@ -841,13 +759,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array(false, 'fòô bàř', 'UTF-8'),
|
||||
array(false, 'fòôbÀŘ', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsSerialized()
|
||||
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"}'),
|
||||
@@ -856,13 +772,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
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()
|
||||
public function isUpperCaseProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(true, ''),
|
||||
array(true, 'FOOBAR'),
|
||||
array(false, 'FOO BAR'),
|
||||
@@ -872,13 +786,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array(false, 'FÒÔ BÀŘ', 'UTF-8'),
|
||||
array(false, 'FÒÔBàř', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForIsHexadecimal()
|
||||
public function isHexadecimalProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(true, ''),
|
||||
array(true, 'abcdef'),
|
||||
array(true, 'ABCDEF'),
|
||||
@@ -893,13 +805,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array(false, '0123456789x', 'UTF-8'),
|
||||
array(false, 'ABCDEFx', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForCount()
|
||||
public function countSubstrProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array(0, '', 'foo'),
|
||||
array(0, 'foo', 'bar'),
|
||||
array(1, 'foo bar', 'foo'),
|
||||
@@ -916,13 +826,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
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'),
|
||||
@@ -938,13 +846,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),
|
||||
array('bàř bàř', 'fòô bàř fòô bàř', 'fòô ', '', 'UTF-8'),
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForRegexReplace()
|
||||
public function regexReplaceProvider()
|
||||
{
|
||||
$testData = array(
|
||||
return array(
|
||||
array('', '', '', ''),
|
||||
array('bar', 'foo', 'f[o]+', 'bar'),
|
||||
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
|
||||
@@ -953,7 +859,5 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('bàř', 'fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'),
|
||||
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
}
|
@@ -1,14 +1,27 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/StaticStringy.php");
|
||||
$base = realpath(dirname(__FILE__) . '/..');
|
||||
require("$base/src/StaticStringy.php");
|
||||
|
||||
use Stringy\StaticStringy as S;
|
||||
|
||||
class StaticStringyTestCase extends CommonTest
|
||||
{
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
public function testChars($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::chars($str, $encoding);
|
||||
$this->assertInternalType('array', $result);
|
||||
foreach ($result as $char) {
|
||||
$this->assertInternalType('string', $char);
|
||||
}
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCaseFirstProvider()
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -18,7 +31,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
* @dataProvider lowerCaseFirstProvider()
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -28,7 +41,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
* @dataProvider camelizeProvider()
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -38,7 +51,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
* @dataProvider upperCamelizeProvider()
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -48,7 +61,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
* @dataProvider dasherizeProvider()
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -58,7 +71,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
* @dataProvider underscoredProvider()
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -68,7 +81,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
* @dataProvider swapCaseProvider()
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -78,7 +91,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
* @dataProvider titleizeProvider()
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
@@ -89,7 +102,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
* @dataProvider humanizeProvider()
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -99,7 +112,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
* @dataProvider tidyProvider()
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
@@ -109,7 +122,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
* @dataProvider collapseWhitespaceProvider()
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -119,7 +132,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToAscii
|
||||
* @dataProvider toAsciiProvider()
|
||||
*/
|
||||
public function testToAscii($expected, $str)
|
||||
{
|
||||
@@ -129,7 +142,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
* @dataProvider padProvider()
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
@@ -148,7 +161,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
* @dataProvider padLeftProvider()
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -159,7 +172,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
* @dataProvider padRightProvider()
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -170,7 +183,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
* @dataProvider padBothProvider()
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -181,7 +194,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
* @dataProvider startsWithProvider()
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -192,7 +205,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
* @dataProvider endsWithProvider()
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -203,7 +216,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
* @dataProvider toSpacesProvider()
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
@@ -213,7 +226,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
* @dataProvider toTabsProvider()
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
@@ -223,7 +236,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToLowerCase
|
||||
* @dataProvider toLowerCaseProvider()
|
||||
*/
|
||||
public function testToLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -233,7 +246,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToUpperCase
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
public function testToUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -243,17 +256,17 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
* @dataProvider slugifyProvider()
|
||||
*/
|
||||
public function testSlugify($expected, $str)
|
||||
public function testSlugify($expected, $str, $replacement = '-')
|
||||
{
|
||||
$result = S::slugify($str);
|
||||
$result = S::slugify($str, $replacement);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForContains
|
||||
* @dataProvider containsProvider()
|
||||
*/
|
||||
public function testContains($expected, $haystack, $needle,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -264,7 +277,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
@@ -274,7 +287,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
* @dataProvider insertProvider()
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
@@ -285,7 +298,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTruncate
|
||||
* @dataProvider truncateProvider()
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
@@ -296,7 +309,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
* @dataProvider safeTruncateProvider()
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
@@ -307,7 +320,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
* @dataProvider reverseProvider()
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -317,7 +330,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
* @dataProvider shuffleProvider()
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
@@ -338,7 +351,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTrim
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
@@ -348,7 +361,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonPrefix
|
||||
* @dataProvider longestCommonPrefixProvider()
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
@@ -359,7 +372,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSuffix
|
||||
* @dataProvider longestCommonSuffixProvider()
|
||||
*/
|
||||
public function testLongestCommonSuffix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
@@ -370,7 +383,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSubstring
|
||||
* @dataProvider longestCommonSubstringProvider()
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
@@ -381,7 +394,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLength
|
||||
* @dataProvider lengthProvider()
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -391,7 +404,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSubstr
|
||||
* @dataProvider substrProvider()
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
@@ -402,7 +415,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForAt
|
||||
* @dataProvider atProvider()
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
@@ -412,7 +425,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForFirst
|
||||
* @dataProvider firstProvider()
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
@@ -422,7 +435,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLast
|
||||
* @dataProvider lastProvider()
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
@@ -432,7 +445,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureLeft
|
||||
* @dataProvider ensureLeftProvider()
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -442,7 +455,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureRight
|
||||
* @dataProvider ensureRightProvider()
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -452,7 +465,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveLeft
|
||||
* @dataProvider removeLeftProvider()
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -462,7 +475,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveRight
|
||||
* @dataProvider removeRightProvider()
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -472,7 +485,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlpha
|
||||
* @dataProvider isAlphaProvider()
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -482,7 +495,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlphanumeric
|
||||
* @dataProvider isAlphanumericProvider()
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -492,7 +505,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsBlank
|
||||
* @dataProvider isBlankProvider()
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -502,7 +515,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsJson
|
||||
* @dataProvider isJsonProvider()
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -512,7 +525,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
* @dataProvider isLowerCaseProvider()
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -522,7 +535,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsSerialized
|
||||
* @dataProvider isSerializedProvider()
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -532,7 +545,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
* @dataProvider isUpperCaseProvider()
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -542,7 +555,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
* @dataProvider isHexadecimalProvider()
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -552,18 +565,18 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
* @dataProvider countSubstrProvider()
|
||||
*/
|
||||
public function testCount($expected, $str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
public function testCountSubstr($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$result = S::count($str, $substring, $caseSensitive, $encoding);
|
||||
$result = S::countSubstr($str, $substring, $caseSensitive, $encoding);
|
||||
$this->assertInternalType('int', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReplace
|
||||
* @dataProvider replaceProvider()
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replacement,
|
||||
$encoding = null)
|
||||
@@ -574,7 +587,7 @@ class StaticStringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRegexReplace
|
||||
* @dataProvider regexReplaceProvider()
|
||||
*/
|
||||
public function testRegexReplace($expected, $str, $pattern, $replacement,
|
||||
$options = 'msr', $encoding = null)
|
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/Stringy.php");
|
||||
$base = realpath(dirname(__FILE__) . '/..');
|
||||
require("$base/src/Stringy.php");
|
||||
|
||||
use Stringy\Stringy as S;
|
||||
|
||||
@@ -11,15 +11,54 @@ class StringyTestCase extends CommonTest
|
||||
{
|
||||
$stringy = new S('foo bar', 'UTF-8');
|
||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||
$this->assertEquals('foo bar', $stringy->str);
|
||||
$this->assertEquals('foo bar', (string) $stringy);
|
||||
$this->assertEquals('UTF-8', $stringy->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testConstructWithArray()
|
||||
{
|
||||
(string) new S(array());
|
||||
$this->fail('Expecting exception when the constructor is passed an array');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testMissingToString()
|
||||
{
|
||||
(string) new S(new stdClass());
|
||||
$this->fail('Expecting exception when the constructor is passed an ' .
|
||||
'object without a __toString method');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toStringProvider()
|
||||
*/
|
||||
public function testToString($expected, $str)
|
||||
{
|
||||
$this->assertEquals($expected, (string) new S($str));
|
||||
}
|
||||
|
||||
public function toStringProvider()
|
||||
{
|
||||
return array(
|
||||
array('', null),
|
||||
array('', false),
|
||||
array('1', true),
|
||||
array('-9', -9),
|
||||
array('1.18', 1.18),
|
||||
array(' string ', ' string ')
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$stringy = S::create('foo bar', 'UTF-8');
|
||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||
$this->assertEquals('foo bar', $stringy->str);
|
||||
$this->assertEquals('foo bar', (string) $stringy);
|
||||
$this->assertEquals('UTF-8', $stringy->encoding);
|
||||
}
|
||||
|
||||
@@ -31,8 +70,105 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals('FÒÔ bÀŘ', $result);
|
||||
}
|
||||
|
||||
public function testCount()
|
||||
{
|
||||
$stringy = S::create('Fòô', 'UTF-8');
|
||||
$this->assertEquals(3, $stringy->count());
|
||||
$this->assertEquals(3, count($stringy));
|
||||
}
|
||||
|
||||
public function testGetIterator()
|
||||
{
|
||||
$stringy = S::create('Fòô Bàř', 'UTF-8');
|
||||
|
||||
$valResult = array();
|
||||
foreach ($stringy as $char) {
|
||||
$valResult[] = $char;
|
||||
}
|
||||
|
||||
$keyValResult = array();
|
||||
foreach ($stringy as $pos => $char) {
|
||||
$keyValResult[$pos] = $char;
|
||||
}
|
||||
|
||||
$this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $valResult);
|
||||
$this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $keyValResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
* @dataProvider offsetExistsProvider()
|
||||
*/
|
||||
public function testOffsetExists($expected, $offset)
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$this->assertEquals($expected, $stringy->offsetExists($offset));
|
||||
$this->assertEquals($expected, isset($stringy[$offset]));
|
||||
}
|
||||
|
||||
public function offsetExistsProvider()
|
||||
{
|
||||
return array(
|
||||
array(true, 0),
|
||||
array(true, 2),
|
||||
array(false, 3),
|
||||
array(true, -1),
|
||||
array(true, -3),
|
||||
array(false, -4)
|
||||
);
|
||||
}
|
||||
|
||||
public function testOffsetGet()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
|
||||
$this->assertEquals('f', $stringy->offsetGet(0));
|
||||
$this->assertEquals('ô', $stringy->offsetGet(2));
|
||||
|
||||
$this->assertEquals('ô', $stringy[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \OutOfBoundsException
|
||||
*/
|
||||
public function testOffsetGetOutOfBounds()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$test = $stringy[3];
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testOffsetSet()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
$stringy[1] = 'invalid';
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testOffsetUnset()
|
||||
{
|
||||
$stringy = S::create('fòô', 'UTF-8');
|
||||
unset($stringy[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider charsProvider()
|
||||
*/
|
||||
public function testChars($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->chars();
|
||||
$this->assertInternalType('array', $result);
|
||||
foreach ($result as $char) {
|
||||
$this->assertInternalType('string', $char);
|
||||
}
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider upperCaseFirstProvider()
|
||||
*/
|
||||
public function testUpperCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -42,7 +178,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
* @dataProvider lowerCaseFirstProvider()
|
||||
*/
|
||||
public function testLowerCaseFirst($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -54,7 +190,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
* @dataProvider camelizeProvider()
|
||||
*/
|
||||
public function testCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -66,7 +202,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
* @dataProvider upperCamelizeProvider()
|
||||
*/
|
||||
public function testUpperCamelize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -78,7 +214,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForDasherize
|
||||
* @dataProvider dasherizeProvider()
|
||||
*/
|
||||
public function testDasherize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -90,7 +226,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUnderscored
|
||||
* @dataProvider underscoredProvider()
|
||||
*/
|
||||
public function testUnderscored($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -102,7 +238,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSwapCase
|
||||
* @dataProvider swapCaseProvider()
|
||||
*/
|
||||
public function testSwapCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -114,7 +250,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTitleize
|
||||
* @dataProvider titleizeProvider()
|
||||
*/
|
||||
public function testTitleize($expected, $str, $ignore = null,
|
||||
$encoding = null)
|
||||
@@ -127,7 +263,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForHumanize
|
||||
* @dataProvider humanizeProvider()
|
||||
*/
|
||||
public function testHumanize($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -139,7 +275,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTidy
|
||||
* @dataProvider tidyProvider()
|
||||
*/
|
||||
public function testTidy($expected, $str)
|
||||
{
|
||||
@@ -151,7 +287,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCollapseWhitespace
|
||||
* @dataProvider collapseWhitespaceProvider()
|
||||
*/
|
||||
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -163,7 +299,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToAscii
|
||||
* @dataProvider toAsciiProvider()
|
||||
*/
|
||||
public function testToAscii($expected, $str)
|
||||
{
|
||||
@@ -175,7 +311,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPad
|
||||
* @dataProvider padProvider()
|
||||
*/
|
||||
public function testPad($expected, $str, $length, $padStr = ' ',
|
||||
$padType = 'right', $encoding = null)
|
||||
@@ -197,7 +333,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadLeft
|
||||
* @dataProvider padLeftProvider()
|
||||
*/
|
||||
public function testPadLeft($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -210,7 +346,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadRight
|
||||
* @dataProvider padRightProvider()
|
||||
*/
|
||||
public function testPadRight($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -223,7 +359,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForPadBoth
|
||||
* @dataProvider padBothProvider()
|
||||
*/
|
||||
public function testPadBoth($expected, $str, $length, $padStr = ' ',
|
||||
$encoding = null)
|
||||
@@ -236,7 +372,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForStartsWith
|
||||
* @dataProvider startsWithProvider()
|
||||
*/
|
||||
public function testStartsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -249,7 +385,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEndsWith
|
||||
* @dataProvider endsWithProvider()
|
||||
*/
|
||||
public function testEndsWith($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -262,7 +398,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToSpaces
|
||||
* @dataProvider toSpacesProvider()
|
||||
*/
|
||||
public function testToSpaces($expected, $str, $tabLength = 4)
|
||||
{
|
||||
@@ -274,7 +410,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToTabs
|
||||
* @dataProvider toTabsProvider()
|
||||
*/
|
||||
public function testToTabs($expected, $str, $tabLength = 4)
|
||||
{
|
||||
@@ -286,7 +422,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToLowerCase
|
||||
* @dataProvider toLowerCaseProvider()
|
||||
*/
|
||||
public function testToLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -298,7 +434,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForToUpperCase
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
public function testToUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -310,19 +446,19 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSlugify
|
||||
* @dataProvider slugifyProvider()
|
||||
*/
|
||||
public function testSlugify($expected, $str)
|
||||
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,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
@@ -335,7 +471,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSurround
|
||||
* @dataProvider surroundProvider()
|
||||
*/
|
||||
public function testSurround($expected, $str, $substring)
|
||||
{
|
||||
@@ -347,7 +483,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForInsert
|
||||
* @dataProvider insertProvider()
|
||||
*/
|
||||
public function testInsert($expected, $str, $substring, $index,
|
||||
$encoding = null)
|
||||
@@ -360,7 +496,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTruncate
|
||||
* @dataProvider truncateProvider()
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
@@ -373,7 +509,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
* @dataProvider reverseProvider()
|
||||
*/
|
||||
public function testReverse($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -385,7 +521,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForShuffle
|
||||
* @dataProvider shuffleProvider()
|
||||
*/
|
||||
public function testShuffle($str, $encoding = null)
|
||||
{
|
||||
@@ -408,7 +544,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTrim
|
||||
* @dataProvider trimProvider()
|
||||
*/
|
||||
public function testTrim($expected, $str)
|
||||
{
|
||||
@@ -420,7 +556,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonPrefix
|
||||
* @dataProvider longestCommonPrefixProvider()
|
||||
*/
|
||||
public function testLongestCommonPrefix($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
@@ -433,7 +569,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLongestCommonSubstring
|
||||
* @dataProvider longestCommonSubstringProvider()
|
||||
*/
|
||||
public function testLongestCommonSubstring($expected, $str, $otherStr,
|
||||
$encoding = null)
|
||||
@@ -446,7 +582,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLength
|
||||
* @dataProvider lengthProvider()
|
||||
*/
|
||||
public function testLength($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -458,7 +594,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSubstr
|
||||
* @dataProvider substrProvider()
|
||||
*/
|
||||
public function testSubstr($expected, $str, $start, $length = null,
|
||||
$encoding = null)
|
||||
@@ -471,7 +607,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForAt
|
||||
* @dataProvider atProvider()
|
||||
*/
|
||||
public function testAt($expected, $str, $index, $encoding = null)
|
||||
{
|
||||
@@ -483,7 +619,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForFirst
|
||||
* @dataProvider firstProvider()
|
||||
*/
|
||||
public function testFirst($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
@@ -495,7 +631,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLast
|
||||
* @dataProvider lastProvider()
|
||||
*/
|
||||
public function testLast($expected, $str, $n, $encoding = null)
|
||||
{
|
||||
@@ -507,7 +643,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureLeft
|
||||
* @dataProvider ensureLeftProvider()
|
||||
*/
|
||||
public function testEnsureLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -519,7 +655,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForEnsureRight
|
||||
* @dataProvider ensureRightProvider()
|
||||
*/
|
||||
public function testEnsureRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -531,7 +667,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveLeft
|
||||
* @dataProvider removeLeftProvider()
|
||||
*/
|
||||
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -543,7 +679,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRemoveRight
|
||||
* @dataProvider removeRightProvider()
|
||||
*/
|
||||
public function testRemoveRight($expected, $str, $substring, $encoding = null)
|
||||
{
|
||||
@@ -555,7 +691,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlpha
|
||||
* @dataProvider isAlphaProvider()
|
||||
*/
|
||||
public function testIsAlpha($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -567,7 +703,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsAlphanumeric
|
||||
* @dataProvider isAlphanumericProvider()
|
||||
*/
|
||||
public function testIsAlphanumeric($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -579,7 +715,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsBlank
|
||||
* @dataProvider isBlankProvider()
|
||||
*/
|
||||
public function testIsBlank($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -591,7 +727,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsJson
|
||||
* @dataProvider isJsonProvider()
|
||||
*/
|
||||
public function testIsJson($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -603,7 +739,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsLowerCase
|
||||
* @dataProvider isLowerCaseProvider()
|
||||
*/
|
||||
public function testIsLowerCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -615,7 +751,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsSerialized
|
||||
* @dataProvider isSerializedProvider()
|
||||
*/
|
||||
public function testIsSerialized($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -627,7 +763,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsUpperCase
|
||||
* @dataProvider isUpperCaseProvider()
|
||||
*/
|
||||
public function testIsUpperCase($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -639,7 +775,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForIsHexadecimal
|
||||
* @dataProvider isHexadecimalProvider()
|
||||
*/
|
||||
public function testIsHexadecimal($expected, $str, $encoding = null)
|
||||
{
|
||||
@@ -651,20 +787,20 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCount
|
||||
* @dataProvider countSubstrProvider()
|
||||
*/
|
||||
public function testCount($expected, $str, $substring, $caseSensitive = true,
|
||||
$encoding = null)
|
||||
public function testCountSubstr($expected, $str, $substring,
|
||||
$caseSensitive = true, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->count($substring, $caseSensitive);
|
||||
$result = $stringy->countSubstr($substring, $caseSensitive);
|
||||
$this->assertInternalType('int', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReplace
|
||||
* @dataProvider replaceProvider()
|
||||
*/
|
||||
public function testReplace($expected, $str, $search, $replacement,
|
||||
$encoding = null)
|
||||
@@ -677,7 +813,7 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForRegexReplace
|
||||
* @dataProvider regexReplaceProvider()
|
||||
*/
|
||||
public function testregexReplace($expected, $str, $pattern, $replacement,
|
||||
$options = 'msr', $encoding = null)
|
Reference in New Issue
Block a user